Skip to content
This repository
Browse code

Merge pull request #818 from ZachBeta/master

Added a custom hash to the collections section of the readme
  • Loading branch information...
commit 1303ccf241e99aecc306fad307b097ad5555ea21 2 parents c919c2f + f9e7ecd
Justin French authored twalpole committed
4 Appraisals
@@ -9,3 +9,7 @@ end
9 9 appraise 'rails-3.2' do
10 10 gem 'rails', '~> 3.2.0'
11 11 end
  12 +
  13 +appraise 'rails-4' do
  14 + gem 'rails', :git => 'git://github.com/rails/rails.git'
  15 +end
1  README.textile
Source Rendered
@@ -329,6 +329,7 @@ Many inputs provide a collection of options to choose from (like @:select@, @:ra
329 329 f.input :author, :as => :radio, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
330 330 f.input :author, :as => :radio, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
331 331 f.input :admin, :as => :radio, :collection => ["Yes!", "No"]
  332 + f.input :book_id, :as => :select, :collection => Hash[Book.all.map{|b| [b.name,b.id]}]
332 333 </pre>
333 334
334 335
2  formtastic.gemspec
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
23 23 s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
24 24 s.rubygems_version = %q{1.3.6}
25 25
26   - s.add_dependency(%q<actionpack>, ["~> 3.0"])
  26 + s.add_dependency(%q<actionpack>, [">= 3.0"])
27 27
28 28 s.add_development_dependency(%q<rspec-rails>, ["~> 2.8.0"])
29 29 s.add_development_dependency(%q<rspec_tag_matchers>, [">= 1.0.0"])
7 gemfiles/rails-4.gemfile
... ... @@ -0,0 +1,7 @@
  1 +# This file was generated by Appraisal
  2 +
  3 +source :rubygems
  4 +
  5 +gem "rails", :git=>"git://github.com/rails/rails.git"
  6 +
  7 +gemspec :path=>"../"
25 lib/formtastic/inputs/boolean_input.rb
@@ -97,9 +97,32 @@ def input_html_options_name
97 97 end
98 98
99 99 def checked?
100   - object && ActionView::Helpers::InstanceTag.check_box_checked?(object.send(method), checked_value)
  100 + if defined? ActionView::Helpers::InstanceTag
  101 + object && ActionView::Helpers::InstanceTag.check_box_checked?(object.send(method), checked_value)
  102 + else
  103 + object && boolean_checked?(object.send(method), checked_value)
  104 + end
101 105 end
  106 +
  107 + private
102 108
  109 + def boolean_checked?(value, checked_value)
  110 + case value
  111 + when TrueClass, FalseClass
  112 + value
  113 + when NilClass
  114 + false
  115 + when Integer
  116 + value != 0
  117 + when String
  118 + value == checked_value
  119 + when Array
  120 + value.include?(checked_value)
  121 + else
  122 + value.to_i != 0
  123 + end
  124 + end
  125 +
103 126 end
104 127 end
105 128 end
2  lib/formtastic/inputs/select_input.rb
@@ -195,7 +195,7 @@ def input_options
195 195 end
196 196
197 197 def input_html_options
198   - extra_input_html_options.merge(super)
  198 + extra_input_html_options.merge(super.reject {|k,v| k==:name && v.nil?} )
199 199 end
200 200
201 201 def extra_input_html_options
2  spec/builder/semantic_fields_for_spec.rb
@@ -101,7 +101,7 @@
101 101
102 102 it 'should render errors on the nested inputs' do
103 103 @errors = mock('errors')
104   - @errors.stub!(:[]).with(:login).and_return(['oh noes'])
  104 + @errors.stub!(:[]).with(errors_matcher(:login)).and_return(['oh noes'])
105 105 @bob.stub!(:errors).and_return(@errors)
106 106
107 107 concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
20 spec/helpers/semantic_errors_helper_spec.rb
@@ -17,7 +17,7 @@
17 17
18 18 describe 'when there is only one error on base' do
19 19 before do
20   - @errors.stub!(:[]).with(:base).and_return(@base_error)
  20 + @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_error)
21 21 end
22 22
23 23 it 'should render an unordered list' do
@@ -29,7 +29,7 @@
29 29
30 30 describe 'when there is more than one error on base' do
31 31 before do
32   - @errors.stub!(:[]).with(:base).and_return(@base_errors)
  32 + @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_errors)
33 33 end
34 34
35 35 it 'should render an unordered list' do
@@ -44,8 +44,8 @@
44 44
45 45 describe 'when there are errors on title' do
46 46 before do
47   - @errors.stub!(:[]).with(:title).and_return(@title_errors)
48   - @errors.stub!(:[]).with(:base).and_return([])
  47 + @errors.stub!(:[]).with(errors_matcher(:title)).and_return(@title_errors)
  48 + @errors.stub!(:[]).with(errors_matcher(:base)).and_return([])
49 49 end
50 50
51 51 it 'should render an unordered list' do
@@ -58,8 +58,8 @@
58 58
59 59 describe 'when there are errors on title and base' do
60 60 before do
61   - @errors.stub!(:[]).with(:title).and_return(@title_errors)
62   - @errors.stub!(:[]).with(:base).and_return(@base_error)
  61 + @errors.stub!(:[]).with(errors_matcher(:title)).and_return(@title_errors)
  62 + @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_error)
63 63 end
64 64
65 65 it 'should render an unordered list' do
@@ -73,8 +73,8 @@
73 73
74 74 describe 'when there are no errors' do
75 75 before do
76   - @errors.stub!(:[]).with(:title).and_return(nil)
77   - @errors.stub!(:[]).with(:base).and_return(nil)
  76 + @errors.stub!(:[]).with(errors_matcher(:title)).and_return(nil)
  77 + @errors.stub!(:[]).with(errors_matcher(:base)).and_return(nil)
78 78 end
79 79
80 80 it 'should return nil' do
@@ -86,7 +86,7 @@
86 86
87 87 describe 'when there is one error on base and options with class is passed' do
88 88 before do
89   - @errors.stub!(:[]).with(:base).and_return(@base_error)
  89 + @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_error)
90 90 end
91 91
92 92 it 'should render an unordered list with given class' do
@@ -98,7 +98,7 @@
98 98
99 99 describe 'when :base is passed in as an argument' do
100 100 before do
101   - @errors.stub!(:[]).with(:base).and_return(@base_error)
  101 + @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_error)
102 102 end
103 103
104 104 it 'should ignore :base and only render base errors once' do
7 spec/inputs/custom_input_spec.rb
@@ -11,8 +11,11 @@ def input_args
11 11 @method = :title
12 12 @options = {}
13 13 @proc = Proc.new {}
14   - @builder = Formtastic::FormBuilder.new(@object_name, @object, @template, @options, @proc)
15   -
  14 + if Rails::VERSION::MAJOR == 4
  15 + @builder = Formtastic::FormBuilder.new(@object_name, @object, @template, @options)
  16 + else
  17 + @builder = Formtastic::FormBuilder.new(@object_name, @object, @template, @options, @proc)
  18 + end
16 19 [@builder, @template, @object, @object_name, @method, @options]
17 20 end
18 21
2  spec/inputs/hidden_input_spec.rb
@@ -52,7 +52,7 @@
52 52
53 53 it "should not render inline errors" do
54 54 @errors = mock('errors')
55   - @errors.stub!(:[]).with(:secret).and_return(["foo", "bah"])
  55 + @errors.stub!(:[]).with(errors_matcher(:secret)).and_return(["foo", "bah"])
56 56 @new_post.stub!(:errors).and_return(@errors)
57 57
58 58 concat(semantic_form_for(@new_post) do |builder|
26 spec/spec_helper.rb
@@ -413,7 +413,31 @@ def with_config(config_method_name, value, &block)
413 413 yield
414 414 Formtastic::FormBuilder.send(:"#{config_method_name}=", old_value)
415 415 end
416   -
  416 +
  417 + class ToSMatcher
  418 + def initialize(str)
  419 + @str=str.to_s
  420 + end
  421 +
  422 + def matches?(value)
  423 + value.to_s==@str
  424 + end
  425 +
  426 + def failure_message_for_should
  427 + "Expected argument that converted to #{@str}"
  428 + end
  429 + end
  430 +
  431 + def errors_matcher(method)
  432 + # In edge rails (Rails 4) tags store method_name as a string and index the errors object using a string
  433 + # therefore allow stubs to match on either string or symbol. The errors object calls to_sym on all index
  434 + # accesses so @object.errors[:abc] is equivalent to @object.errors["abc"]
  435 + if Rails::VERSION::MAJOR == 4
  436 + ToSMatcher.new(method)
  437 + else
  438 + method
  439 + end
  440 + end
417 441 end
418 442
419 443 ::ActiveSupport::Deprecation.silenced = false
4 spec/support/custom_macros.rb
@@ -177,9 +177,9 @@ def it_should_apply_error_logic_for_input_type(type)
177 177 before do
178 178 @title_errors = ['must not be blank', 'must be longer than 10 characters', 'must be awesome']
179 179 @errors = mock('errors')
180   - @errors.stub!(:[]).with(:title).and_return(@title_errors)
  180 + @errors.stub!(:[]).with(errors_matcher(:title)).and_return(@title_errors)
181 181 Formtastic::FormBuilder.file_metadata_suffixes.each do |suffix|
182   - @errors.stub!(:[]).with("title_#{suffix}".to_sym).and_return(nil)
  182 + @errors.stub!(:[]).with(errors_matcher("title_#{suffix}".to_sym)).and_return(nil)
183 183 end
184 184 @new_post.stub!(:errors).and_return(@errors)
185 185 end

0 comments on commit 1303ccf

Please sign in to comment.
Something went wrong with that request. Please try again.