Permalink
Browse files

Added a with_config method for specs.

Wraps up the common pattern of changing a config value at the start of a spec but wanting to return it to it's previous state (so as to not interfere with other specs).

Before

  it "should whatever" do
    old_value = ::Formtastic::SemanticFormBuilder.some_config_option
    ::Formtastic::SemanticFormBuilder.some_config_option = new_value
    ...
    ::Formtastic::SemanticFormBuilder.some_config_option = old_value
  end

After

  it "should whatever" do
    with_config :some_config_option, new_value
      ...
    end
  end

Used it in a few places already, more to come.
  • Loading branch information...
1 parent 8668583 commit ffa9e37e1ab3e11c25c2b649c9e0f5b1a47ea213 @justinfrench committed Dec 2, 2009
Showing with 36 additions and 32 deletions.
  1. +24 −24 spec/commit_button_spec.rb
  2. +5 −8 spec/input_spec.rb
  3. +7 −0 spec/spec_helper.rb
View
@@ -46,42 +46,42 @@
describe "its accesskey" do
it 'should allow nil default' do
- ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey.should == nil
- output_buffer.should_not have_tag('li.commit input[@accesskey]')
+ with_config :default_commit_button_accesskey, nil do
+ output_buffer.should_not have_tag('li.commit input[@accesskey]')
+ end
end
it 'should use the default if set' do
- ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey = 's'
- @new_post.stub!(:new_record?).and_return(false)
- semantic_form_for(@new_post) do |builder|
- concat(builder.commit_button('text', :button_html => {}))
+ with_config :default_commit_button_accesskey, 's' do
+ @new_post.stub!(:new_record?).and_return(false)
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.commit_button('text', :button_html => {}))
+ end
+ output_buffer.should have_tag('li.commit input[@accesskey="s"]')
end
- output_buffer.should have_tag('li.commit input[@accesskey="s"]')
end
it 'should use the value set in options over the default' do
- ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey = 's'
- @new_post.stub!(:new_record?).and_return(false)
- semantic_form_for(@new_post) do |builder|
- concat(builder.commit_button('text', :accesskey => 'o'))
+ with_config :default_commit_button_accesskey, 's' do
+ @new_post.stub!(:new_record?).and_return(false)
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.commit_button('text', :accesskey => 'o'))
+ end
+ output_buffer.should_not have_tag('li.commit input[@accesskey="s"]')
+ output_buffer.should have_tag('li.commit input[@accesskey="o"]')
end
- output_buffer.should_not have_tag('li.commit input[@accesskey="s"]')
- output_buffer.should have_tag('li.commit input[@accesskey="o"]')
end
it 'should use the value set in button_html over options' do
- ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey = 's'
- @new_post.stub!(:new_record?).and_return(false)
- semantic_form_for(@new_post) do |builder|
- concat(builder.commit_button('text', :accesskey => 'o', :button_html => {:accesskey => 't'}))
+ with_config :default_commit_button_accesskey, 's' do
+ @new_post.stub!(:new_record?).and_return(false)
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.commit_button('text', :accesskey => 'o', :button_html => {:accesskey => 't'}))
+ end
+ output_buffer.should_not have_tag('li.commit input[@accesskey="s"]')
+ output_buffer.should_not have_tag('li.commit input[@accesskey="o"]')
+ output_buffer.should have_tag('li.commit input[@accesskey="t"]')
end
- output_buffer.should_not have_tag('li.commit input[@accesskey="s"]')
- output_buffer.should_not have_tag('li.commit input[@accesskey="o"]')
- output_buffer.should have_tag('li.commit input[@accesskey="t"]')
- end
-
- after do
- ::Formtastic::SemanticFormBuilder.default_commit_button_accesskey = nil
end
end
View
@@ -413,15 +413,12 @@ def should_be_required(options)
describe 'and label_str_method is :capitalize' do
it 'should capitalize method name, passing it down to the label tag' do
- old_value = ::Formtastic::SemanticFormBuilder.label_str_method
- ::Formtastic::SemanticFormBuilder.label_str_method = :capitalize
-
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
- concat(builder.input(:meta_description))
+ with_config :label_str_method, :capitalize do
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
+ concat(builder.input(:meta_description))
+ end
+ output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/)
end
-
- output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/)
- ::Formtastic::SemanticFormBuilder.label_str_method = old_value
end
end
end
View
@@ -200,6 +200,13 @@ def protect_against_forgery?
end
end
+ def with_config(config_method_name, value, &block)
+ old_value = ::Formtastic::SemanticFormBuilder.send(config_method_name)
+ ::Formtastic::SemanticFormBuilder.send(:"#{config_method_name}=", value)
+ yield
+ ::Formtastic::SemanticFormBuilder.send(:"#{config_method_name}=", old_value)
+ end
+
end
::ActiveSupport::Deprecation.silenced = false

0 comments on commit ffa9e37

Please sign in to comment.