forked from heartcombo/simple_form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_form.rb
192 lines (152 loc) · 6.58 KB
/
simple_form.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
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
require 'action_view'
require 'simple_form/action_view_extensions/form_helper'
require 'simple_form/action_view_extensions/builder'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/reverse_merge'
module SimpleForm
autoload :Components, 'simple_form/components'
autoload :ErrorNotification, 'simple_form/error_notification'
autoload :FormBuilder, 'simple_form/form_builder'
autoload :Helpers, 'simple_form/helpers'
autoload :I18nCache, 'simple_form/i18n_cache'
autoload :Inputs, 'simple_form/inputs'
autoload :MapType, 'simple_form/map_type'
autoload :Wrappers, 'simple_form/wrappers'
## CONFIGURATION OPTIONS
# Method used to tidy up errors.
mattr_accessor :error_method
@@error_method = :first
# Default tag used for error notification helper.
mattr_accessor :error_notification_tag
@@error_notification_tag = :p
# CSS class to add for error notification helper.
mattr_accessor :error_notification_class
@@error_notification_class = :error_notification
# ID to add for error notification helper.
mattr_accessor :error_notification_id
@@error_notification_id = nil
# Series of attemps to detect a default label method for collection.
mattr_accessor :collection_label_methods
@@collection_label_methods = [ :to_label, :name, :title, :to_s ]
# Series of attemps to detect a default value method for collection.
mattr_accessor :collection_value_methods
@@collection_value_methods = [ :id, :to_s ]
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
mattr_accessor :collection_wrapper_tag
@@collection_wrapper_tag = nil
# You can define the class to use on all collection wrappers. Defaulting to none.
mattr_accessor :collection_wrapper_class
@@collection_wrapper_class = nil
# You can wrap each item in a collection of radio/check boxes with a tag, defaulting to none.
mattr_accessor :item_wrapper_tag
@@item_wrapper_tag = :span
# How the label text should be generated altogether with the required text.
mattr_accessor :label_text
@@label_text = lambda { |label, required| "#{required} #{label}" }
# You can define the class to use on all labels. Default is nil.
mattr_accessor :label_class
@@label_class = nil
# You can define the class to use on all forms. Default is simple_form.
mattr_accessor :form_class
@@form_class = :simple_form
# Whether attributes are required by default (or not).
mattr_accessor :required_by_default
@@required_by_default = true
# Tell browsers whether to use default HTML5 validations (novalidate option).
mattr_accessor :browser_validations
@@browser_validations = true
# Collection of methods to detect if a file type was given.
mattr_accessor :file_methods
@@file_methods = [ :mounted_as, :file?, :public_filename ]
# Custom mappings for input types. This should be a hash containing a regexp
# to match as key, and the input type that will be used when the field name
# matches the regexp as value, such as { /count/ => :integer }.
mattr_accessor :input_mappings
@@input_mappings = nil
# Default priority for time_zone inputs.
mattr_accessor :time_zone_priority
@@time_zone_priority = nil
# Default priority for country inputs.
mattr_accessor :country_priority
@@country_priority = nil
# Maximum size allowed for inputs.
mattr_accessor :default_input_size
@@default_input_size = 50
# When off, do not use translations in labels. Disabling translation in
# hints and placeholders can be done manually in the wrapper API.
mattr_accessor :translate_labels
@@translate_labels = true
# Automatically discover new inputs in Rails' autoload path.
mattr_accessor :inputs_discovery
@@inputs_discovery = true
# Cache simple form inputs discovery
mattr_accessor :cache_discovery
@@cache_discovery = defined?(Rails) && !Rails.env.development?
# Adds a class to each generated button, mostly for compatiblity
mattr_accessor :button_class
@@button_class = 'button'
## WRAPPER CONFIGURATION
@@wrappers = {}
# Retrieves a given wrapper
def self.wrapper(name)
@@wrappers[name.to_sym] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
end
# Raised when fails to find a given wrapper name
class WrapperNotFound < StandardError
end
# Define a new wrapper using SimpleForm::Wrappers::Builder
# and store it in the given name.
def self.wrappers(*args, &block)
if block_given?
options = args.extract_options!
name = args.first || :default
@@wrappers[name.to_sym] = build(options, &block)
else
@@wrappers
end
end
# Builds a new wrapper using SimpleForm::Wrappers::Builder.
def self.build(options={})
options[:tag] = :div if options[:tag].nil?
builder = SimpleForm::Wrappers::Builder.new
yield builder
SimpleForm::Wrappers::Root.new(builder.to_a, options)
end
wrappers :class => :input, :hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
b.use :html5
b.use :min_max
b.use :maxlength
b.use :placeholder
b.use :pattern
b.use :readonly
b.use :label_input
b.use :hint, :tag => :span, :class => :hint
b.use :error, :tag => :span, :class => :error
end
## SETUP
DEPRECATED = %w(hint_tag hint_class error_tag error_class wrapper_tag wrapper_class wrapper_error_class components html5)
@@deprecated = []
DEPRECATED.each do |method|
class_eval "def self.#{method}=(*); @@deprecated << :#{method}=; end"
class_eval "def self.#{method}; ActiveSupport::Deprecation.warn 'SimpleForm.#{method} is deprecated and has no effect'; end"
end
def self.translate=(value)
ActiveSupport::Deprecation.warn "SimpleForm.translate= is disabled in favor of translate_labels="
self.translate_labels = value
end
def self.translate
ActiveSupport::Deprecation.warn "SimpleForm.translate is disabled in favor of translate_labels"
self.translate_labels
end
# Default way to setup SimpleForm. Run rails generate simple_form:install
# to create a fresh initializer with all configuration values.
def self.setup
yield self
unless @@deprecated.empty?
raise "[SIMPLE FORM] Your simple form initializer file is using the following methods: #{@@deprecated.to_sentence}. " <<
"Those methods are part of the outdated configuration API. Updating to the new API is easy and fast. " <<
"Check for more info here: https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0"
end
end
end