Class: Locomotive::ArrayInput
Instance Method Summary
collapse
#translate_from_namespace
#_header_link
#col_wrapping, #row_wrapping
Instance Method Details
#array_wrapper ⇒ Object
12
13
14
15
16
17
18
|
# File 'app/inputs/locomotive/array_input.rb', line 12
def array_wrapper
row_wrapping do
template.content_tag :div,
collection_to_html,
class: array_wrapper_class
end
end
|
#array_wrapper_class ⇒ Object
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
|
#collection ⇒ Object
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_html ⇒ Object
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
|
78
79
80
|
# File 'app/inputs/locomotive/array_input.rb', line 78
def include_input_for_new_item?
options[:template_url].present?
end
|
8
9
10
|
# File 'app/inputs/locomotive/array_input.rb', line 8
def input(wrapper_options)
array_wrapper + new_item_wrapper
end
|
#link(wrapper_options) ⇒ Object
46
47
48
|
# File 'app/inputs/locomotive/array_input.rb', line 46
def link(wrapper_options)
(:new_item, :array_input)
end
|
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_wrapper ⇒ Object
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.content_tag :div,
template.content_tag(:div,
new_item_input,
class: 'field col-md-10 col-xs-9') +
template.content_tag(:div,
template.content_tag(: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_name ⇒ Object
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
|