Skip to content

Commit

Permalink
Merge pull request formtastic#820 from twalpole/rails4
Browse files Browse the repository at this point in the history
Allow use with Rails 4 edge
  • Loading branch information
justinfrench committed Mar 7, 2012
2 parents 0f42a5c + 7c538a7 commit e4f3b04
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Appraisals
Expand Up @@ -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
2 changes: 1 addition & 1 deletion formtastic.gemspec
Expand Up @@ -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"])
Expand Down
7 changes: 7 additions & 0 deletions 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=>"../"
25 changes: 24 additions & 1 deletion lib/formtastic/inputs/boolean_input.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/formtastic/inputs/select_input.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/builder/semantic_fields_for_spec.rb
Expand Up @@ -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|
Expand Down
20 changes: 10 additions & 10 deletions spec/helpers/semantic_errors_helper_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions spec/inputs/custom_input_spec.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/inputs/hidden_input_spec.rb
Expand Up @@ -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|
Expand Down
26 changes: 25 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/support/custom_macros.rb
Expand Up @@ -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
Expand Down

0 comments on commit e4f3b04

Please sign in to comment.