forked from heartcombo/simple_form
/
base.rb
119 lines (103 loc) · 3.46 KB
/
base.rb
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
module SimpleForm
module Inputs
class Base
extend I18nCache
# When action is create or update, we still should use new and edit
ACTIONS = {
:create => :new,
:update => :edit
}
include SimpleForm::Components::Errors
include SimpleForm::Components::Hints
include SimpleForm::Components::Labels
include SimpleForm::Components::Wrapper
delegate :template, :object, :object_name, :attribute_name, :column,
:reflection, :input_type, :options, :to => :@builder
def initialize(builder)
@builder = builder
end
def input
raise NotImplementedError
end
def input_options
options
end
def input_html_options
html_options_for(:input, input_type, required_class)
end
def render
content = SimpleForm.components.map do |component|
next if options[component] == false
send(component)
end
content.compact!
content = html_safe(content.join)
html_safe(wrap(content))
end
protected
def attribute_required?
options[:required] != false
end
def required_class
attribute_required? ? :required : :optional
end
# Find reflection name when available, otherwise use attribute
def reflection_or_attribute_name
reflection ? reflection.name : attribute_name
end
# Retrieve options for the given namespace from the options hash
def html_options_for(namespace, *extra)
html_options = options[:"#{namespace}_html"] || {}
html_options[:class] = (extra << html_options[:class]).join(' ').strip if extra.present?
html_options
end
# Ensure we make html safe only if it responds to it.
def html_safe(content)
if content.respond_to?(:html_safe)
content.html_safe
elsif content.respond_to?(:html_safe!)
content.html_safe!
else
content
end
end
# Lookup translations for the given namespace using I18n, based on object name,
# actual action and attribute name. Lookup priority as follows:
#
# simple_form.{namespace}.{model}.{action}.{attribute}
# simple_form.{namespace}.{model}.{attribute}
# simple_form.{namespace}.{attribute}
#
# Namespace is used for :labels and :hints.
#
# Model is the actual object name, for a @user object you'll have :user.
# Action is the action being rendered, usually :new or :edit.
# And attribute is the attribute itself, :name for example.
#
# Example:
#
# simple_form:
# labels:
# user:
# new:
# email: 'E-mail para efetuar o sign in.'
# edit:
# email: 'E-mail.'
#
# Take a look at our locale example file.
def translate(namespace, default='')
lookups = []
lookups << :"#{object_name}.#{lookup_action}.#{reflection_or_attribute_name}"
lookups << :"#{object_name}.#{reflection_or_attribute_name}"
lookups << :"#{reflection_or_attribute_name}"
lookups << default
I18n.t(lookups.shift, :scope => :"simple_form.#{namespace}", :default => lookups)
end
# The action to be used in lookup.
def lookup_action
action = template.controller.action_name.to_sym
ACTIONS[action] || action
end
end
end
end