Module: Schemer::ActiveRecord

Defined in:
lib/schemer/active_record.rb

Defined Under Namespace

Modules: ClassMethods Classes: Migrator

Instance Method Summary collapse

Instance Method Details

#schema(*args) ⇒ Object

Define a schema on your ActiveRecord model.

schema :name, { :age => :integer }, { :birthdate => :datetime }, :summary

Table + columns will be added automatically based on your definition.

If you remove a column, it will be removed from the table the next time the class is loaded.

If you change the type of a column, it will be also be updated the next time the class is loaded.

Likewise, adding a new column will add the column to your schema on the next class load.

Columns with no types are assumed to be strings.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/schemer/active_record.rb', line 21

def schema(*args)
  extend ClassMethods
  
  class_inheritable_accessor :schema_columns
  self.schema_columns = {}
  args.collect{ |a| a.is_a?(Hash) ? a.stringify_keys : { a.to_s => :string } }.each do |column|
    self.schema_columns.merge!(column)
  end

  update_schema
  update_methods
end