Module: RailsStuff::ResourcesController::StiHelpers::ClassMethods
- Defined in:
- lib/rails_stuff/resources_controller/sti_helpers.rb
Instance Attribute Summary collapse
-
#resource_class_by_type ⇒ Object
Returns hash which is used to get subclass for requested type.
-
#use_resource_class_for_invalid_type ⇒ Object
Returns the value of attribute use_resource_class_for_invalid_type.
Instance Method Summary collapse
-
#permit_attrs_for(klass, *attrs) ⇒ Object
Permits attrs only for specific class.
-
#permitted_attrs_for ⇒ Object
Class-level accessor to permitted attributes for specisic class.
- #resource_class_for(name) ⇒ Object
Instance Attribute Details
#resource_class_by_type ⇒ Object
Returns hash which is used to get subclass for requested type.
By default it uses ‘.types_list` or `.descendants` to get list of classes and indexes them by class names.
16 17 18 19 20 21 22 23 |
# File 'lib/rails_stuff/resources_controller/sti_helpers.rb', line 16 def resource_class_by_type @resource_class_by_type ||= if resource_class.respond_to?(:types_list) resource_class.types_list else resource_class.descendants end.index_by(&:name) end |
#use_resource_class_for_invalid_type ⇒ Object
Returns the value of attribute use_resource_class_for_invalid_type.
37 38 39 |
# File 'lib/rails_stuff/resources_controller/sti_helpers.rb', line 37 def use_resource_class_for_invalid_type @use_resource_class_for_invalid_type end |
Instance Method Details
#permit_attrs_for(klass, *attrs) ⇒ Object
Permits attrs only for specific class.
45 46 47 |
# File 'lib/rails_stuff/resources_controller/sti_helpers.rb', line 45 def permit_attrs_for(klass, *attrs) permitted_attrs_for[klass].concat attrs end |
#permitted_attrs_for ⇒ Object
Class-level accessor to permitted attributes for specisic class.
40 41 42 |
# File 'lib/rails_stuff/resources_controller/sti_helpers.rb', line 40 def permitted_attrs_for @permitted_attrs_for ||= Hash.new { |h, k| h[k] = [] } end |
#resource_class_for(name) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/rails_stuff/resources_controller/sti_helpers.rb', line 27 def resource_class_for(name) return resource_class unless name resource_class_by_type[name] || if use_resource_class_for_invalid_type resource_class else raise(InvalidType, "No type mapped for #{name.inspect}") end end |