Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #820 from twalpole/rails4

Allow use with Rails 4 edge
  • Loading branch information...
commit e4f3b049858e5f4f7adfc7e2ff3d9e141ff5cecb 2 parents 0f42a5c + 7c538a7
@justinfrench authored
View
4 Appraisals
@@ -9,3 +9,7 @@ end
appraise 'rails-3.2' do
gem 'rails', '~> 3.2.0'
end
+
+appraise 'rails-4' do
+ gem 'rails', :git => 'git://github.com/rails/rails.git'
+end
View
2  formtastic.gemspec
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.rubygems_version = %q{1.3.6}
- s.add_dependency(%q<actionpack>, ["~> 3.0"])
+ s.add_dependency(%q<actionpack>, [">= 3.0"])
s.add_development_dependency(%q<rspec-rails>, ["~> 2.8.0"])
s.add_development_dependency(%q<rspec_tag_matchers>, [">= 1.0.0"])
View
7 gemfiles/rails-4.gemfile
@@ -0,0 +1,7 @@
+# This file was generated by Appraisal
+
+source :rubygems
+
+gem "rails", :git=>"git://github.com/rails/rails.git"
+
+gemspec :path=>"../"
View
25 lib/formtastic/inputs/boolean_input.rb
@@ -97,9 +97,32 @@ def input_html_options_name
end
def checked?
- object && ActionView::Helpers::InstanceTag.check_box_checked?(object.send(method), checked_value)
+ if defined? ActionView::Helpers::InstanceTag
+ object && ActionView::Helpers::InstanceTag.check_box_checked?(object.send(method), checked_value)
+ else
+ object && boolean_checked?(object.send(method), checked_value)
+ end
end
+
+ private
+ def boolean_checked?(value, checked_value)
+ case value
+ when TrueClass, FalseClass
+ value
+ when NilClass
+ false
+ when Integer
+ value != 0
+ when String
+ value == checked_value
+ when Array
+ value.include?(checked_value)
+ else
+ value.to_i != 0
+ end
+ end
+
end
end
end
View
2  lib/formtastic/inputs/select_input.rb
@@ -195,7 +195,7 @@ def input_options
end
def input_html_options
- extra_input_html_options.merge(super)
+ extra_input_html_options.merge(super.reject {|k,v| k==:name && v.nil?} )
end
def extra_input_html_options
View
2  spec/builder/semantic_fields_for_spec.rb
@@ -101,7 +101,7 @@
it 'should render errors on the nested inputs' do
@errors = mock('errors')
- @errors.stub!(:[]).with(:login).and_return(['oh noes'])
+ @errors.stub!(:[]).with(errors_matcher(:login)).and_return(['oh noes'])
@bob.stub!(:errors).and_return(@errors)
concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
View
20 spec/helpers/semantic_errors_helper_spec.rb
@@ -17,7 +17,7 @@
describe 'when there is only one error on base' do
before do
- @errors.stub!(:[]).with(:base).and_return(@base_error)
+ @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_error)
end
it 'should render an unordered list' do
@@ -29,7 +29,7 @@
describe 'when there is more than one error on base' do
before do
- @errors.stub!(:[]).with(:base).and_return(@base_errors)
+ @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_errors)
end
it 'should render an unordered list' do
@@ -44,8 +44,8 @@
describe 'when there are errors on title' do
before do
- @errors.stub!(:[]).with(:title).and_return(@title_errors)
- @errors.stub!(:[]).with(:base).and_return([])
+ @errors.stub!(:[]).with(errors_matcher(:title)).and_return(@title_errors)
+ @errors.stub!(:[]).with(errors_matcher(:base)).and_return([])
end
it 'should render an unordered list' do
@@ -58,8 +58,8 @@
describe 'when there are errors on title and base' do
before do
- @errors.stub!(:[]).with(:title).and_return(@title_errors)
- @errors.stub!(:[]).with(:base).and_return(@base_error)
+ @errors.stub!(:[]).with(errors_matcher(:title)).and_return(@title_errors)
+ @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_error)
end
it 'should render an unordered list' do
@@ -73,8 +73,8 @@
describe 'when there are no errors' do
before do
- @errors.stub!(:[]).with(:title).and_return(nil)
- @errors.stub!(:[]).with(:base).and_return(nil)
+ @errors.stub!(:[]).with(errors_matcher(:title)).and_return(nil)
+ @errors.stub!(:[]).with(errors_matcher(:base)).and_return(nil)
end
it 'should return nil' do
@@ -86,7 +86,7 @@
describe 'when there is one error on base and options with class is passed' do
before do
- @errors.stub!(:[]).with(:base).and_return(@base_error)
+ @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_error)
end
it 'should render an unordered list with given class' do
@@ -98,7 +98,7 @@
describe 'when :base is passed in as an argument' do
before do
- @errors.stub!(:[]).with(:base).and_return(@base_error)
+ @errors.stub!(:[]).with(errors_matcher(:base)).and_return(@base_error)
end
it 'should ignore :base and only render base errors once' do
View
7 spec/inputs/custom_input_spec.rb
@@ -11,8 +11,11 @@ def input_args
@method = :title
@options = {}
@proc = Proc.new {}
- @builder = Formtastic::FormBuilder.new(@object_name, @object, @template, @options, @proc)
-
+ if Rails::VERSION::MAJOR == 4
+ @builder = Formtastic::FormBuilder.new(@object_name, @object, @template, @options)
+ else
+ @builder = Formtastic::FormBuilder.new(@object_name, @object, @template, @options, @proc)
+ end
[@builder, @template, @object, @object_name, @method, @options]
end
View
2  spec/inputs/hidden_input_spec.rb
@@ -52,7 +52,7 @@
it "should not render inline errors" do
@errors = mock('errors')
- @errors.stub!(:[]).with(:secret).and_return(["foo", "bah"])
+ @errors.stub!(:[]).with(errors_matcher(:secret)).and_return(["foo", "bah"])
@new_post.stub!(:errors).and_return(@errors)
concat(semantic_form_for(@new_post) do |builder|
View
26 spec/spec_helper.rb
@@ -413,7 +413,31 @@ def with_config(config_method_name, value, &block)
yield
Formtastic::FormBuilder.send(:"#{config_method_name}=", old_value)
end
-
+
+ class ToSMatcher
+ def initialize(str)
+ @str=str.to_s
+ end
+
+ def matches?(value)
+ value.to_s==@str
+ end
+
+ def failure_message_for_should
+ "Expected argument that converted to #{@str}"
+ end
+ end
+
+ def errors_matcher(method)
+ # In edge rails (Rails 4) tags store method_name as a string and index the errors object using a string
+ # therefore allow stubs to match on either string or symbol. The errors object calls to_sym on all index
+ # accesses so @object.errors[:abc] is equivalent to @object.errors["abc"]
+ if Rails::VERSION::MAJOR == 4
+ ToSMatcher.new(method)
+ else
+ method
+ end
+ end
end
::ActiveSupport::Deprecation.silenced = false
View
4 spec/support/custom_macros.rb
@@ -177,9 +177,9 @@ def it_should_apply_error_logic_for_input_type(type)
before do
@title_errors = ['must not be blank', 'must be longer than 10 characters', 'must be awesome']
@errors = mock('errors')
- @errors.stub!(:[]).with(:title).and_return(@title_errors)
+ @errors.stub!(:[]).with(errors_matcher(:title)).and_return(@title_errors)
Formtastic::FormBuilder.file_metadata_suffixes.each do |suffix|
- @errors.stub!(:[]).with("title_#{suffix}".to_sym).and_return(nil)
+ @errors.stub!(:[]).with(errors_matcher("title_#{suffix}".to_sym)).and_return(nil)
end
@new_post.stub!(:errors).and_return(@errors)
end
Please sign in to comment.
Something went wrong with that request. Please try again.