Module: Schemer::ClassMethods
- Defined in:
- lib/schemer.rb
Instance Method Summary collapse
-
#create_table ⇒ Object
Create the underlying table for this class.
-
#protected_columns ⇒ Object
Columns which we don’t touch regardless of not being defined in schema (and are ignored if they are defined in schema).
-
#update_methods ⇒ Object
Update ActiveRecord’s automatically generated methods so we don’t have to reload for schema changes to take effect.
-
#update_schema ⇒ Object
Update the underlying schema as defined by schema call.
Instance Method Details
#create_table ⇒ Object
Create the underlying table for this class
25 26 27 |
# File 'lib/schemer.rb', line 25 def create_table ActiveRecord::Migration.create_table(table_name) do |t|;end; end |
#protected_columns ⇒ Object
Columns which we don’t touch regardless of not being defined in schema (and are ignored if they are defined in schema)
20 21 22 |
# File 'lib/schemer.rb', line 20 def protected_columns %w( id ) end |
#update_methods ⇒ Object
Update ActiveRecord’s automatically generated methods so we don’t have to reload for schema changes to take effect
31 32 33 34 |
# File 'lib/schemer.rb', line 31 def update_methods generated_methods.each { |method| remove_method(method) } @columns = @column_names = @columns_hash = @generated_methods = nil end |
#update_schema ⇒ Object
Update the underlying schema as defined by schema call
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/schemer.rb', line 37 def update_schema create_table unless table_exists? columns.reject{ |column| protected_columns.include?(column.name) }.each do |column| if !schema_columns.has_key?(column.name) # remove any extraneous columns ActiveRecord::Migration.remove_column(table_name, column.name) elsif column.type != schema_columns[column.name] # change any columns w/ wrong type ActiveRecord::Migration.change_column(table_name, column.name, schema_columns[column.name]) end end # add any missing columns (schema_columns.keys - column_names).each do |column| ActiveRecord::Migration.add_column(table_name, column, schema_columns[column]) end end |