Skip to content

Commit

Permalink
Merge pull request #33 from clayallsopp/inheritable_attributes
Browse files Browse the repository at this point in the history
Inheritable attributes
  • Loading branch information
clayallsopp committed Aug 27, 2012
2 parents 295f72d + 3c58f79 commit 62e4fe0
Show file tree
Hide file tree
Showing 4 changed files with 40 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
9 changes: 9 additions & 0 deletions spec/functional/formable_controller_spec.rb
Expand Up @@ -7,6 +7,15 @@ class TestModel
form_property :my_number, :number, title: "My Number", :transform => lambda { |value| value.to_i + 10 }
end

class AdditionalTestModel
include Formotion::Formable

attr_accessor :my_other_name, :my_other_number

form_property :my_other_name, :string
form_property :my_other_number, :number
end

describe "FormableController" do
tests Formotion::FormableController

Expand Down

0 comments on commit 62e4fe0

Please sign in to comment.