Class: Changeling::Models::Logling
- Inherits:
-
Object
- Object
- Changeling::Models::Logling
- Extended by:
- ActiveModel::Naming
- Includes:
- Tire::Model::Callbacks, Tire::Model::Persistence, Tire::Model::Search
- Defined in:
- lib/changeling/models/logling.rb
Instance Attribute Summary collapse
-
#after ⇒ Object
Returns the value of attribute after.
-
#before ⇒ Object
Returns the value of attribute before.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#modifications ⇒ Object
Returns the value of attribute modifications.
-
#modified_at ⇒ Object
Returns the value of attribute modified_at.
-
#modified_by ⇒ Object
Returns the value of attribute modified_by.
-
#modified_fields ⇒ Object
Returns the value of attribute modified_fields.
-
#oid ⇒ Object
Returns the value of attribute oid.
Class Method Summary collapse
- .create(object) ⇒ Object
- .klassify(object) ⇒ Object
- .parse_changes(changes) ⇒ Object
- .records_for(object, length = nil, field = nil) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
- #id ⇒ Object
-
#initialize(object) ⇒ Logling
constructor
A new instance of Logling.
- #save ⇒ Object
- #to_indexed_json ⇒ Object
Constructor Details
#initialize(object) ⇒ Logling
Returns a new instance of Logling.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/changeling/models/logling.rb', line 93 def initialize(object) if object.class == Hash changes = JSON.parse(object['modifications']) self.klass = object['klass'].camelize.constantize self.oid = object['oid'].to_i.to_s == object['oid'] ? object['oid'].to_i : object['oid'] self.modified_by = object['modified_by'].to_i.to_s == object['modified_by'] ? object['modified_by'].to_i : object['modified_by'] self.modifications = changes self.modified_fields = self.modifications.keys self.before, self.after = Logling.parse_changes(changes) self.modified_at = DateTime.parse(object['modified_at']) else changes = object.changes.reject { |k, v| v.nil? } # Remove updated_at field. changes.delete("updated_at") self.klass = object.class self.oid = object.id self.modified_by = Changeling.blame_user.try(:id) || nil self.modifications = changes self.modified_fields = self.modifications.keys self.before, self.after = Logling.parse_changes(changes) if object.respond_to?(:updated_at) self.modified_at = object.updated_at else self.modified_at = Time.now end end end |
Instance Attribute Details
#after ⇒ Object
Returns the value of attribute after.
5 6 7 |
# File 'lib/changeling/models/logling.rb', line 5 def after @after end |
#before ⇒ Object
Returns the value of attribute before.
5 6 7 |
# File 'lib/changeling/models/logling.rb', line 5 def before @before end |
#klass ⇒ Object
Returns the value of attribute klass.
5 6 7 |
# File 'lib/changeling/models/logling.rb', line 5 def klass @klass end |
#modifications ⇒ Object
Returns the value of attribute modifications.
5 6 7 |
# File 'lib/changeling/models/logling.rb', line 5 def modifications @modifications end |
#modified_at ⇒ Object
Returns the value of attribute modified_at.
5 6 7 |
# File 'lib/changeling/models/logling.rb', line 5 def modified_at @modified_at end |
#modified_by ⇒ Object
Returns the value of attribute modified_by.
5 6 7 |
# File 'lib/changeling/models/logling.rb', line 5 def modified_by @modified_by end |
#modified_fields ⇒ Object
Returns the value of attribute modified_fields.
5 6 7 |
# File 'lib/changeling/models/logling.rb', line 5 def modified_fields @modified_fields end |
#oid ⇒ Object
Returns the value of attribute oid.
5 6 7 |
# File 'lib/changeling/models/logling.rb', line 5 def oid @oid end |
Class Method Details
.create(object) ⇒ Object
28 29 30 31 |
# File 'lib/changeling/models/logling.rb', line 28 def create(object) logling = self.new(object) logling.save end |
.klassify(object) ⇒ Object
45 46 47 |
# File 'lib/changeling/models/logling.rb', line 45 def klassify(object) object.class.to_s.underscore end |
.parse_changes(changes) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/changeling/models/logling.rb', line 33 def parse_changes(changes) before = {} after = {} changes.each_pair do |attr, values| before[attr] = values[0] after[attr] = values[1] end [before, after] end |
.records_for(object, length = nil, field = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/changeling/models/logling.rb', line 49 def records_for(object, length = nil, field = nil) filters = [ { :klass => Logling.klassify(object) }, { :oid => object.id.to_s } ] filters << { :modified_fields => field } if field sort = { :field => :modified_at, :direction => :desc } results = Changeling::Support::Search.find_by(:filters => filters, :sort => sort, :size => length) end |
Instance Method Details
#as_json ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/changeling/models/logling.rb', line 78 def as_json { :class => self.klass, :oid => self.oid, :modified_by => self.modified_by, :modifications => self.modifications, :modified_at => self.modified_at } end |
#id ⇒ Object
88 89 90 91 |
# File 'lib/changeling/models/logling.rb', line 88 def id # Make sure ElasticSearch creates new entries rather than update old entries. Digest::MD5.hexdigest("#{self.klass}:#{self.oid}:#{self.modifications}") end |
#save ⇒ Object
126 127 128 129 130 |
# File 'lib/changeling/models/logling.rb', line 126 def save unless self.modifications.empty? self.update_index end end |
#to_indexed_json ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/changeling/models/logling.rb', line 66 def to_indexed_json { :id => self.id, :klass => self.klass.to_s.underscore, :oid => self.oid.to_s, :modified_by => self.modified_by, :modifications => self.modifications.to_json, :modified_at => self.modified_at, :modified_fields => self.modified_fields }.to_json end |