Class: Card::Change

Inherits:
ActiveRecord::Base
  • Object
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).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_actionlessObject

delete all changes not associated with an action (janitorial)



27
28
29
30
31
32
33
34
35
36
37
38
# File 'mod/history/lib/card/change.rb', line 27

def delete_actionless
  joins(
    "LEFT JOIN card_actions "\
    "ON card_changes.card_action_id = card_actions.id "
  ).where(
    "card_actions.id is null"
  ).find_in_batches do |group|
    # used to be .delete_all here, but that was failing on large dbs
    puts "deleting batch of changes"
    where("id in (#{group.map(&:id).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)


44
45
46
# File 'mod/history/lib/card/change.rb', line 44

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:



51
52
53
# File 'mod/history/lib/card/change.rb', line 51

def find_by_field_name value
  find_by_field field_index(value)
end

Instance Method Details

#fieldString

retrieve field name

Returns:

  • (String)


64
65
66
# File 'mod/history/lib/card/change.rb', line 64

def field
  TRACKED_FIELDS[read_attribute(:field)]
end

#field=(value) ⇒ Object

set field value (integer)

Parameters:

  • value (String, Symbol)


58
59
60
# File 'mod/history/lib/card/change.rb', line 58

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