Class: Card::Action
- 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
- #action_type ⇒ Object
- #action_type=(value) ⇒ Object
- #card ⇒ Object
- #cardtype_diff(opts = {}) ⇒ Object
- #change_for(field) ⇒ Object
-
#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.
- #content_diff(diff_type = :expanded, opts = nil) ⇒ Object
- #content_diff_builder(opts = nil) ⇒ Object
- #edit_info ⇒ Object
- #expire ⇒ Object
- #field_index(field) ⇒ Object
- #green? ⇒ Boolean
- #last_value_for(field) ⇒ Object
-
#name_diff(opts = {}) ⇒ Object
def diff @diff ||= { cardtype: type_diff, content: content_diff, name: name_diff} end.
- #new_content? ⇒ Boolean
- #new_name? ⇒ Boolean
- #new_type? ⇒ Boolean
- #new_value_for(field) ⇒ Object
- #new_values ⇒ Object
- #old_values ⇒ Object
- #red? ⇒ Boolean
- #revision_nr ⇒ Object
- #set_act ⇒ Object
Class Method Details
.cache ⇒ Object
27 28 29 |
# File 'mod/01_history/lib/card/action.rb', line 27 def cache Card::Cache[Action] end |
.delete_cardless ⇒ Object
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_old ⇒ Object
42 43 44 45 46 47 |
# File 'mod/01_history/lib/card/action.rb', line 42 def delete_old Card.find_each do |card| card.delete_old_actions end Card::Act.delete_actionless end |
Instance Method Details
#action_type ⇒ Object
142 143 144 |
# File 'mod/01_history/lib/card/action.rb', line 142 def action_type TYPE[read_attribute(:action_type)] end |
#action_type=(value) ⇒ Object
138 139 140 |
# File 'mod/01_history/lib/card/action.rb', line 138 def action_type= value write_attribute :action_type, TYPE.index(value) end |
#card ⇒ Object
194 195 196 |
# File 'mod/01_history/lib/card/action.rb', line 194 def card Card.fetch card_id, look_in_trash: true end |
#cardtype_diff(opts = {}) ⇒ Object
171 172 173 174 |
# File 'mod/01_history/lib/card/action.rb', line 171 def cardtype_diff opts={} return unless new_type? Card::Diff.complete old_values[:cardtype], new_values[:cardtype], opts end |
#change_for(field) ⇒ Object
122 123 124 |
# File 'mod/01_history/lib/card/action.rb', line 122 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.
66 67 68 69 70 |
# File 'mod/01_history/lib/card/action.rb', line 66 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
176 177 178 179 180 181 182 183 |
# File 'mod/01_history/lib/card/action.rb', line 176 def content_diff diff_type=:expanded, opts=nil return unless new_content? if diff_type == :summary content_diff_builder(opts).summary else content_diff_builder(opts).complete end end |
#content_diff_builder(opts = nil) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'mod/01_history/lib/card/action.rb', line 185 def content_diff_builder opts=nil @content_diff_builder ||= begin diff_args = opts || card.include_set_modules.diff_args Card::Diff::DiffBuilder.new( old_values[:content], new_values[:content], diff_args ) end end |
#edit_info ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'mod/01_history/lib/card/action.rb', line 72 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 |
#expire ⇒ Object
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
109 110 111 112 113 114 115 |
# File 'mod/01_history/lib/card/action.rb', line 109 def field_index field if field.is_a? Integer field else Card::TRACKED_FIELDS.index(field.to_s) end end |
#green? ⇒ Boolean
158 159 160 |
# File 'mod/01_history/lib/card/action.rb', line 158 def green? content_diff_builder.green? end |
#last_value_for(field) ⇒ Object
104 105 106 107 |
# File 'mod/01_history/lib/card/action.rb', line 104 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
166 167 168 169 |
# File 'mod/01_history/lib/card/action.rb', line 166 def name_diff opts={} return unless new_name? Card::Diff.complete old_values[:name], new_values[:name], opts end |
#new_content? ⇒ Boolean
130 131 132 |
# File 'mod/01_history/lib/card/action.rb', line 130 def new_content? new_value_for :db_content end |
#new_name? ⇒ Boolean
134 135 136 |
# File 'mod/01_history/lib/card/action.rb', line 134 def new_name? new_value_for :name end |
#new_type? ⇒ Boolean
126 127 128 |
# File 'mod/01_history/lib/card/action.rb', line 126 def new_type? new_value_for :type_id end |
#new_value_for(field) ⇒ Object
117 118 119 120 |
# File 'mod/01_history/lib/card/action.rb', line 117 def new_value_for field ch = card_changes.find_by(field: field_index(field)) ch && ch.value end |
#new_values ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'mod/01_history/lib/card/action.rb', line 84 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_values ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'mod/01_history/lib/card/action.rb', line 94 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
154 155 156 |
# File 'mod/01_history/lib/card/action.rb', line 154 def red? content_diff_builder.red? end |
#revision_nr ⇒ Object
150 151 152 |
# File 'mod/01_history/lib/card/action.rb', line 150 def revision_nr card.actions.index_of self end |
#set_act ⇒ Object
146 147 148 |
# File 'mod/01_history/lib/card/action.rb', line 146 def set_act self.set_act ||= acts.last end |