Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created September 30, 2011 16:49
Show Gist options
  • Save jodosha/1254329 to your computer and use it in GitHub Desktop.
Save jodosha/1254329 to your computer and use it in GitHub Desktop.
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.
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
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