Class: Para::Inputs::MultiSelectInput
- Inherits:
-
SimpleForm::Inputs::Base
- Object
- SimpleForm::Inputs::Base
- Para::Inputs::MultiSelectInput
- Includes:
- Helpers::ResourceName
- Defined in:
- lib/para/inputs/multi_select_input.rb
Instance Attribute Summary collapse
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #attribute_field ⇒ Object
- #foreign_key ⇒ Object
- #input(wrapper_options = nil) ⇒ Object
- #join_resources ⇒ Object
- #model ⇒ Object
- #option_resources ⇒ Object
- #orderable? ⇒ Boolean
- #orderable_association ⇒ Object
- #parent_model ⇒ Object
- #reflection ⇒ Object
- #resource_position(resource) ⇒ Object
Methods included from Helpers::ResourceName
Instance Attribute Details
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
6 7 8 |
# File 'lib/para/inputs/multi_select_input.rb', line 6 def resource @resource end |
Instance Method Details
#attribute_field ⇒ Object
53 54 55 |
# File 'lib/para/inputs/multi_select_input.rb', line 53 def attribute_field @attribute_field ||= AttributeField::HasManyField.new(object.class, name: attribute_name) end |
#foreign_key ⇒ Object
49 50 51 |
# File 'lib/para/inputs/multi_select_input.rb', line 49 def foreign_key :"#{ reflection.name.to_s.singularize }_ids" end |
#input(wrapper_options = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/para/inputs/multi_select_input.rb', line 8 def input( = nil) [:class] << "multi-select" # Load existing resources resources = object.send(attribute_name) # Order them if the list should be orderable resources = resources.sort_by(&method(:resource_position)) if orderable? template.render( partial: 'para/inputs/multi_select', locals: { form: @builder, model: model, attribute_name: foreign_key, orderable: orderable?, resources: resources, option_resources: option_resources } ) end |
#join_resources ⇒ Object
85 86 87 |
# File 'lib/para/inputs/multi_select_input.rb', line 85 def join_resources @join_resources ||= orderable_association.load_target end |
#model ⇒ Object
41 42 43 |
# File 'lib/para/inputs/multi_select_input.rb', line 41 def model @model ||= reflection.klass end |
#option_resources ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/para/inputs/multi_select_input.rb', line 29 def option_resources @option_resources ||= if model.orderable? model.ordered else model.all.sort_by { |resource| resource_name(resource) } end end |
#orderable? ⇒ Boolean
57 58 59 60 61 62 63 64 65 |
# File 'lib/para/inputs/multi_select_input.rb', line 57 def orderable? @orderable ||= .fetch(:orderable) do if attribute_field.through_reflection attribute_field.through_reflection.klass.orderable? else model.orderable? end end end |
#orderable_association ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/para/inputs/multi_select_input.rb', line 77 def orderable_association @orderable_association ||= if attribute_field.through_reflection object.association(attribute_field.through_reflection.name) else object.association(attribute_name) end end |
#parent_model ⇒ Object
37 38 39 |
# File 'lib/para/inputs/multi_select_input.rb', line 37 def parent_model @parent_model ||= @builder.object.class end |
#reflection ⇒ Object
45 46 47 |
# File 'lib/para/inputs/multi_select_input.rb', line 45 def reflection @reflection ||= parent_model.reflect_on_association(attribute_name) end |
#resource_position(resource) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/para/inputs/multi_select_input.rb', line 67 def resource_position(resource) if attribute_field.through_reflection resource = join_resources.find do |res| res.send(attribute_field.through_relation_source_foreign_key) == resource.id end end resource.position end |