Class: Card::Action

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
mod/01_history/lib/card/action.rb

Constant Summary collapse

TYPE =

replace with enum if we start using rails 4

[:create, :update, :delete].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cacheObject



27
28
29
# File 'mod/01_history/lib/card/action.rb', line 27

def cache
  Card::Cache[Action]
end

.delete_cardlessObject



37
38
39
40
# File 'mod/01_history/lib/card/action.rb', line 37

def delete_cardless
  left_join = 'LEFT JOIN cards ON card_actions.card_id = cards.id'
  Card::Action.joins(left_join).where('cards.id IS NULL').delete_all
end

.delete_oldObject



42
43
44
45
# File 'mod/01_history/lib/card/action.rb', line 42

def delete_old
  Card.find_each(&:delete_old_actions)
  Card::Act.delete_actionless
end

.fetch(id) ⇒ Object



31
32
33
34
35
# File 'mod/01_history/lib/card/action.rb', line 31

def fetch id
  cache.read(id.to_s) || begin
    cache.write id.to_s, Action.find(id.to_i)
  end
end

Instance Method Details

#action_typeObject



140
141
142
# File 'mod/01_history/lib/card/action.rb', line 140

def action_type
  TYPE[read_attribute(:action_type)]
end

#action_type=(value) ⇒ Object



136
137
138
# File 'mod/01_history/lib/card/action.rb', line 136

def action_type= value
  write_attribute :action_type, TYPE.index(value)
end

#cardObject



190
191
192
# File 'mod/01_history/lib/card/action.rb', line 190

def card
  Card.fetch card_id, look_in_trash: true
end

#cardtype_diff(opts = {}) ⇒ Object



169
170
171
172
# File 'mod/01_history/lib/card/action.rb', line 169

def cardtype_diff opts={}
  return unless new_type?
  Card::Diff.complete old_values[:cardtype], new_values[:cardtype], opts
end

#change_for(field) ⇒ Object



120
121
122
# File 'mod/01_history/lib/card/action.rb', line 120

def change_for field
  card_changes.where 'card_changes.field = ?', field_index(field)
end

#changed_fields(obj, changed_fields) ⇒ Object

This is the main API from Cards to history See also create_act_and_action, which needs to happen before this or we don’t have the action to call this method on.

When changes are stored for versioned attributes, this is the signal method. By overriding this method in a module, the module takes over handling of changes. Although the standard version stores the Changes in ActiveRecord models (Act, Action and Change records), these could be /dev/nulled for a history-less implementation, or handled by an external service.

If change streams are generated from database triggers, and we aren’t writing here (disabled history), we still have to generate change stream events in another way.



64
65
66
67
68
# File 'mod/01_history/lib/card/action.rb', line 64

def changed_fields obj, changed_fields
  changed_fields.each do |f|
    Card::Change.create field: f, value: obj[f], card_action_id: id
  end
end

#content_diff(diff_type = :expanded, opts = nil) ⇒ Object



174
175
176
177
178
179
180
181
# File 'mod/01_history/lib/card/action.rb', line 174

def content_diff diff_type=:expanded, opts=nil
  return unless new_content?
  if diff_type == :summary
    content_diff_object(opts).summary
  else
    content_diff_object(opts).complete
  end
end

#content_diff_object(opts = nil) ⇒ Object



183
184
185
186
187
188
# File 'mod/01_history/lib/card/action.rb', line 183

def content_diff_object opts=nil
  @diff ||= begin
    diff_args = opts || card.include_set_modules.diff_args
    Card::Diff.new old_values[:content], new_values[:content], diff_args
  end
end

#edit_infoObject



70
71
72
73
74
75
76
77
78
79
80
# File 'mod/01_history/lib/card/action.rb', line 70

def edit_info
  @edit_info ||= {
    action_type:  "#{action_type}d",
    new_content:  new_values[:content],
    new_name:     new_values[:name],
    new_cardtype: new_values[:cardtype],
    old_content:  old_values[:content],
    old_name:     old_values[:name],
    old_cardtype: old_values[:cardtype]
  }
end

#expireObject



20
21
22
# File 'mod/01_history/lib/card/action.rb', line 20

def expire
  self.class.cache.delete id.to_s
end

#field_index(field) ⇒ Object



107
108
109
110
111
112
113
# File 'mod/01_history/lib/card/action.rb', line 107

def field_index field
  if field.is_a? Integer
    field
  else
    Card::TRACKED_FIELDS.index(field.to_s)
  end
end

#green?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'mod/01_history/lib/card/action.rb', line 156

def green?
  content_diff_object.green?
end

#last_value_for(field) ⇒ Object



102
103
104
105
# File 'mod/01_history/lib/card/action.rb', line 102

def last_value_for field
  ch = card.last_change_on(field, before: self)
  ch && ch.value
end

#name_diff(opts = {}) ⇒ Object

def diff

@diff ||= { cardtype: type_diff, content: content_diff, name: name_diff}

end



164
165
166
167
# File 'mod/01_history/lib/card/action.rb', line 164

def name_diff opts={}
  return unless new_name?
  Card::Diff.complete old_values[:name], new_values[:name], opts
end

#new_content?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'mod/01_history/lib/card/action.rb', line 128

def new_content?
  new_value_for :db_content
end

#new_name?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'mod/01_history/lib/card/action.rb', line 132

def new_name?
  new_value_for :name
end

#new_type?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'mod/01_history/lib/card/action.rb', line 124

def new_type?
  new_value_for :type_id
end

#new_value_for(field) ⇒ Object



115
116
117
118
# File 'mod/01_history/lib/card/action.rb', line 115

def new_value_for field
  ch = card_changes.find_by(field: field_index(field))
  ch && ch.value
end

#new_valuesObject



82
83
84
85
86
87
88
89
90
# File 'mod/01_history/lib/card/action.rb', line 82

def new_values
  @new_values ||=
    {
      content:  new_value_for(:db_content),
      name:     new_value_for(:name),
      cardtype: ((typecard = Card[new_value_for(:type_id).to_i]) &&
                 typecard.name.capitalize)
    }
end

#old_valuesObject



92
93
94
95
96
97
98
99
100
# File 'mod/01_history/lib/card/action.rb', line 92

def old_values
  @old_values ||= {
    content:  last_value_for(:db_content),
    name:     last_value_for(:name),
    cardtype: ((value = last_value_for(:type_id)) &&
               (typecard = Card.find(value)) &&
               typecard.name.capitalize)
  }
end

#red?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'mod/01_history/lib/card/action.rb', line 152

def red?
  content_diff_object.red?
end

#revision_nrObject



148
149
150
# File 'mod/01_history/lib/card/action.rb', line 148

def revision_nr
  card.actions.index_of self
end

#set_actObject



144
145
146
# File 'mod/01_history/lib/card/action.rb', line 144

def set_act
  self.set_act ||= acts.last
end