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]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cacheObject



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

def cache
  Card::Cache[Action]
end

.delete_cardlessObject



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

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

.delete_oldObject



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

def delete_old
  Card.find_each do |card|
    card.delete_old_actions
  end
  Card::Act.delete_actionless
end

.fetch(id) ⇒ Object



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

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

Instance Method Details

#action_typeObject



134
135
136
# File 'mod/01_history/lib/card/action.rb', line 134

def action_type
  TYPE[read_attribute(:action_type)]
end

#action_type=(value) ⇒ Object



130
131
132
# File 'mod/01_history/lib/card/action.rb', line 130

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

#cardObject



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

def card
  Card.fetch card_id, look_in_trash: true
end

#cardtype_diff(opts = {}) ⇒ Object



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

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

#change_for(field) ⇒ Object



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

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 active record 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.



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

def changed_fields obj, changed_fields
  #changed_fields.each{ |f| changes.build :field => f, :value => self[f] }
  changed_fields.each{ |f| Card::Change.create :field => f, :value => obj[f], :card_action_id=>id }
end

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



170
171
172
173
174
175
176
177
178
# File 'mod/01_history/lib/card/action.rb', line 170

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

#content_diff_builder(opts = nil) ⇒ Object



180
181
182
183
184
# File 'mod/01_history/lib/card/action.rb', line 180

def content_diff_builder opts=nil
  @content_diff_builder ||= begin
    Card::Diff::DiffBuilder.new(old_values[:content], new_values[:content], opts || card.include_set_modules.diff_args)
  end
end

#edit_infoObject



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

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



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

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

#field_index(field) ⇒ Object



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

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

#green?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'mod/01_history/lib/card/action.rb', line 150

def green?
  content_diff_builder.green?
end

#last_value_for(field) ⇒ Object



98
99
100
# File 'mod/01_history/lib/card/action.rb', line 98

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

#name_diff(opts = {}) ⇒ Object

def diff

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

end



158
159
160
161
162
# File 'mod/01_history/lib/card/action.rb', line 158

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

#new_content?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'mod/01_history/lib/card/action.rb', line 122

def new_content?
  new_value_for(:db_content)
end

#new_name?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'mod/01_history/lib/card/action.rb', line 126

def new_name?
  new_value_for(:name)
end

#new_type?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'mod/01_history/lib/card/action.rb', line 118

def new_type?
  new_value_for(:type_id)
end

#new_value_for(field) ⇒ Object



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

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

#new_valuesObject



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

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] and typecard.name.capitalize )
  }
end

#old_valuesObject



89
90
91
92
93
94
95
96
# File 'mod/01_history/lib/card/action.rb', line 89

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

#red?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'mod/01_history/lib/card/action.rb', line 146

def red?
  content_diff_builder.red?
end

#revision_nrObject



142
143
144
# File 'mod/01_history/lib/card/action.rb', line 142

def revision_nr
  self.card.actions.index_of(self)
end

#set_actObject



138
139
140
# File 'mod/01_history/lib/card/action.rb', line 138

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