simple_form 1.0.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of simple_form might be problematic. Click here for more details.
- data/README.rdoc +17 -32
- data/init.rb +1 -1
- data/{generators → lib/generators}/simple_form_install/USAGE +0 -0
- data/lib/generators/simple_form_install/simple_form_install_generator.rb +19 -0
- data/lib/generators/simple_form_install/templates/_form.html.erb +11 -0
- data/lib/{simple_form/locale → generators/simple_form_install/templates}/en.yml +0 -4
- data/{generators → lib/generators}/simple_form_install/templates/simple_form.rb +13 -8
- data/lib/simple_form.rb +8 -4
- data/lib/simple_form/action_view_extensions/builder.rb +6 -6
- data/lib/simple_form/components/labels.rb +2 -2
- data/lib/simple_form/form_builder.rb +11 -42
- data/lib/simple_form/inputs.rb +2 -1
- data/lib/simple_form/inputs/base.rb +2 -14
- data/lib/simple_form/inputs/{text_field_input.rb → numeric_input.rb} +3 -3
- data/lib/simple_form/inputs/string_input.rb +24 -0
- data/lib/simple_form/version.rb +1 -1
- data/test/action_view_extensions/form_helper_test.rb +0 -13
- data/test/form_builder_test.rb +16 -56
- data/test/inputs_test.rb +10 -5
- data/test/support/models.rb +7 -5
- data/test/test_helper.rb +1 -1
- metadata +12 -40
- data/generators/simple_form_install/simple_form_install_generator.rb +0 -19
- data/lib/simple_form/action_view_extensions/instance_tag.rb +0 -37
data/README.rdoc
CHANGED
@@ -8,17 +8,16 @@ SimpleForm aims to be as flexible as possible while helping you with powerful co
|
|
8
8
|
|
9
9
|
Install the gem:
|
10
10
|
|
11
|
-
gem install simple_form --version=1.
|
12
|
-
|
13
|
-
Configure simple_form gem inside your app:
|
14
|
-
|
15
|
-
config.gem 'simple_form'
|
11
|
+
sudo gem install simple_form --version=1.1
|
16
12
|
|
17
13
|
Run the generator:
|
18
14
|
|
19
|
-
|
15
|
+
rails generate simple_form_install
|
16
|
+
|
17
|
+
And you are ready to go. Since this branch is aims Rails 3 support,
|
18
|
+
if you want to use it with Rails 2.3 you should check this branch:
|
20
19
|
|
21
|
-
|
20
|
+
http://github.com/plataformatec/simple_form/tree/v1.0
|
22
21
|
|
23
22
|
== Usage
|
24
23
|
|
@@ -158,8 +157,8 @@ Now we have the user form:
|
|
158
157
|
|
159
158
|
Simple enough right? This is going to render a :select input for choosing the :company, and another :select input with :multiple option for the :roles. You can of course change it, to use radios and check boxes as well:
|
160
159
|
|
161
|
-
|
162
|
-
|
160
|
+
f.association :company, :as => :radio
|
161
|
+
f.association :roles, :as => :check_boxes
|
163
162
|
|
164
163
|
And you will get a set of radios to select the company and another set of check boxes for the roles. Some options are available for refining the collection for associations: :conditions, :include, :joins, :order. These options are given straight to the find method. Here is an example of how to use these options:
|
165
164
|
|
@@ -176,34 +175,14 @@ The association helper just invokes input under the hood, so all options availab
|
|
176
175
|
|
177
176
|
== Buttons
|
178
177
|
|
179
|
-
All web forms need buttons, right?
|
178
|
+
All web forms need buttons, right? SimpleForm wraps them in the DSL, acting like a proxy:
|
180
179
|
|
181
180
|
<% simple_form_for @user do |f| -%>
|
182
181
|
<p><%= f.input :name %></p>
|
183
182
|
<p><%= f.button :submit %></p>
|
184
183
|
<% end -%>
|
185
184
|
|
186
|
-
|
187
|
-
|
188
|
-
You can also pass the button text directly:
|
189
|
-
|
190
|
-
f.button :submit, 'Save User'
|
191
|
-
f.button :submit, :label => 'Save User'
|
192
|
-
|
193
|
-
As button is just a wrapper, it is actually calling Rails helper :submit_tag with the provided text. That said, any other option you pass will be given to :submit_tag call:
|
194
|
-
|
195
|
-
f.button :submit, :confirm => 'Are you sure?'
|
196
|
-
|
197
|
-
And if you want to use any other button tag, you just need to create a helper that ends with "_tag":
|
198
|
-
|
199
|
-
# inside ApplicationHelper for instance
|
200
|
-
def custom_submit_tag(text, options={})
|
201
|
-
# render submit tag here
|
202
|
-
end
|
203
|
-
|
204
|
-
And you will able to use it with SimpleForm:
|
205
|
-
|
206
|
-
f.button :custom_submit
|
185
|
+
The above will simply call submit. You choose to use it or not, it's just a question of taste.
|
207
186
|
|
208
187
|
== Extra helpers
|
209
188
|
|
@@ -238,7 +217,7 @@ Creates a collection of radio inputs with labels associated (same API as collect
|
|
238
217
|
Creates a collection of check boxes with labels associated (same API as collection_select):
|
239
218
|
|
240
219
|
form_for @user do |f|
|
241
|
-
f.
|
220
|
+
f.collection_check_box :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
|
242
221
|
end
|
243
222
|
|
244
223
|
<input name="user[options][]" type="hidden" value="" />
|
@@ -256,6 +235,8 @@ SimpleForm comes with a lot of default mappings:
|
|
256
235
|
|
257
236
|
boolean check box boolean
|
258
237
|
string text field string
|
238
|
+
email email field string
|
239
|
+
url url field string
|
259
240
|
password password field string with name matching "password"
|
260
241
|
text text area text
|
261
242
|
file file field string, responding to file methods
|
@@ -324,6 +305,10 @@ SimpleForm has several configuration values. You can read and change them in the
|
|
324
305
|
|
325
306
|
ruby script/generate simple_form_install
|
326
307
|
|
308
|
+
== TODO
|
309
|
+
|
310
|
+
Please refer to TODO file.
|
311
|
+
|
327
312
|
== Maintainers
|
328
313
|
|
329
314
|
* José Valim (http://github.com/josevalim)
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'simple_form'
|
1
|
+
require 'simple_form'
|
File without changes
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class SimpleFormInstallGenerator < Rails::Generators::Base
|
2
|
+
desc "Copy SimpleForm default files"
|
3
|
+
|
4
|
+
def self.source_root
|
5
|
+
@_source_root = File.expand_path('../templates', __FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
def copy_initializers
|
9
|
+
copy_file 'simple_form.rb', 'config/initializers/simple_form.rb'
|
10
|
+
end
|
11
|
+
|
12
|
+
def copy_locale_file
|
13
|
+
copy_file 'en.yml', 'config/locales/simple_form.en.yml'
|
14
|
+
end
|
15
|
+
|
16
|
+
def copy_scaffold_template
|
17
|
+
copy_file '_form.html.erb', 'lib/templates/erb/scaffold/_form.html.erb'
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<%% simple_form_for(@<%= singular_name %>) do |f| %>
|
2
|
+
<div class="inputs">
|
3
|
+
<%- attributes.each do |attribute| -%>
|
4
|
+
<%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
|
5
|
+
<%- end -%>
|
6
|
+
</div>
|
7
|
+
|
8
|
+
<div class="actions">
|
9
|
+
<%%= f.button :submit %>
|
10
|
+
</div>
|
11
|
+
<%% end %>
|
@@ -5,16 +5,18 @@ SimpleForm.setup do |config|
|
|
5
5
|
# remove any of them, change the order, or even add your own components in the
|
6
6
|
# stack. By inheriting your component from SimpleForm::Components::Base you'll
|
7
7
|
# have some extra helpers for free.
|
8
|
-
# config.components = [
|
8
|
+
# config.components = [
|
9
|
+
# SimpleForm::Components::Wrapper, SimpleForm::Components::Label,
|
10
|
+
# SimpleForm::Components::Input, SimpleForm::Components::Hint,
|
11
|
+
# SimpleForm::Components::Error
|
12
|
+
#]
|
9
13
|
|
10
|
-
# Default tag used
|
11
|
-
#
|
14
|
+
# Default tag used in components (hints and errors basically). When using one
|
15
|
+
# of these components, this tag will be used to render it.
|
16
|
+
# config.component_tag = :span
|
12
17
|
|
13
|
-
#
|
14
|
-
# config.
|
15
|
-
|
16
|
-
# You can wrap all inputs in a pre-defined tag.
|
17
|
-
# config.wrapper_tag = :div
|
18
|
+
# You can wrap all inputs in a pre-defined tag. By default is nil.
|
19
|
+
# config.wrapper_tag = nil
|
18
20
|
|
19
21
|
# How the label text should be generated altogether with the required text.
|
20
22
|
# config.label_text = lambda { |label, required| "#{required} #{label}" }
|
@@ -33,4 +35,7 @@ SimpleForm.setup do |config|
|
|
33
35
|
|
34
36
|
# Default priority for country inputs.
|
35
37
|
# config.country_priority = nil
|
38
|
+
|
39
|
+
# Default size for text inputs
|
40
|
+
# config.default_input_size = 50
|
36
41
|
end
|
data/lib/simple_form.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
+
require 'action_view'
|
1
2
|
require 'simple_form/action_view_extensions/form_helper'
|
2
3
|
require 'simple_form/action_view_extensions/builder'
|
3
|
-
require 'simple_form/action_view_extensions/instance_tag'
|
4
4
|
|
5
5
|
module SimpleForm
|
6
6
|
autoload :Components, 'simple_form/components'
|
@@ -9,11 +9,11 @@ module SimpleForm
|
|
9
9
|
autoload :Inputs, 'simple_form/inputs'
|
10
10
|
autoload :MapType, 'simple_form/map_type'
|
11
11
|
|
12
|
-
# Default tag used
|
12
|
+
# Default tag used in hints.
|
13
13
|
mattr_accessor :hint_tag
|
14
14
|
@@hint_tag = :span
|
15
15
|
|
16
|
-
# Default tag used
|
16
|
+
# Default tag used in errors.
|
17
17
|
mattr_accessor :error_tag
|
18
18
|
@@error_tag = :span
|
19
19
|
|
@@ -31,7 +31,7 @@ module SimpleForm
|
|
31
31
|
|
32
32
|
# You can wrap all inputs in a pre-defined tag. By default is nil.
|
33
33
|
mattr_accessor :wrapper_tag
|
34
|
-
@@wrapper_tag =
|
34
|
+
@@wrapper_tag = :div
|
35
35
|
|
36
36
|
# How the label text should be generated altogether with the required text.
|
37
37
|
mattr_accessor :label_text
|
@@ -49,6 +49,10 @@ module SimpleForm
|
|
49
49
|
mattr_accessor :country_priority
|
50
50
|
@@country_priority = nil
|
51
51
|
|
52
|
+
# Maximum size allowed for inputs.
|
53
|
+
mattr_accessor :default_input_size
|
54
|
+
@@default_input_size = 50
|
55
|
+
|
52
56
|
# Default way to setup SimpleForm. Run script/generate simple_form_install
|
53
57
|
# to create a fresh initializer with all configuration values.
|
54
58
|
def self.setup
|
@@ -38,7 +38,7 @@ module SimpleForm
|
|
38
38
|
|
39
39
|
result << radio_button(attribute, value, default_html_options) <<
|
40
40
|
label("#{attribute}_#{value}", text, :class => "collection_radio")
|
41
|
-
end
|
41
|
+
end.html_safe
|
42
42
|
end
|
43
43
|
|
44
44
|
# Creates a collection of check boxes for each item in the collection, associated
|
@@ -48,15 +48,15 @@ module SimpleForm
|
|
48
48
|
# == Examples
|
49
49
|
#
|
50
50
|
# form_for @user do |f|
|
51
|
-
# f.
|
51
|
+
# f.collection_check_box :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
|
52
52
|
# end
|
53
53
|
#
|
54
54
|
# <input name="user[options][]" type="hidden" value="" />
|
55
55
|
# <input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
|
56
|
-
# <label class="
|
56
|
+
# <label class="collection_check_box" for="user_options_true">Yes</label>
|
57
57
|
# <input name="user[options][]" type="hidden" value="" />
|
58
58
|
# <input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
|
59
|
-
# <label class="
|
59
|
+
# <label class="collection_check_box" for="user_options_false">No</label>
|
60
60
|
#
|
61
61
|
# == Options
|
62
62
|
#
|
@@ -78,7 +78,7 @@ module SimpleForm
|
|
78
78
|
|
79
79
|
result << check_box(attribute, default_html_options, value, '') <<
|
80
80
|
label("#{attribute}_#{value}", text, :class => "collection_check_boxes")
|
81
|
-
end
|
81
|
+
end.html_safe
|
82
82
|
end
|
83
83
|
|
84
84
|
# Wrapper for using simple form inside a default rails form.
|
@@ -101,7 +101,7 @@ module SimpleForm
|
|
101
101
|
# Generate default options for collection helpers, such as :checked and
|
102
102
|
# :disabled.
|
103
103
|
def default_html_options_for_collection(item, value, options, html_options) #:nodoc:
|
104
|
-
html_options.dup
|
104
|
+
returning(html_options.dup) do |default_html_options|
|
105
105
|
[:checked, :disabled].each do |option|
|
106
106
|
next unless options[option]
|
107
107
|
|
@@ -28,7 +28,7 @@ module SimpleForm
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def label_text
|
31
|
-
|
31
|
+
SimpleForm.label_text.call(raw_label_text, required_label_text)
|
32
32
|
end
|
33
33
|
|
34
34
|
def label_target
|
@@ -65,4 +65,4 @@ module SimpleForm
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
68
|
-
end
|
68
|
+
end
|
@@ -7,7 +7,8 @@ module SimpleForm
|
|
7
7
|
include SimpleForm::Inputs
|
8
8
|
|
9
9
|
map_type :boolean, :password, :text, :file, :to => SimpleForm::Inputs::MappingInput
|
10
|
-
map_type :string, :
|
10
|
+
map_type :string, :email, :url, :to => SimpleForm::Inputs::StringInput
|
11
|
+
map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
|
11
12
|
map_type :select, :radio, :check_boxes, :to => SimpleForm::Inputs::CollectionInput
|
12
13
|
map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput
|
13
14
|
map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput
|
@@ -172,7 +173,7 @@ module SimpleForm
|
|
172
173
|
klass.all(finders)
|
173
174
|
end
|
174
175
|
|
175
|
-
input(attribute, options)
|
176
|
+
returning(input(attribute, options)) { @reflection = nil }
|
176
177
|
end
|
177
178
|
|
178
179
|
# Creates a button:
|
@@ -181,48 +182,14 @@ module SimpleForm
|
|
181
182
|
# f.button :submit
|
182
183
|
# end
|
183
184
|
#
|
184
|
-
#
|
185
|
-
# otherwise it will create with label "Update User". You can overwrite the label
|
186
|
-
# giving a second parameter or giving :label.
|
185
|
+
# It just acts as a proxy to method name given.
|
187
186
|
#
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
#
|
194
|
-
# submit_tag "Create a new user"
|
195
|
-
#
|
196
|
-
# All options given to button are given straight to submit_tag. That said, you can
|
197
|
-
# use :confirm normally:
|
198
|
-
#
|
199
|
-
# f.button :submit, :confirm => "Are you sure?"
|
200
|
-
#
|
201
|
-
# And if you want to use image_submit_tag, just give it as an option:
|
202
|
-
#
|
203
|
-
# f.button :image_submit, "/images/foo/bar.png"
|
204
|
-
#
|
205
|
-
# This comes with a bonus that any method added to your ApplicationController can
|
206
|
-
# be used by SimpleForm, as long as it ends with _tag. So is quite easy to customize
|
207
|
-
# your buttons.
|
208
|
-
#
|
209
|
-
def button(type, *args)
|
210
|
-
options = args.extract_options!
|
211
|
-
value = args.first || options.delete(:label)
|
212
|
-
key = @object ? (@object.new_record? ? :create : :update) : :submit
|
213
|
-
|
214
|
-
value ||= begin
|
215
|
-
model = if @object.class.respond_to?(:human_name)
|
216
|
-
@object.class.human_name
|
217
|
-
else
|
218
|
-
@object_name.to_s.humanize
|
219
|
-
end
|
220
|
-
|
221
|
-
I18n.t(:"simple_form.buttons.#{key}", :model => model, :default => "#{key.to_s.humanize} #{model}")
|
187
|
+
def button(type, *args, &block)
|
188
|
+
if respond_to?(:"#{type}_button")
|
189
|
+
send(:"#{type}_button", *args, &block)
|
190
|
+
else
|
191
|
+
send(type, *args, &block)
|
222
192
|
end
|
223
|
-
|
224
|
-
options[:class] = "#{key} #{options[:class]}".strip
|
225
|
-
@template.send(:"#{type}_tag", value, options)
|
226
193
|
end
|
227
194
|
|
228
195
|
# Creates an error tag based on the given attribute, only when the attribute
|
@@ -304,6 +271,8 @@ module SimpleForm
|
|
304
271
|
when /password/ then :password
|
305
272
|
when /time_zone/ then :time_zone
|
306
273
|
when /country/ then :country
|
274
|
+
when /email/ then :email
|
275
|
+
when /url/ then :url
|
307
276
|
end
|
308
277
|
|
309
278
|
match || input_type || file_method? || :string
|
data/lib/simple_form/inputs.rb
CHANGED
@@ -6,7 +6,8 @@ module SimpleForm
|
|
6
6
|
autoload :DateTimeInput, 'simple_form/inputs/date_time_input'
|
7
7
|
autoload :HiddenInput, 'simple_form/inputs/hidden_input'
|
8
8
|
autoload :MappingInput, 'simple_form/inputs/mapping_input'
|
9
|
+
autoload :NumericInput, 'simple_form/inputs/numeric_input'
|
9
10
|
autoload :PriorityInput, 'simple_form/inputs/priority_input'
|
10
|
-
autoload :
|
11
|
+
autoload :StringInput, 'simple_form/inputs/string_input'
|
11
12
|
end
|
12
13
|
end
|
@@ -39,8 +39,7 @@ module SimpleForm
|
|
39
39
|
send(component)
|
40
40
|
end
|
41
41
|
content.compact!
|
42
|
-
|
43
|
-
html_safe(wrap(content))
|
42
|
+
wrap(content.join).html_safe
|
44
43
|
end
|
45
44
|
|
46
45
|
protected
|
@@ -65,17 +64,6 @@ module SimpleForm
|
|
65
64
|
html_options
|
66
65
|
end
|
67
66
|
|
68
|
-
# Ensure we make html safe only if it responds to it.
|
69
|
-
def html_safe(content)
|
70
|
-
if content.respond_to?(:html_safe)
|
71
|
-
content.html_safe
|
72
|
-
elsif content.respond_to?(:html_safe!)
|
73
|
-
content.html_safe!
|
74
|
-
else
|
75
|
-
content
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
67
|
# Lookup translations for the given namespace using I18n, based on object name,
|
80
68
|
# actual action and attribute name. Lookup priority as follows:
|
81
69
|
#
|
@@ -116,4 +104,4 @@ module SimpleForm
|
|
116
104
|
end
|
117
105
|
end
|
118
106
|
end
|
119
|
-
end
|
107
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module SimpleForm
|
2
2
|
module Inputs
|
3
|
-
|
4
|
-
class TextFieldInput < Base
|
3
|
+
class NumericInput < Base
|
5
4
|
def input
|
6
5
|
@builder.text_field(attribute_name, input_html_options)
|
7
6
|
end
|
8
7
|
|
9
8
|
def input_html_options
|
10
9
|
input_options = super
|
11
|
-
input_options[:
|
10
|
+
input_options[:class] = "numeric #{input_options[:class]}"
|
11
|
+
input_options[:size] ||= SimpleForm.default_input_size
|
12
12
|
input_options
|
13
13
|
end
|
14
14
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module SimpleForm
|
2
|
+
module Inputs
|
3
|
+
class StringInput < Base
|
4
|
+
def input
|
5
|
+
@builder.text_field(attribute_name, input_html_options)
|
6
|
+
end
|
7
|
+
|
8
|
+
def input_html_options
|
9
|
+
input_options = super
|
10
|
+
input_options[:class] = "string #{input_options[:class]}" unless input_type == :string
|
11
|
+
input_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
|
12
|
+
input_options[:maxlength] ||= limit if limit
|
13
|
+
input_options[:type] ||= input_type
|
14
|
+
input_options
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def limit
|
20
|
+
column && column.limit
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/simple_form/version.rb
CHANGED
@@ -34,17 +34,4 @@ class FormHelperTest < ActionView::TestCase
|
|
34
34
|
assert f.instance_of?(SimpleForm::FormBuilder)
|
35
35
|
end
|
36
36
|
end
|
37
|
-
|
38
|
-
test 'remote form for yields an instance of FormBuilder' do
|
39
|
-
simple_remote_form_for :user do |f|
|
40
|
-
assert f.instance_of?(SimpleForm::FormBuilder)
|
41
|
-
end
|
42
|
-
assert_select "form[onsubmit]"
|
43
|
-
end
|
44
|
-
|
45
|
-
test 'pass options to simple remote form' do
|
46
|
-
simple_remote_form_for :user, :url => '/account', :html => { :id => 'my_form' } do |f| end
|
47
|
-
assert_select 'form#my_form'
|
48
|
-
assert_select 'form[action=/account]'
|
49
|
-
end
|
50
37
|
end
|
data/test/form_builder_test.rb
CHANGED
@@ -79,12 +79,12 @@ class FormBuilderTest < ActionView::TestCase
|
|
79
79
|
|
80
80
|
test 'builder should use integer text field for integer columns' do
|
81
81
|
with_form_for @user, :age
|
82
|
-
assert_select 'form input#user_age.integer'
|
82
|
+
assert_select 'form input#user_age.numeric.integer'
|
83
83
|
end
|
84
84
|
|
85
85
|
test 'builder should generate decimal text field for decimal columns' do
|
86
86
|
with_form_for @user, :credit_limit
|
87
|
-
assert_select 'form input#user_credit_limit.decimal'
|
87
|
+
assert_select 'form input#user_credit_limit.numeric.decimal'
|
88
88
|
end
|
89
89
|
|
90
90
|
test 'builder should generate password fields for columns that matches password' do
|
@@ -102,6 +102,16 @@ class FormBuilderTest < ActionView::TestCase
|
|
102
102
|
assert_select 'form select#user_time_zone.time_zone'
|
103
103
|
end
|
104
104
|
|
105
|
+
test 'builder should generate email fields for columns that matches email' do
|
106
|
+
with_form_for @user, :email
|
107
|
+
assert_select 'form input#user_email.string.email'
|
108
|
+
end
|
109
|
+
|
110
|
+
test 'builder should generate url fields for columns that matches url' do
|
111
|
+
with_form_for @user, :url
|
112
|
+
assert_select 'form input#user_url.string.url'
|
113
|
+
end
|
114
|
+
|
105
115
|
test 'builder should generate date select for date columns' do
|
106
116
|
with_form_for @user, :born_at
|
107
117
|
assert_select 'form select#user_born_at_1i.date'
|
@@ -344,63 +354,13 @@ class FormBuilderTest < ActionView::TestCase
|
|
344
354
|
# BUTTONS
|
345
355
|
test 'builder should create buttons' do
|
346
356
|
with_button_for :post, :submit
|
347
|
-
assert_select 'form input
|
357
|
+
assert_select 'form input[type=submit][value=Save Post]'
|
348
358
|
end
|
349
359
|
|
350
|
-
test 'builder should create buttons for
|
360
|
+
test 'builder should create buttons for records' do
|
351
361
|
@user.new_record!
|
352
362
|
with_button_for @user, :submit
|
353
|
-
assert_select 'form input
|
354
|
-
end
|
355
|
-
|
356
|
-
test 'builder should create buttons for existing records' do
|
357
|
-
with_button_for @user, :submit
|
358
|
-
assert_select 'form input.update[type=submit][value=Update User]'
|
359
|
-
end
|
360
|
-
|
361
|
-
test 'builder should create buttons using human_name' do
|
362
|
-
@user.class.expects(:human_name).returns("Usuario")
|
363
|
-
with_button_for @user, :submit
|
364
|
-
assert_select 'form input[type=submit][value=Update Usuario]'
|
365
|
-
end
|
366
|
-
|
367
|
-
test 'builder should create object buttons with localized labels' do
|
368
|
-
store_translations(:en, :simple_form => { :buttons => {
|
369
|
-
:create => "Criar %{model}", :update => "Atualizar %{model}" }}) do
|
370
|
-
with_button_for @user, :submit
|
371
|
-
assert_select 'form input[type=submit][value=Atualizar User]'
|
372
|
-
|
373
|
-
@user.new_record!
|
374
|
-
with_button_for @user, :submit
|
375
|
-
assert_select 'form input[type=submit][value=Criar User]'
|
376
|
-
end
|
377
|
-
end
|
378
|
-
|
379
|
-
test 'builder should create non object buttons with localized labels' do
|
380
|
-
store_translations(:en, :simple_form => { :buttons => { :submit => "Enviar %{model}" }}) do
|
381
|
-
with_button_for :post, :submit
|
382
|
-
assert_select 'form input[type=submit][value=Enviar Post]'
|
383
|
-
end
|
384
|
-
end
|
385
|
-
|
386
|
-
test 'builder forwards first options as button text' do
|
387
|
-
with_button_for :post, :submit, "Send it!"
|
388
|
-
assert_select 'form input[type=submit][value=Send it!]'
|
389
|
-
end
|
390
|
-
|
391
|
-
test 'builder forwards label option as button text' do
|
392
|
-
with_button_for :post, :submit, :label => "Send it!"
|
393
|
-
assert_select 'form input[type=submit][value=Send it!]'
|
394
|
-
end
|
395
|
-
|
396
|
-
test 'builder forwards all options except label to button' do
|
397
|
-
with_button_for :post, :submit, :class => "cool", :id => "super"
|
398
|
-
assert_select 'form input#super.submit.cool[type=submit]'
|
399
|
-
end
|
400
|
-
|
401
|
-
test 'builder calls any button tag' do
|
402
|
-
with_button_for :post, :image_submit, "/image/foo/bar"
|
403
|
-
assert_select 'form input[src=/image/foo/bar][type=image]'
|
363
|
+
assert_select 'form input[type=submit][value=Create User]'
|
404
364
|
end
|
405
365
|
|
406
366
|
# ASSOCIATIONS
|
@@ -426,7 +386,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
426
386
|
end
|
427
387
|
end
|
428
388
|
|
429
|
-
assert_equal
|
389
|
+
assert_equal 3, calls
|
430
390
|
end
|
431
391
|
|
432
392
|
# ASSOCIATIONS - BELONGS TO
|
data/test/inputs_test.rb
CHANGED
@@ -47,14 +47,19 @@ class InputTest < ActionView::TestCase
|
|
47
47
|
assert_select 'input.decimal#user_age'
|
48
48
|
end
|
49
49
|
|
50
|
-
test 'input should
|
50
|
+
test 'input should use default text size for decimal attributes' do
|
51
|
+
with_input_for @user, :credit_limit, :decimal
|
52
|
+
assert_select 'input.decimal[size=50]'
|
53
|
+
end
|
54
|
+
|
55
|
+
test 'input should get maxlength from column definition for string attributes' do
|
51
56
|
with_input_for @user, :name, :string
|
52
57
|
assert_select 'input.string[maxlength=100]'
|
53
58
|
end
|
54
|
-
|
55
|
-
test 'input should get
|
56
|
-
with_input_for @user, :
|
57
|
-
assert_select 'input.
|
59
|
+
|
60
|
+
test 'input should get size from column definition for string attributes respecting maximum value' do
|
61
|
+
with_input_for @user, :name, :string
|
62
|
+
assert_select 'input.string[size=50]'
|
58
63
|
end
|
59
64
|
|
60
65
|
# MappingInput
|
data/test/support/models.rb
CHANGED
@@ -4,6 +4,8 @@ Column = Struct.new(:name, :type, :limit)
|
|
4
4
|
Association = Struct.new(:klass, :name, :macro, :options)
|
5
5
|
|
6
6
|
class Company < Struct.new(:id, :name)
|
7
|
+
extend ActiveModel::Naming
|
8
|
+
|
7
9
|
def self.all(options={})
|
8
10
|
all = (1..3).map{|i| Company.new(i, "Company #{i}")}
|
9
11
|
return [all.first] if options[:conditions].present?
|
@@ -23,12 +25,16 @@ class Company < Struct.new(:id, :name)
|
|
23
25
|
end
|
24
26
|
|
25
27
|
class Tag < Company
|
28
|
+
extend ActiveModel::Naming
|
29
|
+
|
26
30
|
def self.all(options={})
|
27
31
|
(1..3).map{|i| Tag.new(i, "Tag #{i}")}
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
31
35
|
class User < OpenStruct
|
36
|
+
extend ActiveModel::Naming
|
37
|
+
|
32
38
|
# Get rid of deprecation warnings
|
33
39
|
undef_method :id
|
34
40
|
|
@@ -40,7 +46,7 @@ class User < OpenStruct
|
|
40
46
|
@new_record || false
|
41
47
|
end
|
42
48
|
|
43
|
-
def company_attributes=(
|
49
|
+
def company_attributes=(*)
|
44
50
|
end
|
45
51
|
|
46
52
|
def column_for_attribute(attribute)
|
@@ -71,10 +77,6 @@ class User < OpenStruct
|
|
71
77
|
end
|
72
78
|
end
|
73
79
|
|
74
|
-
def self.human_name
|
75
|
-
"User"
|
76
|
-
end
|
77
|
-
|
78
80
|
def self.reflect_on_association(association)
|
79
81
|
case association
|
80
82
|
when :company
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease: false
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 0
|
9
|
-
- 4
|
10
|
-
version: 1.0.4
|
4
|
+
version: 1.1.0
|
11
5
|
platform: ruby
|
12
6
|
authors:
|
13
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -16,7 +10,7 @@ autorequire:
|
|
16
10
|
bindir: bin
|
17
11
|
cert_chain: []
|
18
12
|
|
19
|
-
date: 2010-
|
13
|
+
date: 2010-02-07 00:00:00 +01:00
|
20
14
|
default_executable:
|
21
15
|
dependencies: []
|
22
16
|
|
@@ -29,14 +23,15 @@ extensions: []
|
|
29
23
|
extra_rdoc_files:
|
30
24
|
- README.rdoc
|
31
25
|
files:
|
32
|
-
- generators/simple_form_install/USAGE
|
33
|
-
- generators/simple_form_install/simple_form_install_generator.rb
|
34
|
-
- generators/simple_form_install/templates/simple_form.rb
|
35
26
|
- init.rb
|
27
|
+
- lib/generators/simple_form_install/USAGE
|
28
|
+
- lib/generators/simple_form_install/simple_form_install_generator.rb
|
29
|
+
- lib/generators/simple_form_install/templates/_form.html.erb
|
30
|
+
- lib/generators/simple_form_install/templates/en.yml
|
31
|
+
- lib/generators/simple_form_install/templates/simple_form.rb
|
36
32
|
- lib/simple_form.rb
|
37
33
|
- lib/simple_form/action_view_extensions/builder.rb
|
38
34
|
- lib/simple_form/action_view_extensions/form_helper.rb
|
39
|
-
- lib/simple_form/action_view_extensions/instance_tag.rb
|
40
35
|
- lib/simple_form/components.rb
|
41
36
|
- lib/simple_form/components/errors.rb
|
42
37
|
- lib/simple_form/components/hints.rb
|
@@ -51,29 +46,12 @@ files:
|
|
51
46
|
- lib/simple_form/inputs/date_time_input.rb
|
52
47
|
- lib/simple_form/inputs/hidden_input.rb
|
53
48
|
- lib/simple_form/inputs/mapping_input.rb
|
49
|
+
- lib/simple_form/inputs/numeric_input.rb
|
54
50
|
- lib/simple_form/inputs/priority_input.rb
|
55
|
-
- lib/simple_form/inputs/
|
56
|
-
- lib/simple_form/locale/en.yml
|
51
|
+
- lib/simple_form/inputs/string_input.rb
|
57
52
|
- lib/simple_form/map_type.rb
|
58
53
|
- lib/simple_form/version.rb
|
59
54
|
- README.rdoc
|
60
|
-
- test/action_view_extensions/builder_test.rb
|
61
|
-
- test/action_view_extensions/form_helper_test.rb
|
62
|
-
- test/components/error_test.rb
|
63
|
-
- test/components/hint_test.rb
|
64
|
-
- test/components/label_test.rb
|
65
|
-
- test/form_builder_test.rb
|
66
|
-
- test/inputs_test.rb
|
67
|
-
- test/simple_form_test.rb
|
68
|
-
- test/support/country_select/init.rb
|
69
|
-
- test/support/country_select/install.rb
|
70
|
-
- test/support/country_select/lib/country_select.rb
|
71
|
-
- test/support/country_select/uninstall.rb
|
72
|
-
- test/support/misc_helpers.rb
|
73
|
-
- test/support/mock_controller.rb
|
74
|
-
- test/support/mock_response.rb
|
75
|
-
- test/support/models.rb
|
76
|
-
- test/test_helper.rb
|
77
55
|
has_rdoc: true
|
78
56
|
homepage: http://github.com/plataformatec/simple_form
|
79
57
|
licenses: []
|
@@ -84,27 +62,21 @@ rdoc_options:
|
|
84
62
|
require_paths:
|
85
63
|
- lib
|
86
64
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
-
none: false
|
88
65
|
requirements:
|
89
66
|
- - ">="
|
90
67
|
- !ruby/object:Gem::Version
|
91
|
-
hash: 3
|
92
|
-
segments:
|
93
|
-
- 0
|
94
68
|
version: "0"
|
69
|
+
version:
|
95
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
-
none: false
|
97
71
|
requirements:
|
98
72
|
- - ">="
|
99
73
|
- !ruby/object:Gem::Version
|
100
|
-
hash: 3
|
101
|
-
segments:
|
102
|
-
- 0
|
103
74
|
version: "0"
|
75
|
+
version:
|
104
76
|
requirements: []
|
105
77
|
|
106
78
|
rubyforge_project:
|
107
|
-
rubygems_version: 1.3.
|
79
|
+
rubygems_version: 1.3.5
|
108
80
|
signing_key:
|
109
81
|
specification_version: 3
|
110
82
|
summary: Forms made easy!
|
@@ -1,19 +0,0 @@
|
|
1
|
-
class SimpleFormInstallGenerator < Rails::Generator::Base
|
2
|
-
|
3
|
-
def manifest
|
4
|
-
record do |m|
|
5
|
-
m.directory 'config/initializers'
|
6
|
-
m.template 'simple_form.rb', 'config/initializers/simple_form.rb'
|
7
|
-
|
8
|
-
m.directory 'config/locales'
|
9
|
-
m.template locale_file, 'config/locales/simple_form.en.yml'
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def locale_file
|
16
|
-
@locale_file ||= '../../../lib/simple_form/locale/en.yml'
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module SimpleForm
|
2
|
-
module ActionViewExtensions
|
3
|
-
module InstanceTag #:nodoc:
|
4
|
-
# Overwrite to_check_box_tag to make it available to work with :multiple => true
|
5
|
-
def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
|
6
|
-
options = options.stringify_keys
|
7
|
-
options["type"] = "checkbox"
|
8
|
-
options["value"] = checked_value
|
9
|
-
|
10
|
-
if options.has_key?("checked")
|
11
|
-
cv = options.delete "checked"
|
12
|
-
checked = cv == true || cv == "checked"
|
13
|
-
else
|
14
|
-
checked = self.class.check_box_checked?(value(object), checked_value)
|
15
|
-
end
|
16
|
-
options["checked"] = "checked" if checked
|
17
|
-
|
18
|
-
# The only part added to deal with multiple check box is this conditional.
|
19
|
-
if options["multiple"]
|
20
|
-
add_default_name_and_id_for_value(checked_value, options)
|
21
|
-
options.delete("multiple")
|
22
|
-
else
|
23
|
-
add_default_name_and_id(options)
|
24
|
-
end
|
25
|
-
|
26
|
-
hidden = tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
|
27
|
-
checkbox = tag("input", options)
|
28
|
-
|
29
|
-
result = hidden + checkbox
|
30
|
-
result.respond_to?(:html_safe) ? result.html_safe : result
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
ActionView::Helpers::InstanceTag.send :remove_method, :to_check_box_tag
|
37
|
-
ActionView::Helpers::InstanceTag.send :include, SimpleForm::ActionViewExtensions::InstanceTag
|