Class: ToyModel::AttributeObserver
- Inherits:
-
Object
- Object
- ToyModel::AttributeObserver
- Defined in:
- lib/toy-model/attribute_observer.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
Instance Method Summary collapse
- #add_attribute(attribute) ⇒ Object
-
#initialize(model) ⇒ AttributeObserver
constructor
A new instance of AttributeObserver.
- #set_attribute(attribute) ⇒ Object
- #update_attribute(attribute) ⇒ Object
Constructor Details
#initialize(model) ⇒ AttributeObserver
Returns a new instance of AttributeObserver.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/toy-model/attribute_observer.rb', line 6 def initialize model return unless model.respond_to?(:use_toy_model?) && model.use_toy_model? @model = model unless ActiveRecord::Base.connection.table_exists? @model.table_name Class.new(ActiveRecord::Migration).create_table(@model.table_name.to_sym) do |t| t. end end @model.attributes.each do |attribute| set_attribute(attribute) @model.attr_accessible attribute.column @model.attr_accessible :"#{attribute.column}_id" if attribute._as == :belongs_to end @model.reset_column_information end |
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
4 5 6 |
# File 'lib/toy-model/attribute_observer.rb', line 4 def attribute @attribute end |
Instance Method Details
#add_attribute(attribute) ⇒ Object
28 29 30 31 32 |
# File 'lib/toy-model/attribute_observer.rb', line 28 def add_attribute attribute unless @model.column_names.include? attribute.to_table_column.to_s Class.new(ActiveRecord::Migration).add_column @model.table_name.to_sym, attribute.to_table_column, attribute.to_table_type end end |
#set_attribute(attribute) ⇒ Object
22 23 24 25 26 |
# File 'lib/toy-model/attribute_observer.rb', line 22 def set_attribute attribute return if attribute.skip_table_column? add_attribute attribute update_attribute attribute end |
#update_attribute(attribute) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/toy-model/attribute_observer.rb', line 34 def update_attribute attribute column = @model.columns.select{|c| c.name == attribute.to_table_column.to_s}.first if column && column.type != attribute.to_table_type Class.new(ActiveRecord::Migration).change_column @model.table_name, attribute.to_table_column, attribute.to_table_type end end |