Class: AuditedChangeSet::Change::Field

Inherits:
Object
  • Object
show all
Includes:
Hooks
Defined in:
lib/audited_change_set/change.rb

Constant Summary collapse

@@display_methods =
%w[field_name name to_s]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hooks

#hooks, included

Constructor Details

#initialize(change_type, name, new_val, old_val = nil) ⇒ Field

Returns a new instance of Field.



40
41
42
43
44
# File 'lib/audited_change_set/change.rb', line 40

def initialize(change_type, name, new_val, old_val=nil)
  @change_type = change_type
  @name = name.to_s
  @new_value, @old_value = [new_val, old_val].map {|val| transform_value(val) }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



38
39
40
# File 'lib/audited_change_set/change.rb', line 38

def name
  @name
end

#new_valueObject (readonly)

Returns the value of attribute new_value.



38
39
40
# File 'lib/audited_change_set/change.rb', line 38

def new_value
  @new_value
end

#old_valueObject (readonly)

Returns the value of attribute old_value.



38
39
40
# File 'lib/audited_change_set/change.rb', line 38

def old_value
  @old_value
end

Instance Method Details

#associated_value(val) ⇒ Object



50
51
52
53
# File 'lib/audited_change_set/change.rb', line 50

def associated_value(val)
  object = get_associated_object(val)
  object.send(display_method(object))
end

#association_classObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/audited_change_set/change.rb', line 55

def association_class
  @association_class ||= begin
    if reflection && class_name = reflection.options[:class_name]
      class_name.constantize
    else
      name.to_s =~ /(.*)_id$/
      $1.camelize.constantize
    end
  end
end

#association_field?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/audited_change_set/change.rb', line 75

def association_field?
  name.ends_with? "_id"
end

#get_associated_object(id) ⇒ Object



71
72
73
# File 'lib/audited_change_set/change.rb', line 71

def get_associated_object(id)
  hook(:get_associated_object, id) || association_class.find_by_id(id)
end

#name_without_idObject



79
80
81
# File 'lib/audited_change_set/change.rb', line 79

def name_without_id
  name.chomp "_id"
end

#reflectionObject



66
67
68
69
# File 'lib/audited_change_set/change.rb', line 66

def reflection
  change_class = @change_type.constantize
  @reflection ||= change_class.reflect_on_association(name_without_id.to_sym) if change_class.respond_to?(:reflect_on_association)
end

#transform_value(val) ⇒ Object



46
47
48
# File 'lib/audited_change_set/change.rb', line 46

def transform_value(val)
  hook(:transform_value, val) || (association_field? ? associated_value(val) : val.to_s)
end