simple_form 1.1.3 → 1.2.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.

@@ -10,9 +10,13 @@ Install the gem:
10
10
 
11
11
  sudo gem install simple_form
12
12
 
13
+ Add it to your Gemfile:
14
+
15
+ gem "simple_form"
16
+
13
17
  Run the generator:
14
18
 
15
- rails generate simple_form_install
19
+ rails generate simple_form:install
16
20
 
17
21
  And you are ready to go. Since this branch is aims Rails 3 support,
18
22
  if you want to use it with Rails 2.3 you should check this branch:
@@ -294,7 +298,7 @@ There are other options that can be configured through I18n API, such as require
294
298
 
295
299
  SimpleForm has several configuration values. You can read and change them in the initializer created by SimpleForm, so if you haven't executed the command below yet, please do:
296
300
 
297
- ruby script/generate simple_form_install
301
+ rails generate simple_form:install
298
302
 
299
303
  == TODO
300
304
 
@@ -0,0 +1,23 @@
1
+ module SimpleForm
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Copy SimpleForm default files"
5
+
6
+ def self.source_root
7
+ @_source_root = File.expand_path('../templates', __FILE__)
8
+ end
9
+
10
+ def copy_initializers
11
+ copy_file 'simple_form.rb', 'config/initializers/simple_form.rb'
12
+ end
13
+
14
+ def copy_locale_file
15
+ copy_file 'en.yml', 'config/locales/simple_form.en.yml'
16
+ end
17
+
18
+ def copy_scaffold_template
19
+ copy_file '_form.html.erb', 'lib/templates/erb/scaffold/_form.html.erb'
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ <%%= simple_form_for(@<%= singular_name %>) do |f| %>
2
+ <%% if @<%= singular_name %>.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%%= pluralize(@<%= singular_name %>.errors.count, "error") %> prohibited this <%= singular_name %> from being saved:</h2>
5
+
6
+ <ul>
7
+ <%% @<%= singular_name %>.errors.full_messages.each do |msg| %>
8
+ <li><%%= msg %></li>
9
+ <%% end %>
10
+ </ul>
11
+ </div>
12
+ <%% end %>
13
+
14
+ <div class="inputs">
15
+ <%- attributes.each do |attribute| -%>
16
+ <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
17
+ <%- end -%>
18
+ </div>
19
+
20
+ <div class="actions">
21
+ <%%= f.button :submit %>
22
+ </div>
23
+ <%% end %>
@@ -5,18 +5,16 @@ 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 = [
9
- # SimpleForm::Components::Wrapper, SimpleForm::Components::Label,
10
- # SimpleForm::Components::Input, SimpleForm::Components::Hint,
11
- # SimpleForm::Components::Error
12
- #]
13
-
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
17
-
18
- # You can wrap all inputs in a pre-defined tag. By default is nil.
19
- # config.wrapper_tag = nil
8
+ # config.components = [ :label, :input, :hint, :error ]
9
+
10
+ # Default tag used on hints.
11
+ # config.hint_tag = :span
12
+
13
+ # Default tag used on errors.
14
+ # config.error_tag = :span
15
+
16
+ # You can wrap all inputs in a pre-defined tag.
17
+ # config.wrapper_tag = :div
20
18
 
21
19
  # How the label text should be generated altogether with the required text.
22
20
  # config.label_text = lambda { |label, required| "#{required} #{label}" }
@@ -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 in hints.
12
+ # Default tag used on hints.
13
13
  mattr_accessor :hint_tag
14
14
  @@hint_tag = :span
15
15
 
16
- # Default tag used in errors.
16
+ # Default tag used on errors.
17
17
  mattr_accessor :error_tag
18
18
  @@error_tag = :span
19
19
 
@@ -30,15 +30,15 @@ module SimpleForm
30
30
  # item or an array of items.
31
31
  #
32
32
  def collection_radio(attribute, collection, value_method, text_method, options={}, html_options={})
33
- collection.inject('') do |result, item|
33
+ collection.map do |item|
34
34
  value = item.send value_method
35
35
  text = item.send text_method
36
36
 
37
37
  default_html_options = default_html_options_for_collection(item, value, options, html_options)
38
38
 
39
- result << radio_button(attribute, value, default_html_options) <<
40
- label("#{attribute}_#{value}", text, :class => "collection_radio")
41
- end.html_safe
39
+ radio_button(attribute, value, default_html_options) <<
40
+ label("#{attribute}_#{value}", text, :class => "collection_radio")
41
+ end.join.html_safe
42
42
  end
43
43
 
44
44
  # Creates a collection of check boxes for each item in the collection, associated
@@ -69,16 +69,16 @@ module SimpleForm
69
69
  # item or an array of items.
70
70
  #
71
71
  def collection_check_boxes(attribute, collection, value_method, text_method, options={}, html_options={})
72
- collection.inject('') do |result, item|
72
+ collection.map do |item|
73
73
  value = item.send value_method
74
74
  text = item.send text_method
75
75
 
76
76
  default_html_options = default_html_options_for_collection(item, value, options, html_options)
77
77
  default_html_options[:multiple] = true
78
78
 
79
- result << check_box(attribute, default_html_options, value, '') <<
80
- label("#{attribute}_#{value}", text, :class => "collection_check_boxes")
81
- end.html_safe
79
+ check_box(attribute, default_html_options, value, '') <<
80
+ label("#{attribute}_#{value}", text, :class => "collection_check_boxes")
81
+ end.join.html_safe
82
82
  end
83
83
 
84
84
  # Wrapper for using simple form inside a default rails form.
@@ -53,7 +53,6 @@ module SimpleForm
53
53
  end
54
54
 
55
55
  # First check human attribute name and then labels.
56
- # TODO Remove me in Rails > 2.3.5
57
56
  def label_translation #:nodoc:
58
57
  default = if object.class.respond_to?(:human_attribute_name)
59
58
  object.class.human_attribute_name(reflection_or_attribute_name.to_s)
@@ -14,7 +14,7 @@ module SimpleForm
14
14
  end
15
15
 
16
16
  def wrapper_html_options
17
- html_options_for(:wrapper, input_type, required_class)
17
+ html_options_for(:wrapper, "input", input_type, required_class)
18
18
  end
19
19
  end
20
20
  end
@@ -34,7 +34,7 @@ module SimpleForm
34
34
  end
35
35
 
36
36
  def render
37
- content = SimpleForm.components.map do |component|
37
+ content = components_list.map do |component|
38
38
  next if options[component] == false
39
39
  send(component)
40
40
  end
@@ -44,8 +44,18 @@ module SimpleForm
44
44
 
45
45
  protected
46
46
 
47
+ def components_list
48
+ SimpleForm.components
49
+ end
50
+
47
51
  def attribute_required?
48
- options[:required] != false
52
+ if options.key?(:required)
53
+ options[:required]
54
+ elsif defined?(object.class.validators_on)
55
+ object.class.validators_on(attribute_name).any? { |v| v.kind == :presence }
56
+ else
57
+ true
58
+ end
49
59
  end
50
60
 
51
61
  def required_class
@@ -1,3 +1,3 @@
1
1
  module SimpleForm
2
- VERSION = "1.1.3".freeze
2
+ VERSION = "1.2.0".freeze
3
3
  end
@@ -94,13 +94,27 @@ class LabelTest < ActionView::TestCase
94
94
  with_label_for @user, :created_at, :datetime
95
95
  assert_select 'label.datetime'
96
96
  end
97
+
98
+ test 'label should obtain required from ActiveModel::Validations when it is included' do
99
+ with_label_for @validating_user, :name, :string
100
+ assert_select 'label.required'
101
+ with_label_for @validating_user, :status, :string
102
+ assert_select 'label.optional'
103
+ end
104
+
105
+ test 'label should allow overriding required when ActiveModel::Validations is included' do
106
+ with_label_for @validating_user, :name, :string, :required => false
107
+ assert_select 'label.optional'
108
+ with_label_for @validating_user, :status, :string, :required => true
109
+ assert_select 'label.required'
110
+ end
97
111
 
98
- test 'label should be required by default' do
112
+ test 'label should be required by default when ActiveModel::Validations is not included' do
99
113
  with_label_for @user, :name, :string
100
114
  assert_select 'label.required'
101
115
  end
102
116
 
103
- test 'label should be able to disable required' do
117
+ test 'label should be able to disable required when ActiveModel::Validations is not included' do
104
118
  with_label_for @user, :name, :string, :required => false
105
119
  assert_no_select 'label.required'
106
120
  end
@@ -229,12 +229,27 @@ class FormBuilderTest < ActionView::TestCase
229
229
  assert_select 'span.error#cool', "can't be blank"
230
230
  end
231
231
 
232
- test 'builder input should be required by default' do
232
+ # REQUIRED AND PRESENCE VALIDATION
233
+ test 'builder input should obtain required from ActiveModel::Validations when it is included' do
234
+ with_form_for @validating_user, :name
235
+ assert_select 'input.required#validating_user_name'
236
+ with_form_for @validating_user, :status
237
+ assert_select 'input.optional#validating_user_status'
238
+ end
239
+
240
+ test 'builder input should allow overriding required when ActiveModel::Validations is included' do
241
+ with_form_for @validating_user, :name, :required => false
242
+ assert_select 'input.optional#validating_user_name'
243
+ with_form_for @validating_user, :status, :required => true
244
+ assert_select 'input.required#validating_user_status'
245
+ end
246
+
247
+ test 'builder input should be required by default when ActiveModel::Validations is not included' do
233
248
  with_form_for @user, :name
234
249
  assert_select 'input.required#user_name'
235
250
  end
236
-
237
- test 'builder input should allow disabling required' do
251
+
252
+ test 'builder input should allow disabling required when ActiveModel::Validations is not included' do
238
253
  with_form_for @user, :name, :required => false
239
254
  assert_no_select 'input.required'
240
255
  assert_select 'input.optional#user_name'
@@ -252,10 +267,10 @@ class FormBuilderTest < ActionView::TestCase
252
267
  test 'builder wrapping tag adds default css classes' do
253
268
  swap SimpleForm, :wrapper_tag => :p do
254
269
  with_form_for @user, :name
255
- assert_select 'form p.required.string'
270
+ assert_select 'form p.input.required.string'
256
271
 
257
272
  with_form_for @user, :age, :required => false
258
- assert_select 'form p.optional.integer'
273
+ assert_select 'form p.input.optional.integer'
259
274
  end
260
275
  end
261
276
 
@@ -333,9 +348,14 @@ class FormBuilderTest < ActionView::TestCase
333
348
  assert_select 'label.string[for=user_name]', /Name/
334
349
  end
335
350
 
351
+ test 'builder should add a required class to label if the attribute is required' do
352
+ with_label_for @validating_user, :name
353
+ assert_select 'label.string[for=validating_user_name]', /Name/
354
+ end
355
+
336
356
  test 'builder should allow passing options to label tag' do
337
357
  with_label_for @user, :name, :label => 'My label', :id => 'name_label'
338
- assert_select 'label.string.required#name_label', /My label/
358
+ assert_select 'label.string#name_label', /My label/
339
359
  end
340
360
 
341
361
  test 'builder should fallback to default label when string is given' do
@@ -5,6 +5,10 @@ class MockController
5
5
  @action_name || "edit"
6
6
  end
7
7
 
8
+ def _router
9
+ self
10
+ end
11
+
8
12
  def url_for(*args)
9
13
  "http://example.com"
10
14
  end
@@ -100,3 +100,8 @@ class User < OpenStruct
100
100
  }
101
101
  end
102
102
  end
103
+
104
+ class ValidatingUser < User
105
+ include ActiveModel::Validations
106
+ validates :name, :presence => true
107
+ end
@@ -55,6 +55,13 @@ class ActionView::TestCase
55
55
  :description => 'Hello!',
56
56
  :created_at => Time.now
57
57
  }.merge(options))
58
+
59
+ @validating_user = ValidatingUser.new({
60
+ :id => 1,
61
+ :name => 'New in Simple Form!',
62
+ :description => 'Hello!',
63
+ :created_at => Time.now
64
+ }.merge(options))
58
65
  end
59
66
 
60
67
  def protect_against_forgery?
@@ -66,4 +73,5 @@ class ActionView::TestCase
66
73
  end
67
74
  alias :users_path :user_path
68
75
  alias :super_user_path :user_path
76
+ alias :validating_user_path :user_path
69
77
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 1
8
- - 3
9
- version: 1.1.3
7
+ - 2
8
+ - 0
9
+ version: 1.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Jos\xC3\xA9 Valim"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-04-03 00:00:00 +02:00
18
+ date: 2010-05-24 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,11 +29,11 @@ extra_rdoc_files:
29
29
  - README.rdoc
30
30
  files:
31
31
  - init.rb
32
- - lib/generators/simple_form_install/USAGE
33
- - lib/generators/simple_form_install/simple_form_install_generator.rb
34
- - lib/generators/simple_form_install/templates/_form.html.erb
35
- - lib/generators/simple_form_install/templates/en.yml
36
- - lib/generators/simple_form_install/templates/simple_form.rb
32
+ - lib/generators/simple_form/USAGE
33
+ - lib/generators/simple_form/install_generator.rb
34
+ - lib/generators/simple_form/templates/_form.html.erb
35
+ - lib/generators/simple_form/templates/en.yml
36
+ - lib/generators/simple_form/templates/simple_form.rb
37
37
  - lib/simple_form.rb
38
38
  - lib/simple_form/action_view_extensions/builder.rb
39
39
  - lib/simple_form/action_view_extensions/form_helper.rb
@@ -1,19 +0,0 @@
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
@@ -1,11 +0,0 @@
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 %>