Module: Trackzor::InstanceMethods

Defined in:
lib/trackzor.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#merge_with(other, unique_id_col = 'id') ⇒ Object

merge record with another, accepting the latest values available



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/trackzor.rb', line 106

def merge_with(other, unique_id_col = 'id')
  raise "unmergable objects" if other.class.column_names != self.class.column_names || self.send(unique_id_col.to_sym) != other.send(unique_id_col.to_sym)

  column_names = self.class.column_names

  self.trackzored_columns.each do |tc|
    has_updated_by_col = column_names.include?("#{tc}_updated_by")
    has_updated_at_col = column_names.include?("#{tc}_updated_at")
    
    if has_updated_at_col
      self_time = self.send("#{tc}_updated_at".to_sym)
      other_time = other.send("#{tc}_updated_at".to_sym)
    else
      self_time = self.updated_at
      other_time = other.updated_at
    end

    if self_time.nil? || (!other_time.nil? && other_time > self_time)
      self.send("#{tc}_updated_at=".to_sym, other_time) if has_updated_at_col
      self.send("#{tc}_updated_by=".to_sym, other.send("#{tc}_updated_by".to_sym)) if has_updated_by_col
      self.send("#{tc}=".to_sym, other.send(tc.to_sym))
    end
  end

  if other.updated_at > self.updated_at
    (column_names - self.trackzored_columns - self.trackzor_maintained_columns).each do |c|
      self.send("#{c}=".to_sym, other.send(c.to_sym))
    end
  end

  puts "Merged #{self.send(unique_id_col.to_sym)}: #{self.changes.inspect}" unless self.changes.empty?
  self.send(:update_without_callbacks)
end

#trackzor_assign_and_validateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/trackzor.rb', line 56

def trackzor_assign_and_validate
  user = Thread.current[:trackzor_user] || Thread.current[:acts_as_audited_user]

  self.changes.keys.each do |attr|
    unless self.trackzor_exempt_columns.include?(attr)
      time_column = "#{attr}_updated_at"
      user_association = "#{attr}_source"

      if self.respond_to?(time_column.to_sym)
        self.send("#{time_column}=".to_sym, Time.now)
      end

      if self.respond_to?(user_association.to_sym)
        if user
          self.send("#{user_association}=".to_sym, user)
        else
          self.errors.add("#{attr}_updated_by", "requires Trackzor user to be set")
        end
      end
    end
  end
end

#will_update_attributes!(new_attributes, guard_protected_attributes = true) ⇒ Object

update multiple attributes and force update of trackzored attributes



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/trackzor.rb', line 80

def will_update_attributes!(new_attributes, guard_protected_attributes = true)
  return if new_attributes.nil?
  attributes = new_attributes.dup
  attributes.stringify_keys!

  multi_parameter_attributes = []
  attributes = remove_attributes_protected_from_mass_assignment(attributes) if guard_protected_attributes

  attributes.each do |k, v|
    if k.include?("(")
      multi_parameter_attributes << [ k, v ]
    else
      if respond_to?("#{k}=")
        send("#{k}=", v)
        send("#{k}_will_change!") if self.trackzored_columns.include?(k)
      else
        raise "unknown attribute: #{k}"
      end
    end
  end

  assign_multiparameter_attributes(multi_parameter_attributes)
  save!
end