Module: Historical::ActiveRecord

Defined in:
lib/historical/active_record.rb

Defined Under Namespace

Modules: Extensions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sql_to_type(type) ⇒ Object

converts database fieldtypes to Ruby types



4
5
6
7
8
9
10
11
12
13
# File 'lib/historical/active_record.rb', line 4

def self.sql_to_type(type)
  case type.to_sym
  when :datetime then "Time"
  when :text then "String"
  when :decimal then "Float"
  when :timestamp then "Time"
  else
    type.to_s.classify
  end
end

Instance Method Details

#is_historical(&block) ⇒ Object

Enables Historical in this model.

Examples:

simple

class Message < ActiveRecord::Base
  is_historical
end

advanced, with custom meta-data (auditing)

class Message < ActiveRecord::Base
  is_historical do
    meta do
      belongs_to_active_record :user
      key :cause, String
    end

    callback do |version|
      version.meta.cause  = "just because"
      version.meta.user   = App.current_user
    end
  end
end


137
138
139
140
141
142
143
144
145
# File 'lib/historical/active_record.rb', line 137

def is_historical(&block)
  include Historical::ActiveRecord::Extensions
  
  self.historical_installed         = true
  self.historical_customizations    ||= []
  self.historical_customizations    << block if block_given?
  
  Historical.historical_models      << self
end