Skip to content

Loading…

Inheritable attributes #33

Merged
merged 2 commits into from

2 participants

@mordaroso

With the commit 805046c my app failed because I had more than one formable model.
I changed a test case to prove it in this commit 3c58f79

Switching to inheritable class attributes solved the problem.
I'm not sure if this still solves issue #31.

@clayallsopp clayallsopp merged commit 62e4fe0 into master
@clayallsopp
Owner

looks good, merged

@clayallsopp
Owner

hmm actually this isn't working. i'll write a test case but it looks like inherited isn't being called right now

@clayallsopp
Owner

Yeah, this happens in the FormModel example:

(main)> User.class.form_title
=> "Edit User"
(main)> User.class
=> Class
(main)> Class.form_title
=> "Edit User"
@clayallsopp
Owner

I changed it to just use inherited and not do the multiple extending of modules, was getting confusing 07ec1d6

tests pass, but still doesnt fix #31 =\

@mordaroso

Looks way better. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 22, 2012
  1. @mordaroso

    Use class level inheritable attributes for form_properties and form_t…

    mordaroso committed
    …itle
    
    Solves clashing of class variables when having two formable classes
  2. @mordaroso
View
1 lib/formotion.rb
@@ -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
View
13 lib/formotion/model/formable.rb
@@ -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
@@ -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
View
23 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
View
9 spec/functional/formable_controller_spec.rb
@@ -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
Something went wrong with that request. Please try again.