Class: Card::Action
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Card::Action
- Extended by:
- Admin
- Includes:
- Differ
- Defined in:
- mod/history/lib/card/action.rb,
mod/history/lib/card/action/admin.rb,
mod/history/lib/card/action/differ.rb
Overview
An action is a group of changes to a single card that is recorded during an act. Together, acts, actions, and changes comprise a comprehensive card history tracking system.
For example, if a given web submission changes both the name and type of a given card, that would be recorded as one action with two changes. If there are multiple cards changed, each card would have its own action, but the whole submission would still comprise just one single act.
An Action records:
Defined Under Namespace
Constant Summary collapse
- TYPE_OPTIONS =
these are the three possible values for action_type
[:create, :update, :delete].freeze
Class Method Summary collapse
-
.cache ⇒ Card::Cache
cache object for actions.
-
.fetch(id) ⇒ Action?
retrieve action from cache if available.
Instance Method Summary collapse
-
#action_type ⇒ Symbol
retrieve action_type (create, update, or delete).
-
#action_type=(value) ⇒ Integer
assign action_type (create, update, or delete).
-
#card ⇒ Card
each action is associated with on and only one card.
-
#change(field) ⇒ Change
action's Change object for given field.
-
#changes ⇒ Hash
all action changes in hash form.
-
#expire ⇒ Object
remove action from action cache.
-
#interpret_field(field) ⇒ Symbol
translate field into fieldname as referred to in database.
-
#interpret_value(field, value) ⇒ Integer, ...
value in form prescribed for specific field name.
-
#new_content? ⇒ true/false
does action change card's content?.
-
#new_name? ⇒ true/false
does action change card's name?.
-
#new_type? ⇒ true/false
does action change card's type?.
-
#previous_change(field) ⇒ Change
most recent change to given field before this one.
-
#previous_value(field) ⇒ Object
value of field set by most recent Change before this one.
-
#value(field) ⇒ Object
value set by action's Change to given field.
Methods included from Admin
delete_cardless, delete_changeless, delete_old
Methods included from Differ
#cardtype_diff, #content_diff, #green?, #name_diff, #red?
Class Method Details
.cache ⇒ Card::Cache
cache object for actions
56 57 58 |
# File 'mod/history/lib/card/action.rb', line 56 def cache Card::Cache[Action] end |
.fetch(id) ⇒ Action?
retrieve action from cache if available
48 49 50 51 52 |
# File 'mod/history/lib/card/action.rb', line 48 def fetch id cache.fetch id.to_s do find id.to_i end end |
Instance Method Details
#action_type ⇒ Symbol
retrieve action_type (create, update, or delete)
81 82 83 |
# File 'mod/history/lib/card/action.rb', line 81 def action_type TYPE_OPTIONS[read_attribute(:action_type)] end |
#action_type=(value) ⇒ Integer
assign action_type (create, update, or delete)
75 76 77 |
# File 'mod/history/lib/card/action.rb', line 75 def action_type= value write_attribute :action_type, TYPE_OPTIONS.index(value) end |
#card ⇒ Card
each action is associated with on and only one card
63 64 65 |
# File 'mod/history/lib/card/action.rb', line 63 def card Card.fetch card_id, look_in_trash: true, skip_modules: true end |
#change(field) ⇒ Change
action's Change object for given field
105 106 107 |
# File 'mod/history/lib/card/action.rb', line 105 def change field changes[interpret_field field] end |
#changes ⇒ Hash
all action changes in hash form. { field1: Change1 }
124 125 126 127 128 129 |
# File 'mod/history/lib/card/action.rb', line 124 def changes @changes ||= card_changes.each_with_object({}) do |change, hash| hash[change.field.to_sym] = change end end |
#expire ⇒ Object
remove action from action cache
68 69 70 |
# File 'mod/history/lib/card/action.rb', line 68 def expire self.class.cache.delete id.to_s end |
#interpret_field(field) ⇒ Symbol
translate field into fieldname as referred to in database
154 155 156 157 158 159 160 |
# File 'mod/history/lib/card/action.rb', line 154 def interpret_field field case field when :content then :db_content when :cardtype then :type_id else field.to_sym end end |
#interpret_value(field, value) ⇒ Integer, ...
value in form prescribed for specific field name
167 168 169 170 171 172 173 174 175 176 |
# File 'mod/history/lib/card/action.rb', line 167 def interpret_value field, value case field.to_sym when :type_id value && value.to_i when :cardtype type_card = value && Card.quick_fetch(value.to_i) type_card && type_card.name.capitalize else value end end |
#new_content? ⇒ true/false
does action change card's content?
139 140 141 |
# File 'mod/history/lib/card/action.rb', line 139 def new_content? !value(:db_content).nil? end |
#new_name? ⇒ true/false
does action change card's name?
145 146 147 |
# File 'mod/history/lib/card/action.rb', line 145 def new_name? !value(:name).nil? end |
#new_type? ⇒ true/false
does action change card's type?
133 134 135 |
# File 'mod/history/lib/card/action.rb', line 133 def new_type? !value(:type_id).nil? end |
#previous_change(field) ⇒ Change
most recent change to given field before this one
112 113 114 115 116 117 118 119 120 |
# File 'mod/history/lib/card/action.rb', line 112 def previous_change field field = interpret_field field if @previous_changes && @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
96 97 98 99 100 |
# File 'mod/history/lib/card/action.rb', line 96 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
88 89 90 91 |
# File 'mod/history/lib/card/action.rb', line 88 def value field return unless (change = change field) interpret_value field, change.value end |