Class: Para::Inputs::MultiSelectInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Includes:
Helpers::ResourceName, ModelHelper, SearchHelper
Defined in:
lib/para/inputs/multi_select_input.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SearchHelper

#distinct_search_results, #filtered?, #fulltext_search_param_for, #searchable_attributes

Methods included from ModelHelper

#attribute_field_mappings_for, #excerpt_value_for, #field_for, #field_value_for, #model_field_mappings, #relation_klass_for, #value_for

Methods included from Helpers::ResourceName

#resource_name

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



8
9
10
# File 'lib/para/inputs/multi_select_input.rb', line 8

def resource
  @resource
end

Instance Method Details

#attribute_fieldObject



57
58
59
60
61
# File 'lib/para/inputs/multi_select_input.rb', line 57

def attribute_field
  @attribute_field ||= AttributeField::HasManyField.new(
    object.class, name: attribute_name
  )
end

#collectionObject



70
71
72
73
74
# File 'lib/para/inputs/multi_select_input.rb', line 70

def collection
  @collection ||= options.fetch(:collection) do
    model.all
  end
end

#foreign_keyObject



53
54
55
# File 'lib/para/inputs/multi_select_input.rb', line 53

def foreign_key
  :"#{ reflection.name.to_s.singularize }_ids"
end

#input(wrapper_options = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/para/inputs/multi_select_input.rb', line 10

def input(wrapper_options = nil)
  input_html_options[:class] << "multi-select"

  # Load existing resources
  resources = input_html_options[:value] || 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,
      search_param: search_param,
      collection: collection
    }
  )
end

#join_resourcesObject



106
107
108
# File 'lib/para/inputs/multi_select_input.rb', line 106

def join_resources
  @join_resources ||= orderable_association.load_target
end

#modelObject



45
46
47
# File 'lib/para/inputs/multi_select_input.rb', line 45

def model
  @model ||= reflection.klass
end

#option_resourcesObject



33
34
35
36
37
38
39
# File 'lib/para/inputs/multi_select_input.rb', line 33

def option_resources
  @option_resources ||= if model.orderable?
    collection.ordered
  else
    collection.sort_by { |resource| resource_name(resource) }
  end
end

#orderable?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
# File 'lib/para/inputs/multi_select_input.rb', line 76

def orderable?
  @orderable ||= options.fetch(:orderable) do
    if attribute_field.through_reflection
      attribute_field.through_reflection.klass.orderable?
    else
      model.orderable?
    end
  end
end

#orderable_associationObject



98
99
100
101
102
103
104
# File 'lib/para/inputs/multi_select_input.rb', line 98

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_modelObject



41
42
43
# File 'lib/para/inputs/multi_select_input.rb', line 41

def parent_model
  @parent_model ||= @builder.object.class
end

#reflectionObject



49
50
51
# File 'lib/para/inputs/multi_select_input.rb', line 49

def reflection
  @reflection ||= parent_model.reflect_on_association(attribute_name)
end

#resource_position(resource) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/para/inputs/multi_select_input.rb', line 86

def resource_position(resource)
  existing_resource = if attribute_field.through_reflection
    join_resources.find do |res|
      res.send(attribute_field.through_relation_source_foreign_key) == resource.id
    end
  else
    resource
  end

  existing_resource.position
end

#search_paramObject



63
64
65
66
67
68
# File 'lib/para/inputs/multi_select_input.rb', line 63

def search_param
  @search_param ||= options.fetch(:search_param) do
    attributes = model_field_mappings(model).fields
    fulltext_search_param_for(attributes)
  end
end