Created
September 30, 2011 16:49
-
-
Save jodosha/1254329 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The first implementation was too long (8 LOCs) and referenced `options[:class]` 3 times (duplication). | |
After the refactoring, with two invocations and a single line of code the problem is solved. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module LayoutHelper | |
# ... | |
# before | |
def submit_button(content, options = {}) | |
options[:class] ||= "" | |
options[:class] << " submit" | |
options[:class].lstrip! | |
options.stringify_keys! | |
if disable_with = options.delete('disable_with') | |
options['data-disable-with'] = disable_with | |
end | |
content_tag :button, content, options | |
end | |
# after | |
def submit_button(content, options = {}) | |
options.merge!(:class => "#{options[:class]} submit".lstrip) | |
options.stringify_keys! | |
if disable_with = options.delete('disable_with') | |
options['data-disable-with'] = disable_with | |
end | |
content_tag :button, content, options | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe LayoutHelper do | |
# ... | |
describe "#submit_button" do | |
it "renders a button tag for form submission" do | |
helper.submit_button("Create").should == %(<button class="submit">Create</button>) | |
helper.submit_button("Create", :class => "cool").should == %(<button class="cool submit">Create</button>) | |
helper.submit_button("Create", :id => "cool").should == %(<button class="submit" id="cool">Create</button>) | |
helper.submit_button("Create", :disable_with => "Creating..").should == %(<button class="submit" data-disable-with="Creating..">Create</button>) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment