Class: Locomotive::ArrayInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Includes:
SimpleForm::BootstrapHelpers, SimpleForm::HeaderLink, SimpleForm::Inputs::FasterTranslate
Defined in:
app/inputs/locomotive/array_input.rb

Direct Known Subclasses

LocalesInput

Instance Method Summary collapse

Methods included from SimpleForm::Inputs::FasterTranslate

#translate_from_namespace

Methods included from SimpleForm::HeaderLink

#_header_link

Methods included from SimpleForm::BootstrapHelpers

#col_wrapping, #row_wrapping

Instance Method Details

#array_wrapperObject



12
13
14
15
16
17
18
# File 'app/inputs/locomotive/array_input.rb', line 12

def array_wrapper
  row_wrapping do
    template. :div,
      collection_to_html,
      class: array_wrapper_class
  end
end

#array_wrapper_classObject



20
21
22
23
24
25
# File 'app/inputs/locomotive/array_input.rb', line 20

def array_wrapper_class
  %w(col-md-12 list).tap do |wrapper_class|
    wrapper_class << 'hide' if collection.empty?
    wrapper_class << 'new-input-disabled' unless include_input_for_new_item?
  end.join(' ')
end

#collectionObject



42
43
44
# File 'app/inputs/locomotive/array_input.rb', line 42

def collection
  @collection ||= (options[:collection] || object.send(attribute_name.to_sym))
end

#collection_to_htmlObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/inputs/locomotive/array_input.rb', line 27

def collection_to_html
  _template = options[:template]
  key       = self.singularized_name

  path, locals = (if _template.respond_to?(:has_key?)
   [_template[:path].to_s, _template[:locals] || {}]
  else
    [_template.to_s, {}]
  end)

  collection.map do |item|
    template.render(path, locals.merge(key => item, item: item))
  end.join("\n").html_safe
end

#include_input_for_new_item?Boolean

Returns:



78
79
80
# File 'app/inputs/locomotive/array_input.rb', line 78

def include_input_for_new_item?
  options[:template_url].present?
end

#input(wrapper_options) ⇒ Object



8
9
10
# File 'app/inputs/locomotive/array_input.rb', line 8

def input(wrapper_options)
  array_wrapper + new_item_wrapper
end


46
47
48
# File 'app/inputs/locomotive/array_input.rb', line 46

def link(wrapper_options)
  _header_link(:new_item, :array_input)
end

#new_item_inputObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/inputs/locomotive/array_input.rb', line 63

def new_item_input
  css = 'form-control input'

  if options[:select_options]
    template.select_tag(singularized_name, template.options_for_select(options[:select_options]), class: css)
  elsif data = options[:picker]
    tag_id = 'locomotive-picker-' + (data.delete(:id) || 'id')
    template.select_tag(tag_id, '', class: css, data: data)
  else
    text_options = input_html_options.dup
    text_options[:class] << css
    template.text_field_tag(singularized_name, '', text_options)
  end
end

#new_item_wrapperObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/inputs/locomotive/array_input.rb', line 50

def new_item_wrapper
  return '' unless include_input_for_new_item?

  template. :div,
    template.(:div,
      new_item_input,
      class: 'field col-md-10 col-xs-9') +
    template.(:div,
      template.(:a, text(:add), class: 'btn btn-primary btn-sm add', href: options[:template_url]),
      class: 'button col-md-2 col-xs-3 text-right'),
    class: 'row new-field'
end

#singularized_nameObject



82
83
84
# File 'app/inputs/locomotive/array_input.rb', line 82

def singularized_name
  attribute_name.to_s.singularize.to_sym
end

#text(name) ⇒ Object



86
87
88
# File 'app/inputs/locomotive/array_input.rb', line 86

def text(name)
  I18n.t(name, scope: 'locomotive.inputs.array')
end