Class: PaperTrailChanges
- Inherits:
-
Object
- Object
- PaperTrailChanges
- Defined in:
- lib/paper_trail_changes.rb
Constant Summary collapse
- CHANGES_SINCE_VERSION_VALID_ARGS =
[:version_id, :version_at, :model, :attributes]
- CHANGES_HASH_VALID_ARGS =
[:attributes, :version_hash, :model, :column_types]
Class Method Summary collapse
- .changes_hash(args) ⇒ Object
- .changes_since_version(args) ⇒ Object
- .column_types_from_class(class_obj) ⇒ Object
- .last_version(model) ⇒ Object
Class Method Details
.changes_hash(args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/paper_trail_changes.rb', line 58 def self.changes_hash(args) args.each do |key, val| raise "Invalid argument: '#{key}'." unless CHANGES_HASH_VALID_ARGS.include?(key) end attributes, version_hash, model, column_types = args[:attributes], args[:version_hash], args[:model], args[:column_types] changes_since = {} attributes.each do |key, val| key_s = key.to_s if match = key_s.match(/^(.+)_attributes$/) # Nested model. Since paper-trail doesn't keep track of this just pass it through. changes_since[key] = val elsif version_hash.key?(key_s) last_val = version_hash[key_s] changed = false type = column_types[key_s] if type == :string || type == :date || type == :text || type == :datetime changed = true if last_val.to_s != val.to_s elsif type == :integer changed = true if last_val.to_i != val.to_i changed = true if last_val == nil && val elsif type == :boolean if last_val == true bool_i = 1 elsif last_val == false bool_i = 0 elsif last_val.to_i == 1 bool_i = 1 elsif last_val.to_i == 0 bool_i = 0 end changed = true if bool_i != val.to_i changed = true if last_val == nil && val else raise "Unknown type: '#{type}'." end changes_since[key_s] = val if changed else changes_since[key_s] = val end end return changes_since end |
.changes_since_version(args) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/paper_trail_changes.rb', line 10 def self.changes_since_version(args) args.each do |key, val| raise "Invalid argument: '#{key}'." unless CHANGES_SINCE_VERSION_VALID_ARGS.include?(key) end attributes, model = args[:attributes], args[:model] column_types = PaperTrailChanges.column_types_from_class(model.class) if args[:version_id] version_obj = Version.find(args[:version_id]) rescue nil elsif args[:version_at] version_model = model.version_at(args[:version_at]) end version_model = model if version_obj.nil? && version_model.nil? if version_obj version_hash = Psych.load(version_obj.object).stringify_keys elsif version_model version_hash = {} version_model.attributes.each do |key, val| version_hash[key.to_s] = version_model.__send__("#{key}_before_type_cast") end else raise "Dont know what to do?" end changes_since = PaperTrailChanges.changes_hash( :attributes => attributes, :version_hash => version_hash, :model => model, :column_types => column_types ) return changes_since end |
.column_types_from_class(class_obj) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/paper_trail_changes.rb', line 48 def self.column_types_from_class(class_obj) column_types = {} class_obj.columns_hash.each do |name, col| column_types[name] = col.type end return column_types end |
.last_version(model) ⇒ Object
4 5 6 7 |
# File 'lib/paper_trail_changes.rb', line 4 def self.last_version(model) version = Version.where(:item_type => model.class.name, :item_id => model.id, :event => :update).order(:id).reverse_order.first return version end |