Module: Card::Action::Changes

Included in:
Card::Action
Defined in:
mod/history/lib/card/action/changes.rb

Overview

methods for relating Card::Action to Card::Change

Instance Method Summary collapse

Instance Method Details

#all_changesObject



46
47
48
49
50
51
# File 'mod/history/lib/card/action/changes.rb', line 46

def all_changes
  self.class.cache.fetch("#{id}-changes") do
    # using card_changes causes caching problem
    Card::Change.where(card_action_id: id).to_a
  end
end

#change(field) ⇒ Change

action’s Change object for given field

Returns:

See Also:

  • #interpret_field for field param


27
28
29
# File 'mod/history/lib/card/action/changes.rb', line 27

def change field
  changes[interpret_field field]
end

#changed_valuesObject

all changed values in hash form. { field1: new_value }



67
68
69
# File 'mod/history/lib/card/action/changes.rb', line 67

def changed_values
  @changed_values ||= changes.transform_values(&:value)
end

#changesHash

all action changes in hash form. { field1: Change1 }

Returns:

  • (Hash)


55
56
57
58
59
60
61
62
63
64
# File 'mod/history/lib/card/action/changes.rb', line 55

def changes
  @changes ||=
    if sole?
      current_changes
    else
      all_changes.each_with_object({}) do |change, hash|
        hash[change.field.to_sym] = change
      end
    end
end

#current_changesHash

Returns:

  • (Hash)


72
73
74
75
76
77
78
79
80
81
# File 'mod/history/lib/card/action/changes.rb', line 72

def current_changes
  return {} unless card

  @current_changes ||=
    Card::Change::TRACKED_FIELDS.each_with_object({}) do |field, hash|
      hash[field.to_sym] = Card::Change.new field: field,
                                            value: card.send(field),
                                            card_action_id: id
    end
end

#previous_change(field) ⇒ Change

most recent change to given field before this one

Returns:

See Also:

  • #interpret_field for field param


34
35
36
37
38
39
40
41
42
43
44
# File 'mod/history/lib/card/action/changes.rb', line 34

def previous_change field
  return nil if action_type == :create

  field = interpret_field field
  if @previous_changes&.key?(field)
    @previous_changes[field]
  else
    @previous_changes ||= {}
    @previous_changes[field] = card.last_change_on field, before: self
  end
end

#previous_value(field) ⇒ Object

value of field set by most recent Change before this one

See Also:

  • #interpret_field for field param
  • #interpret_field for field param


17
18
19
20
21
22
# File 'mod/history/lib/card/action/changes.rb', line 17

def previous_value field
  return if action_type == :create
  return unless (previous_change = previous_change field)

  interpret_value field, previous_change.value
end

#value(field) ⇒ Object

value set by action’s Change to given field

See Also:

  • #interpret_field for field param
  • #interpret_value for return values


8
9
10
11
12
# File 'mod/history/lib/card/action/changes.rb', line 8

def value field
  return unless (change = change field)

  interpret_value field, change.value
end