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 Method Summary collapse

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



4
5
6
# File 'app/models/effective/resources/instance.rb', line 4

def instance
  @instance
end

Instance Method Details

#instance_attributes(only: nil, except: nil) ⇒ Object

called by effective_trash and effective_logging



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
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
# File 'app/models/effective/resources/instance.rb', line 14

def instance_attributes(only: nil, except: nil)
  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

  # 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.options[: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

  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

  attributes.delete_if { |_, value| value.blank? }
end

#instance_changes(only: nil, except: nil) ⇒ Object

used by effective_logging



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
107
108
# File 'app/models/effective/resources/instance.rb', line 78

def instance_changes(only: nil, except: nil)
  return {} unless (instance.present? && instance.previous_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?

  # 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