Class: Card::Change

Inherits:
Cardio::Record show all
Defined in:
mod/history/lib/card/change.rb

Overview

A change is an alteration to a card’s name, type, content, or trash state. 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 card, that would be recorded as one action with two changes.

A Change records:

  • the field changed
  • the new value of that field
  • the action of which the change is part

Constant Summary collapse

TRACKED_FIELDS =

lists the database fields for which changes are recorded

%w[name type_id db_content trash left_id right_id].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_actionlessObject

delete all changes not associated with an action (janitorial)



30
31
32
33
34
35
36
37
38
39
40
41
# File 'mod/history/lib/card/change.rb', line 30

def delete_actionless
  joins(
    "LEFT JOIN card_actions "\
    "ON card_changes.card_action_id = card_actions.id "
  ).where(
    "card_actions.id is null"
  ).pluck_in_batches(:id) do |group_ids|
    # used to be .delete_all here, but that was failing on large dbs
    Rails.logger.info "deleting batch of changes"
    where("id in (#{group_ids.join ','})").delete_all
  end
end

.field_index(value) ⇒ Integer

Change fields are recorded as integers. #field_index looks up the integer associated with a given field name.

Parameters:

  • value (String, Symbol)

Returns:

  • (Integer)


47
48
49
# File 'mod/history/lib/card/change.rb', line 47

def field_index value
  value.is_a?(Integer) ? value : TRACKED_FIELDS.index(value.to_s)
end

.find_by_field_name(value) ⇒ Change

look up changes based on field name

Parameters:

  • value (String, Symbol)

Returns:



54
55
56
# File 'mod/history/lib/card/change.rb', line 54

def find_by_field_name value
  find_by_field field_index(value)
end

Instance Method Details

#fieldString

retrieve field name

Returns:

  • (String)


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

def field
  TRACKED_FIELDS[read_attribute(:field)]
end

#field=(value) ⇒ Object

set field value (integer)

Parameters:

  • value (String, Symbol)


61
62
63
# File 'mod/history/lib/card/change.rb', line 61

def field= value
  write_attribute(:field, TRACKED_FIELDS.index(value.to_s))
end