-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathsimple_form.rb
More file actions
192 lines (155 loc) · 5.48 KB
/
simple_form.rb
File metadata and controls
192 lines (155 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
module Locomotive
module SimpleForm
module BootstrapHelpers
def row_wrapping(options = {}, &block)
options.merge!(class: 'row')
template.content_tag(:div,
template.capture(&block).html_safe,
options)
end
def col_wrapping(class_name, column = 6, &block)
template.content_tag(:div,
template.capture(&block).html_safe,
class: "col-md-#{column} #{class_name}")
end
end
module HeaderLink
def _header_link(name, type)
if _options = options[name]
label = _options[:label] || I18n.t(:edit, scope: ['locomotive.shared.form', type])
url = _options[:url]
template.link_to(label, url, class: 'action')
else
''
end
end
end
module Inputs
module FasterTranslate
def translate_from_namespace(namespace, default = '')
if (model_names = lookup_model_names)[0] == 'locomotive'
model_name = model_names.join('.')
_key = [
Locomotive::VERSION,
I18n.locale,
template.instance_variable_get(:"@virtual_path"),
namespace,
model_name,
reflection_or_attribute_name].join('/')
Rails.cache.fetch(_key) do
lookups = [:"#{model_name}.#{lookup_action}.#{reflection_or_attribute_name}"]
lookups << :"#{model_name}.#{reflection_or_attribute_name}"
lookups << default
t(lookups.shift, scope: :"#{i18n_scope}.#{namespace}", default: lookups).presence
end
else
super
end
end
end
::SimpleForm::FormBuilder.mappings.values.uniq.each do |klass|
klass.send(:include, FasterTranslate)
end
end
end
class FormBuilder < ::SimpleForm::FormBuilder
def inputs(name = nil, options = {}, &block)
html = template.content_tag(:legend, template.content_tag(:span, name))
html += template.capture(&block)
options[:class] ||= 'inputs'
template.content_tag(:fieldset, html, options)
end
def actions(options = {}, &block)
if options[:clone_url] && options[:back_url]
actions_with_back_clone_button(options)
elsif options[:clone_url]
actions_with_clone_button(options)
elsif options[:back_url]
actions_with_back_button(options)
else
options[:class] ||= 'text-right form-actions'
template.content_tag(:div, options, &block)
end
end
def actions_with_back_clone_button(options = {})
clone_button = clone_button_action(options)
back_button = back_button_action(options)
template.content_tag(:div, action +
' '.html_safe +
translate_button(:or) +
' '.html_safe +
back_button +
' '.html_safe +
translate_button(:or) +
' '.html_safe +
clone_button, class: 'text-right form-actions')
end
def actions_with_clone_button(options = {})
clone_button = clone_button_action(options)
template.content_tag(:div, action +
' '.html_safe +
translate_button(:or) +
' '.html_safe +
clone_button, class: 'text-right form-actions')
end
def actions_with_back_button(options = {})
back_button = back_button_action(options)
template.content_tag(:div, action +
' '.html_safe +
translate_button(:or) +
' '.html_safe +
back_button, class: 'text-right form-actions')
end
def back_button_action(options = {})
label = translate_button(:cancel)
url = options[:back_url]
if options[:use_stored_location]
url = template.last_saved_location(url)
end
template.link_to(label, url)
end
def clone_button_action(options = {})
label = translate_button(:clone)
url = options[:clone_url]
loading_text = translate_button(:loading_text)
template.link_to label, url,
class: options[:change_class] || "btn btn-primary btn-sm #{options[:class]}",
data: { loading_text: loading_text },
method: :post
end
def action(options = {})
action = object.persisted? ? :update : :create
label = options[:label] || translate_button(action)
loading_text = translate_button(:loading_text)
template.content_tag :button, label,
type: 'submit',
class: options[:change_class] || "btn btn-primary btn-sm #{options[:class]}",
data: { loading_text: loading_text }
end
def submit_text(action = :submit)
translate_button(action)
end
def translate_button(key)
template.t("simple_form.buttons.defaults.locomotive.#{key}")
end
# Extract the model names from the object_name mess, ignoring numeric and
# explicit child indexes.
# Prefix it by locomotive
#
# Example:
#
# route[blocks_attributes][0][blocks_learning_object_attributes][1][foo_attributes]
# ["locomotive", "route", "blocks", "blocks_learning_object", "foo"]
#
def lookup_model_names #:nodoc:
@lookup_model_names ||= begin
child_index = options[:child_index]
names = object_name.to_s.scan(/(?!\d)\w+/).flatten
names.unshift('locomotive')
names.delete(child_index) if child_index
names.each { |name| name.gsub!('_attributes', '') }
names.freeze
end
end
end
end