Class: Madmin::Field::Polymorphic
- Inherits:
-
Madmin::Field
- Object
- Madmin::Field
- Madmin::Field::Polymorphic
- Defined in:
- lib/madmin/field/polymorphic.rb
Overview
This field represents a polymorphic relationship.
Instance Attribute Summary collapse
-
#polymorphic_display_value ⇒ Object
readonly
Returns the value of attribute polymorphic_display_value.
-
#polymorphic_scope ⇒ Object
readonly
Returns the value of attribute polymorphic_scope.
Attributes inherited from Madmin::Field
#form, #index, #key, #label, #model, #options, #show, #type
Instance Method Summary collapse
-
#initialize(args) ⇒ Polymorphic
constructor
A new instance of Polymorphic.
-
#polymorphic_id_for(resource) ⇒ Object
Returns the id for the associated polymorphic record.
-
#polymorphic_id_param ⇒ Object
Returns the polymorphic ID param.
- #polymorphic_relationship_exists?(resource) ⇒ Boolean
-
#polymorphic_slug_for(resource) ⇒ Object
Returns the slug for the polymorphic resource.
-
#polymorphic_type_param ⇒ Object
Returns the polymorphic type param.
-
#polymorphic_value_for(resource) ⇒ Object
Returns the value to show in the admin for the polymorphic relationship.
-
#strong_params_keys ⇒ Object
Returns the polymorphic stront params keys.
Methods inherited from Madmin::Field
Constructor Details
#initialize(args) ⇒ Polymorphic
9 10 11 12 13 |
# File 'lib/madmin/field/polymorphic.rb', line 9 def initialize(args) super(args) @polymorphic_display_value = option_or_default(:display_value, :name) @polymorphic_scope = :all end |
Instance Attribute Details
#polymorphic_display_value ⇒ Object (readonly)
Returns the value of attribute polymorphic_display_value.
6 7 8 |
# File 'lib/madmin/field/polymorphic.rb', line 6 def polymorphic_display_value @polymorphic_display_value end |
#polymorphic_scope ⇒ Object (readonly)
Returns the value of attribute polymorphic_scope.
7 8 9 |
# File 'lib/madmin/field/polymorphic.rb', line 7 def polymorphic_scope @polymorphic_scope end |
Instance Method Details
#polymorphic_id_for(resource) ⇒ Object
Returns the id for the associated polymorphic record.
17 18 19 |
# File 'lib/madmin/field/polymorphic.rb', line 17 def polymorphic_id_for(resource) value_for(resource).id end |
#polymorphic_id_param ⇒ Object
Returns the polymorphic ID param.
23 24 25 |
# File 'lib/madmin/field/polymorphic.rb', line 23 def polymorphic_id_param "#{key}_id".to_sym end |
#polymorphic_relationship_exists?(resource) ⇒ Boolean
27 28 29 |
# File 'lib/madmin/field/polymorphic.rb', line 27 def polymorphic_relationship_exists?(resource) resource.send(polymorphic_type_param) && resource.send(polymorphic_id_param) end |
#polymorphic_slug_for(resource) ⇒ Object
Returns the slug for the polymorphic resource.
33 34 35 |
# File 'lib/madmin/field/polymorphic.rb', line 33 def polymorphic_slug_for(resource) Object.const_get("::Madmin::Resources::#{resource.send("#{key}_type")}").new.slug end |
#polymorphic_type_param ⇒ Object
Returns the polymorphic type param.
39 40 41 |
# File 'lib/madmin/field/polymorphic.rb', line 39 def polymorphic_type_param "#{key}_type".to_sym end |
#polymorphic_value_for(resource) ⇒ Object
Returns the value to show in the admin for the polymorphic relationship.
46 47 48 |
# File 'lib/madmin/field/polymorphic.rb', line 46 def polymorphic_value_for(resource) value_for(resource).send(polymorphic_display_value) end |
#strong_params_keys ⇒ Object
Returns the polymorphic stront params keys.
52 53 54 |
# File 'lib/madmin/field/polymorphic.rb', line 52 def strong_params_keys [polymorphic_id_param, polymorphic_type_param] end |