Module: Effective::Resources::Instance
- Included in:
- Effective::Resource
- Defined in:
- app/models/effective/resources/instance.rb
Constant Summary collapse
- BLACKLIST =
This is written for use by effective_logging and effective_trash
[:logged_changes, :trash]
Instance Attribute Summary collapse
-
#instance ⇒ Object
Returns the value of attribute instance.
Instance Method Summary collapse
- #instance_action_texts_previous_changes ⇒ Object
-
#instance_attributes(only: nil, except: nil, associations: true) ⇒ Object
called by effective_trash and effective_logging.
-
#instance_changes(only: nil, except: nil) ⇒ Object
used by effective_logging.
Instance Attribute Details
#instance ⇒ Object
Returns the value of attribute instance.
6 7 8 |
# File 'app/models/effective/resources/instance.rb', line 6 def instance @instance end |
Instance Method Details
#instance_action_texts_previous_changes ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'app/models/effective/resources/instance.rb', line 81 def instance_action_texts_previous_changes return {} unless instance.present? && action_texts.present? action_texts .map { |ass| instance.send(ass.name) } .compact .flatten .select { |obj| obj.previous_changes['body'].present? } .inject({}) { |h, obj| h[obj.name.to_sym] = obj.previous_changes['body']; h } end |
#instance_attributes(only: nil, except: nil, associations: true) ⇒ Object
called by effective_trash and effective_logging
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/effective/resources/instance.rb', line 16 def instance_attributes(only: nil, except: nil, associations: true) return {} unless instance.present? # Build up our only and except only = Array(only).map(&:to_sym) except = Array(except).map(&:to_sym) + BLACKLIST # Simple Attributes attributes = { attributes: instance.attributes.symbolize_keys } attributes[:attributes] = attributes[:attributes].except(*except) if except.present? attributes[:attributes] = attributes[:attributes].slice(*only) if only.present? # Collect to_s representations of all belongs_to associations belong_tos.each do |association| next if except.present? && except.include?(association.name) next unless only.blank? || only.include?(association.name) attributes[association.name] = instance.send(association.name).to_s end if associations # Grab the instance attributes of each nested resource nested_resources.each do |association| next if except.present? && except.include?(association.name) next unless only.blank? || only.include?(association.name) next if association.[:through] attributes[association.name] ||= {} Array(instance.send(association.name)).each_with_index do |child, index| next unless child.present? resource = Effective::Resource.new(child) attributes[association.name][index] = resource.instance_attributes(only: only, except: except) end end (action_texts + has_ones).each do |association| next if except.present? && except.include?(association.name) next unless only.blank? || only.include?(association.name) attributes[association.name] = instance.send(association.name).to_s end has_manys.each do |association| next if except.present? && except.include?(association.name) next unless only.blank? || only.include?(association.name) next if BLACKLIST.include?(association.name) attributes[association.name] = instance.send(association.name).map { |obj| obj.to_s } end has_and_belongs_to_manys.each do |association| next if except.present? && except.include?(association.name) next unless only.blank? || only.include?(association.name) attributes[association.name] = instance.send(association.name).map { |obj| obj.to_s } end end attributes.delete_if { |_, value| value.blank? } end |
#instance_changes(only: nil, except: nil) ⇒ Object
used by effective_logging
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 125 126 127 128 129 130 131 132 133 |
# File 'app/models/effective/resources/instance.rb', line 93 def instance_changes(only: nil, except: nil) return {} unless instance.present? action_texts_changes = instance_action_texts_previous_changes() return {} unless instance.previous_changes.present? || action_texts_changes.present? # Build up our only and except only = Array(only).map(&:to_sym) except = Array(except).map(&:to_sym) + BLACKLIST changes = instance.previous_changes.symbolize_keys.delete_if do |attribute, (before, after)| begin (before.kind_of?(ActiveSupport::TimeWithZone) && after.kind_of?(ActiveSupport::TimeWithZone) && before.to_i == after.to_i) || (before == nil && after == false) || (before == nil && after == ''.freeze) rescue => e true end end changes = changes.except(*except) if except.present? changes = changes.slice(*only) if only.present? action_texts_changes.each do |name, (before, after)| next if except.present? && except.include?(name) next unless only.blank? || only.include?(name) changes[name] = [before.to_s, after.to_s] end # Log to_s changes on all belongs_to associations belong_tos.each do |association| next if except.present? && except.include?(association.name) next unless only.blank? || only.include?(association.name) if (change = changes.delete(association.foreign_key)).present? changes[association.name] = [(association.klass.find_by_id(change.first) if changes.first), instance.send(association.name)] end end changes end |