Sha256: e4b52fda2f0e3151bf15665f1f10c90e8797528b7ad0baadc9aa2a8a8429ccbe

Contents?: true

Size: 1.26 KB

Versions: 26

Compression:

Stored size: 1.26 KB

Contents

class DirtyHistoryRecord < ActiveRecord::Base   
  belongs_to :creator,  :polymorphic => true
  belongs_to :object,   :polymorphic => true

  validates_presence_of :old_value,   :unless => proc { |record| record.object.initialize_dirty_history }
  validates_presence_of :object_type, :object_id, :column_name, :column_type, :new_value

  scope :created_by,      lambda { |creator| where(:creator_id => creator.id, :creator_type => creator.class.name) }
  scope :for_object_type, lambda { |object_type| where(:object_type => object_type.to_s.classify) }
  
  acts_as_paranoid
    
  [:new_value, :old_value].each do |attribute|
    define_method "#{attribute}" do 
      val_to_col_type(attribute)
    end
    define_method "#{attribute}=" do |val|
      self[attribute] = val.nil? ? nil : val.to_s
      instance_variable_set "@#{attribute}", val
    end
  end       
    
  private
  
  def val_to_col_type attribute
    val_as_string = self[attribute]
    return nil if val_as_string.nil?
    case self[:column_type].to_sym
    when :integer, :boolean
      val_as_string.to_i
    when :decimal, :float
      val_as_string.to_f
    when :datetime
      Time.parse val_as_string
    when :date
      Date.parse val_as_string
    else # :string, :text
      val_as_string
    end
  end
  
end

Version data entries

26 entries across 13 versions & 1 rubygems

Version Path
dirty_history-0.5.2 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.5.2 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.5.1 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.5.1 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.5.0 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.5.0 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.10 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.10 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.9 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.9 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.8 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.8 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.7 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.7 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.6 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.6 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.5 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.5 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.4 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb
dirty_history-0.4.4 dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/dirty_history/ruby/1.9.1/gems/dirty_history-0.2.0/lib/dirty_history/dirty_history_record.rb