simple_form 1.5.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +234 -0
- data/MIT-LICENSE +1 -1
- data/README.md +816 -0
- data/lib/generators/simple_form/install_generator.rb +15 -1
- data/lib/generators/simple_form/templates/README +12 -0
- data/lib/generators/simple_form/templates/_form.html.erb +2 -2
- data/lib/generators/simple_form/templates/_form.html.haml +2 -2
- data/lib/generators/simple_form/templates/_form.html.slim +4 -4
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +176 -0
- data/lib/simple_form/action_view_extensions/builder.rb +206 -59
- data/lib/simple_form/action_view_extensions/form_helper.rb +30 -23
- data/lib/simple_form/components/errors.rb +6 -24
- data/lib/simple_form/components/hints.rb +7 -21
- data/lib/simple_form/components/html5.rb +26 -0
- data/lib/simple_form/components/labels.rb +22 -14
- data/lib/simple_form/components/maxlength.rb +41 -0
- data/lib/simple_form/components/min_max.rb +49 -0
- data/lib/simple_form/components/pattern.rb +34 -0
- data/lib/simple_form/components/placeholders.rb +5 -17
- data/lib/simple_form/components/readonly.rb +22 -0
- data/lib/simple_form/components.rb +11 -1
- data/lib/simple_form/core_ext/hash.rb +16 -0
- data/lib/simple_form/error_notification.rb +9 -3
- data/lib/simple_form/form_builder.rb +105 -28
- data/lib/simple_form/helpers/autofocus.rb +11 -0
- data/lib/simple_form/helpers/disabled.rb +15 -0
- data/lib/simple_form/helpers/readonly.rb +15 -0
- data/lib/simple_form/helpers/required.rb +10 -11
- data/lib/simple_form/helpers/validators.rb +4 -4
- data/lib/simple_form/helpers.rb +7 -4
- data/lib/simple_form/inputs/base.rb +53 -81
- data/lib/simple_form/inputs/boolean_input.rb +46 -4
- data/lib/simple_form/inputs/collection_check_boxes_input.rb +21 -0
- data/lib/simple_form/inputs/collection_input.rb +27 -13
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +67 -0
- data/lib/simple_form/inputs/collection_select_input.rb +14 -0
- data/lib/simple_form/inputs/date_time_input.rb +10 -6
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +41 -0
- data/lib/simple_form/inputs/hidden_input.rb +3 -6
- data/lib/simple_form/inputs/numeric_input.rb +3 -51
- data/lib/simple_form/inputs/password_input.rb +1 -2
- data/lib/simple_form/inputs/priority_input.rb +2 -2
- data/lib/simple_form/inputs/range_input.rb +1 -3
- data/lib/simple_form/inputs/string_input.rb +6 -8
- data/lib/simple_form/inputs/text_input.rb +1 -2
- data/lib/simple_form/inputs.rb +17 -13
- data/lib/simple_form/version.rb +1 -1
- data/lib/simple_form/wrappers/builder.rb +103 -0
- data/lib/simple_form/wrappers/many.rb +69 -0
- data/lib/simple_form/wrappers/root.rb +34 -0
- data/lib/simple_form/wrappers/single.rb +18 -0
- data/lib/simple_form/wrappers.rb +8 -0
- data/lib/simple_form.rb +118 -48
- data/test/action_view_extensions/builder_test.rb +285 -102
- data/test/action_view_extensions/form_helper_test.rb +32 -10
- data/test/components/label_test.rb +44 -5
- data/test/form_builder/association_test.rb +177 -0
- data/test/form_builder/button_test.rb +47 -0
- data/test/{error_notification_test.rb → form_builder/error_notification_test.rb} +18 -1
- data/test/form_builder/error_test.rb +121 -0
- data/test/form_builder/general_test.rb +356 -0
- data/test/form_builder/hint_test.rb +123 -0
- data/test/form_builder/input_field_test.rb +63 -0
- data/test/form_builder/label_test.rb +65 -0
- data/test/form_builder/wrapper_test.rb +149 -0
- data/test/generators/simple_form_generator_test.rb +32 -0
- data/test/inputs/boolean_input_test.rb +101 -0
- data/test/inputs/collection_check_boxes_input_test.rb +224 -0
- data/test/inputs/collection_radio_buttons_input_test.rb +326 -0
- data/test/inputs/collection_select_input_test.rb +241 -0
- data/test/inputs/datetime_input_test.rb +99 -0
- data/test/inputs/disabled_test.rb +38 -0
- data/test/inputs/discovery_test.rb +61 -0
- data/test/inputs/file_input_test.rb +16 -0
- data/test/inputs/general_test.rb +69 -0
- data/test/inputs/grouped_collection_select_input_test.rb +118 -0
- data/test/inputs/hidden_input_test.rb +30 -0
- data/test/inputs/numeric_input_test.rb +167 -0
- data/test/inputs/priority_input_test.rb +43 -0
- data/test/inputs/readonly_test.rb +61 -0
- data/test/inputs/required_test.rb +113 -0
- data/test/inputs/string_input_test.rb +140 -0
- data/test/inputs/text_input_test.rb +24 -0
- data/test/support/misc_helpers.rb +53 -12
- data/test/support/mock_controller.rb +2 -2
- data/test/support/models.rb +20 -5
- data/test/test_helper.rb +11 -12
- metadata +124 -96
- data/.gitignore +0 -3
- data/.gitmodules +0 -3
- data/.travis.yml +0 -15
- data/CHANGELOG.rdoc +0 -159
- data/Gemfile +0 -9
- data/README.rdoc +0 -466
- data/Rakefile +0 -27
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +0 -93
- data/lib/simple_form/components/wrapper.rb +0 -38
- data/lib/simple_form/helpers/has_errors.rb +0 -15
- data/lib/simple_form/helpers/maxlength.rb +0 -24
- data/lib/simple_form/helpers/pattern.rb +0 -28
- data/simple_form.gemspec +0 -25
- data/test/components/error_test.rb +0 -56
- data/test/components/hint_test.rb +0 -74
- data/test/components/wrapper_test.rb +0 -63
- data/test/custom_components.rb +0 -7
- data/test/form_builder_test.rb +0 -930
- data/test/inputs_test.rb +0 -995
- /data/test/{discovery_inputs.rb → support/discovery_inputs.rb} +0 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module SimpleForm
|
|
2
|
+
module Wrappers
|
|
3
|
+
# `Single` is an optimization for a wrapper that has only one component.
|
|
4
|
+
class Single < Many
|
|
5
|
+
def initialize(name, options={})
|
|
6
|
+
super(name, [name], options)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def render(input)
|
|
10
|
+
options = input.options
|
|
11
|
+
if options[namespace] != false
|
|
12
|
+
content = input.send(namespace)
|
|
13
|
+
wrap(input, options, content) if content
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/simple_form.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
require 'action_view'
|
|
2
2
|
require 'simple_form/action_view_extensions/form_helper'
|
|
3
3
|
require 'simple_form/action_view_extensions/builder'
|
|
4
|
+
require 'active_support/core_ext/hash/slice'
|
|
5
|
+
require 'active_support/core_ext/hash/except'
|
|
6
|
+
require 'active_support/core_ext/hash/reverse_merge'
|
|
4
7
|
|
|
5
8
|
module SimpleForm
|
|
6
9
|
autoload :Components, 'simple_form/components'
|
|
@@ -10,22 +13,9 @@ module SimpleForm
|
|
|
10
13
|
autoload :I18nCache, 'simple_form/i18n_cache'
|
|
11
14
|
autoload :Inputs, 'simple_form/inputs'
|
|
12
15
|
autoload :MapType, 'simple_form/map_type'
|
|
16
|
+
autoload :Wrappers, 'simple_form/wrappers'
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
mattr_accessor :hint_tag
|
|
16
|
-
@@hint_tag = :span
|
|
17
|
-
|
|
18
|
-
# CSS class to add to all hint tags.
|
|
19
|
-
mattr_accessor :hint_class
|
|
20
|
-
@@hint_class = :hint
|
|
21
|
-
|
|
22
|
-
# Default tag used on errors.
|
|
23
|
-
mattr_accessor :error_tag
|
|
24
|
-
@@error_tag = :span
|
|
25
|
-
|
|
26
|
-
# CSS class to add to all error tags.
|
|
27
|
-
mattr_accessor :error_class
|
|
28
|
-
@@error_class = :error
|
|
18
|
+
## CONFIGURATION OPTIONS
|
|
29
19
|
|
|
30
20
|
# Method used to tidy up errors.
|
|
31
21
|
mattr_accessor :error_method
|
|
@@ -39,14 +29,6 @@ module SimpleForm
|
|
|
39
29
|
mattr_accessor :error_notification_class
|
|
40
30
|
@@error_notification_class = :error_notification
|
|
41
31
|
|
|
42
|
-
# ID to add for error notification helper.
|
|
43
|
-
mattr_accessor :error_notification_id
|
|
44
|
-
@@error_notification_id = nil
|
|
45
|
-
|
|
46
|
-
# Components used by the form builder.
|
|
47
|
-
mattr_accessor :components
|
|
48
|
-
@@components = [ :placeholder, :label_input, :hint, :error ]
|
|
49
|
-
|
|
50
32
|
# Series of attemps to detect a default label method for collection.
|
|
51
33
|
mattr_accessor :collection_label_methods
|
|
52
34
|
@@collection_label_methods = [ :to_label, :name, :title, :to_s ]
|
|
@@ -59,21 +41,19 @@ module SimpleForm
|
|
|
59
41
|
mattr_accessor :collection_wrapper_tag
|
|
60
42
|
@@collection_wrapper_tag = nil
|
|
61
43
|
|
|
62
|
-
# You can
|
|
44
|
+
# You can define the class to use on all collection wrappers, defaulting to none.
|
|
45
|
+
mattr_accessor :collection_wrapper_class
|
|
46
|
+
@@collection_wrapper_class = nil
|
|
47
|
+
|
|
48
|
+
# You can wrap each item in a collection of radio/check boxes with a tag,
|
|
49
|
+
# defaulting to none. Please note that when using :boolean_style = :nested,
|
|
50
|
+
# SimpleForm will force this option to be a :label.
|
|
63
51
|
mattr_accessor :item_wrapper_tag
|
|
64
52
|
@@item_wrapper_tag = :span
|
|
65
53
|
|
|
66
|
-
# You can
|
|
67
|
-
mattr_accessor :
|
|
68
|
-
@@
|
|
69
|
-
|
|
70
|
-
# You can define the class to use on all wrappers. Default is input.
|
|
71
|
-
mattr_accessor :wrapper_class
|
|
72
|
-
@@wrapper_class = :input
|
|
73
|
-
|
|
74
|
-
# You can define the class to add to the wrapper when the field has errors. Default is field_with_errors.
|
|
75
|
-
mattr_accessor :wrapper_error_class
|
|
76
|
-
@@wrapper_error_class = :field_with_errors
|
|
54
|
+
# You can define the class to use on all item wrappers, defaulting to none.
|
|
55
|
+
mattr_accessor :item_wrapper_class
|
|
56
|
+
@@item_wrapper_class = nil
|
|
77
57
|
|
|
78
58
|
# How the label text should be generated altogether with the required text.
|
|
79
59
|
mattr_accessor :label_text
|
|
@@ -83,10 +63,20 @@ module SimpleForm
|
|
|
83
63
|
mattr_accessor :label_class
|
|
84
64
|
@@label_class = nil
|
|
85
65
|
|
|
66
|
+
# Define the way to render check boxes / radio buttons with labels.
|
|
67
|
+
# :inline => input + label (default)
|
|
68
|
+
# :nested => label > input
|
|
69
|
+
mattr_accessor :boolean_style
|
|
70
|
+
@@boolean_style = :inline
|
|
71
|
+
|
|
86
72
|
# You can define the class to use on all forms. Default is simple_form.
|
|
87
73
|
mattr_accessor :form_class
|
|
88
74
|
@@form_class = :simple_form
|
|
89
75
|
|
|
76
|
+
# You can define which elements should obtain additional classes
|
|
77
|
+
mattr_accessor :generate_additional_classes_for
|
|
78
|
+
@@generate_additional_classes_for = [:wrapper, :label, :input]
|
|
79
|
+
|
|
90
80
|
# Whether attributes are required by default (or not).
|
|
91
81
|
mattr_accessor :required_by_default
|
|
92
82
|
@@required_by_default = true
|
|
@@ -95,13 +85,6 @@ module SimpleForm
|
|
|
95
85
|
mattr_accessor :browser_validations
|
|
96
86
|
@@browser_validations = true
|
|
97
87
|
|
|
98
|
-
# Determines whether HTML5 types (:email, :url, :search, :tel) and attributes
|
|
99
|
-
# (e.g. required) are used or not. True by default.
|
|
100
|
-
# Having this on in non-HTML5 compliant sites can cause odd behavior in
|
|
101
|
-
# HTML5-aware browsers such as Chrome.
|
|
102
|
-
mattr_accessor :html5
|
|
103
|
-
@@html5 = true
|
|
104
|
-
|
|
105
88
|
# Collection of methods to detect if a file type was given.
|
|
106
89
|
mattr_accessor :file_methods
|
|
107
90
|
@@file_methods = [ :mounted_as, :file?, :public_filename ]
|
|
@@ -124,22 +107,109 @@ module SimpleForm
|
|
|
124
107
|
mattr_accessor :default_input_size
|
|
125
108
|
@@default_input_size = 50
|
|
126
109
|
|
|
127
|
-
# When off, do not use translations in
|
|
128
|
-
#
|
|
129
|
-
mattr_accessor :
|
|
130
|
-
@@
|
|
110
|
+
# When off, do not use translations in labels. Disabling translation in
|
|
111
|
+
# hints and placeholders can be done manually in the wrapper API.
|
|
112
|
+
mattr_accessor :translate_labels
|
|
113
|
+
@@translate_labels = true
|
|
131
114
|
|
|
132
115
|
# Automatically discover new inputs in Rails' autoload path.
|
|
133
116
|
mattr_accessor :inputs_discovery
|
|
134
117
|
@@inputs_discovery = true
|
|
135
118
|
|
|
136
|
-
# Cache
|
|
119
|
+
# Cache SimpleForm inputs discovery
|
|
137
120
|
mattr_accessor :cache_discovery
|
|
138
|
-
@@cache_discovery = !Rails.env.development?
|
|
121
|
+
@@cache_discovery = defined?(Rails) && !Rails.env.development?
|
|
122
|
+
|
|
123
|
+
# Adds a class to each generated button, mostly for compatiblity
|
|
124
|
+
mattr_accessor :button_class
|
|
125
|
+
@@button_class = 'button'
|
|
126
|
+
|
|
127
|
+
## WRAPPER CONFIGURATION
|
|
128
|
+
# The default wrapper to be used by the FormBuilder.
|
|
129
|
+
mattr_accessor :default_wrapper
|
|
130
|
+
@@default_wrapper = :default
|
|
131
|
+
@@wrappers = {}
|
|
132
|
+
|
|
133
|
+
# Retrieves a given wrapper
|
|
134
|
+
def self.wrapper(name)
|
|
135
|
+
@@wrappers[name.to_sym] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Raised when fails to find a given wrapper name
|
|
139
|
+
class WrapperNotFound < StandardError
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Define a new wrapper using SimpleForm::Wrappers::Builder
|
|
143
|
+
# and store it in the given name.
|
|
144
|
+
def self.wrappers(*args, &block)
|
|
145
|
+
if block_given?
|
|
146
|
+
options = args.extract_options!
|
|
147
|
+
name = args.first || :default
|
|
148
|
+
@@wrappers[name.to_sym] = build(options, &block)
|
|
149
|
+
else
|
|
150
|
+
@@wrappers
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Builds a new wrapper using SimpleForm::Wrappers::Builder.
|
|
155
|
+
def self.build(options={})
|
|
156
|
+
options[:tag] = :div if options[:tag].nil?
|
|
157
|
+
builder = SimpleForm::Wrappers::Builder.new(options)
|
|
158
|
+
yield builder
|
|
159
|
+
SimpleForm::Wrappers::Root.new(builder.to_a, options)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
wrappers :class => :input, :hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
|
|
163
|
+
b.use :html5
|
|
164
|
+
|
|
165
|
+
b.use :min_max
|
|
166
|
+
b.use :maxlength
|
|
167
|
+
b.use :placeholder
|
|
168
|
+
b.optional :pattern
|
|
169
|
+
b.optional :readonly
|
|
170
|
+
|
|
171
|
+
b.use :label_input
|
|
172
|
+
b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
|
|
173
|
+
b.use :error, :wrap_with => { :tag => :span, :class => :error }
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
## SETUP
|
|
177
|
+
|
|
178
|
+
DEPRECATED = %w(hint_tag hint_class error_tag error_class error_notification_id wrapper_tag wrapper_class wrapper_error_class components html5)
|
|
179
|
+
@@deprecated = []
|
|
180
|
+
|
|
181
|
+
DEPRECATED.each do |method|
|
|
182
|
+
class_eval "def self.#{method}=(*); @@deprecated << :#{method}=; end"
|
|
183
|
+
class_eval "def self.#{method}; deprecation_warn 'SimpleForm.#{method} is deprecated and has no effect'; end"
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def self.translate=(value)
|
|
187
|
+
deprecation_warn "SimpleForm.translate= is disabled in favor of translate_labels="
|
|
188
|
+
self.translate_labels = value
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def self.translate
|
|
192
|
+
deprecation_warn "SimpleForm.translate is disabled in favor of translate_labels"
|
|
193
|
+
self.translate_labels
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def self.deprecation_warn(message)
|
|
197
|
+
ActiveSupport::Deprecation.warn "[SIMPLE_FORM] #{message}", caller
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def self.additional_classes_for(component)
|
|
201
|
+
generate_additional_classes_for.include?(component) ? yield : []
|
|
202
|
+
end
|
|
139
203
|
|
|
140
204
|
# Default way to setup SimpleForm. Run rails generate simple_form:install
|
|
141
205
|
# to create a fresh initializer with all configuration values.
|
|
142
206
|
def self.setup
|
|
143
207
|
yield self
|
|
208
|
+
|
|
209
|
+
unless @@deprecated.empty?
|
|
210
|
+
raise "[SIMPLE FORM] Your SimpleForm initializer file is using the following methods: #{@@deprecated.to_sentence}. " <<
|
|
211
|
+
"Those methods are part of the outdated configuration API. Updating to the new API is easy and fast. " <<
|
|
212
|
+
"Check for more info here: https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0"
|
|
213
|
+
end
|
|
144
214
|
end
|
|
145
215
|
end
|