Skip to content

Commit

Permalink
Use class level inheritable attributes for form_properties and form_t…
Browse files Browse the repository at this point in the history
…itle

Solves clashing of class variables when having two formable classes
  • Loading branch information
mordaroso committed Aug 22, 2012
1 parent 295f72d commit 042bfcd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/formotion.rb
Expand Up @@ -25,4 +25,5 @@
file("lib/formotion/#{file}").depends_on 'lib/formotion/base.rb'
}
file("lib/formotion/controller/form_controller.rb").depends_on 'lib/formotion/patch/ui_text_field.rb'
file("lib/formotion/model/formable.rb").depends_on 'lib/formotion/patch/class_level_inheritable_attributes.rb'
end
13 changes: 7 additions & 6 deletions lib/formotion/model/formable.rb
Expand Up @@ -2,9 +2,14 @@ module Formotion
module Formable
def self.included(base)
base.extend(ClassMethods)
base.form_properties = []
end

module ClassMethods
include ClassLevelInheritableAttributes
inheritable_attributes :form_properties
inheritable_attributes :form_title

# Relates a property to a RowType.
# @param property is the name of the attribute to KVO
# @param row_type is the Formotion::RowType to use for that attribute
Expand All @@ -19,16 +24,12 @@ def form_property(property, row_type, options = {})
self.form_properties << { property: property, row_type: row_type}.merge(options)
end

def form_properties
@@form_properties ||= []
end

# Sets the top bar title for this model
# EX
# form_title "Some Settings"
def form_title(title = -1)
@@form_title = title if title != -1
@@form_title
self.class.form_title = title if title != -1
self.class.form_title
end
end

Expand Down
23 changes: 23 additions & 0 deletions lib/formotion/patch/class_level_inheritable_attributes.rb
@@ -0,0 +1,23 @@
module ClassLevelInheritableAttributes
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def inheritable_attributes(*args)
@inheritable_attributes ||= [:inheritable_attributes]
@inheritable_attributes += args
args.each do |arg|
self.class.send(:attr_accessor, arg.to_sym)
end
@inheritable_attributes
end

def inherited(subclass)
@inheritable_attributes.each do |inheritable_attribute|
instance_var = "@#{inheritable_attribute}"
subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
end
end
end
end

0 comments on commit 042bfcd

Please sign in to comment.