Skip to content

Commit

Permalink
Updated spec
Browse files Browse the repository at this point in the history
  • Loading branch information
thetron committed May 3, 2011
1 parent 6d39d3a commit 291e2d3
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 13 deletions.
18 changes: 16 additions & 2 deletions lib/css3buttons/helpers/button_helper.rb
Expand Up @@ -4,11 +4,11 @@ module ButtonHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::FormTagHelper
def method_missing(method, *args)
if method.to_s.index("button_link_to") || method.to_s.index("button_submit_tag")
if is_link_method?(method) || is_submit_method?(method) || is_button_method?(method)
qualifiers = ["primary", "big", "positive", "negative", "pill", "danger", "safe", "button"]
color_map = {"positive" => "safe", "negative" => "danger"}

method_qualifiers = method.to_s.split("_")[0...-3] + ["button"]
method_qualifiers = strip_method_name(method).split("_") + ["button"]
method_qualifiers.map! do |qualifier|
if color_map.has_key?(qualifier)
qualifier = color_map[qualifier]
Expand All @@ -31,6 +31,8 @@ def method_missing(method, *args)

if is_link_method?(method)
link_to(label, link, options)
elsif is_button_method?(method)
content_tag :button, label, { "type" => "submit", "name" => "commit", "value" => "commit" }.merge(options.stringify_keys)
else
submit_tag(label, options)
end
Expand Down Expand Up @@ -74,6 +76,18 @@ def add_classes_to_html_options(classes, html_options = {})
def is_link_method?(method)
method.to_s.index("button_link_to")
end

def is_button_method?(method)
method.to_s.index("button_tag")
end

def is_submit_method?(method)
method.to_s.index("button_submit_tag")
end

def strip_method_name(method)
method.to_s.gsub("button_link_to", "").gsub("button_tag", "").gsub("button_submit_tag", "")
end
end
end
end
127 changes: 116 additions & 11 deletions spec/button_helper_spec.rb
Expand Up @@ -10,6 +10,8 @@
@path = "/search/site"
end

#--- basic buttons

it "should create basic buttons" do
link = html(button_link_to(@label, @path))

Expand All @@ -18,38 +20,62 @@
link.should_not have_selector("a.#{qualifier}")
end
end

it "should create basic submit buttons" do
button = html(button_submit_tag(@label))

button.should have_selector("input.button[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("input.#{qualifier}")
end
end
it "should create basic button tags" do
button = html(button_tag(@label))
button.should have_selector("button.button")
@qualifiers.each do |qualifier|
button.should_not have_selector("button.#{qualifier}")
end
end

#--- basic buttons with icons

it "should create basic buttons with valid icons" do
@icons.each do |icon|
link = html(send(:"#{icon}_button_link_to", @label, @path))
link.should have_selector("a.button.icon.#{icon}[href='#{@path}']")
end
end

it "should create basic button tags with valid icons" do
@icons.each do |icon|
button = html(send(:"#{icon}_button_tag", @label))
button.should have_selector("button.button.icon.#{icon}[type='submit']")
end
end

#--- positive buttons

it "should create positive buttons" do
link = html(positive_button_link_to(@label, @path))
link.should have_selector("a.button.safe[href='#{@path}']")
@qualifiers.each do |qualifier|
link.should_not have_selector("a.#{qualifier}") unless qualifier == "safe"
end
end

it "should create positive submit buttons" do
button = html(positive_button_submit_tag(@label))
button.should have_selector("input.button.safe[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("input.#{qualifier}") unless qualifier == "safe"
end
end
it "should create positive button tags" do
button = html(positive_button_tag(@label))
button.should have_selector("button.button.safe[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("button.#{qualifier}") unless qualifier == "safe"
end
end

#--- negative buttons

it "should create negative buttons" do
link = html(negative_button_link_to(@label, @path))
Expand All @@ -58,14 +84,22 @@
link.should_not have_selector("a.#{qualifier}") unless qualifier == "danger"
end
end

it "should create positive submit buttons" do
it "should create negative submit buttons" do
button = html(negative_button_submit_tag(@label))
button.should have_selector("input.button.danger[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("a.#{qualifier}") unless qualifier == "danger"
button.should_not have_selector("input.#{qualifier}") unless qualifier == "danger"
end
end
it "should create negative button tags" do
button = html(negative_button_tag(@label))
button.should have_selector("button.button.danger[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("button.#{qualifier}") unless qualifier == "danger"
end
end

#--- positive buttons with icons

it "should create positive buttons with valid icons" do
@icons.each do |icon|
Expand All @@ -76,6 +110,17 @@
end
end
end
it "should create positive button tags with valid icons" do
@icons.each do |icon|
button = html(send(:"positive_#{icon}_button_tag", @label))
button.should have_selector("button.button.safe.icon.#{icon}[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("button.#{qualifier}") unless qualifier == "safe"
end
end
end

#--- negative buttons with icons

it "should create negative buttons with valid icons" do
@icons.each do |icon|
Expand All @@ -86,6 +131,17 @@
end
end
end
it "should create negative button tags with valid icons" do
@icons.each do |icon|
button = html(send(:"negative_#{icon}_button_tag"))
button.should have_selector("button.button.danger.icon.#{icon}[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("button.#{qualifier}") unless qualifier == "danger"
end
end
end

#--- pill buttons

it "should create pill buttons" do
link = html(pill_button_link_to(@label, @path))
Expand All @@ -94,14 +150,22 @@
link.should_not have_selector("a.#{qualifier}") unless qualifier == "pill"
end
end

it "should create pill submit buttons" do
button = html(pill_button_submit_tag(@label))
button.should have_selector("input.button.pill[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("input.#{qualifier}") unless qualifier == "pill"
end
end
it "should create pill button tags" do
button = html(pill_button_tag(@label))
button.should have_selector("button.button.pill[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("button.#{qualifier}") unless qualifier == "pill"
end
end

#--- pill buttons with icons

it "should create pill buttons with valid icons" do
@icons.each do |icon|
Expand All @@ -112,30 +176,53 @@
end
end
end
it "should create pill button tags with valid icons" do
@icons.each do |icon|
button = html(send(:"pill_#{icon}_button_tag", @label))
button.should have_selector("button.button.pill.icon.#{icon}[type='submit']")
@qualifiers.each do |qualifier|
button.should_not have_selector("button.#{qualifier}") unless qualifier == "pill"
end
end
end

#--- positive pill buttons

it "should create positive pill buttons" do
link = html(positive_pill_button_link_to(@label, @path))
link.should have_selector("a.button.pill.safe[href='#{@path}']")
link.should_not have_selector("a.danger")
end

it "should create positive pill submit buttons" do
button = html(positive_pill_button_submit_tag(@label))
button.should have_selector("input.button.pill.safe[type='submit']")
button.should_not have_selector("input.danger")
end
it "should create positive pill button tags" do
button = html(positive_pill_button_tag(@label))
button.should have_selector("button.button.pill.safe[type='submit']")
button.should_not have_selector("button.danger")
end

#--- negative pill buttons

it "should create negative pill buttons" do
link = html(negative_pill_button_link_to(@label, @path))
link.should have_selector("a.button.pill.danger[href='#{@path}']")
link.should_not have_selector("a.safe")
end

it "should create negative pill submit buttons" do
button = html(negative_pill_button_submit_tag(@label))
button.should have_selector("input.button.pill.danger[type='submit']")
button.should_not have_selector("input.safe")
end
it "should create negative pill button tags" do
button = html(negative_pill_button_tag(@label))
button.should have_selector("button.button.pill.danger[type='submit']")
button.should_not have_selector("button.safe")
end

#--- positive pill buttons with icons

it "should create positive pill buttons with valid icons" do
@icons.each do |icon|
Expand All @@ -144,14 +231,32 @@
link.should_not have_selector("a.danger")
end
end
it "should create positive pill button tags with valid icons" do
@icons.each do |icon|
button = html(send(:"positive_pill_#{icon}_button_tag", @label))
button.should have_selector("button.button.safe.pill.icon.#{icon}[type='submit']")
button.should_not have_selector("button.danger")
end
end

#--- negative pill buttons with icons

it "should create negative pill buttons with valid icons" do
@icons.each do |icon|
link = html(send(:"negative_pill_#{icon}_button_link_to", @label, @path))
link.should have_selector("a.button.danger.pill.icon.#{icon}[href='#{@path}']")
link.should_not have_selector("a.positive")
link.should_not have_selector("a.safe")
end
end
it "should create negative pill button tags with valid icons" do
@icons.each do |icon|
button = html(send(:"negative_pill_#{icon}_button_tag", @label))
button.should have_selector("button.button.danger.pill.icon.#{icon}[type='submit']")
button.should_not have_selector("button.safe")
end
end

#--- end

def html(text)
Capybara::string(text)
Expand Down

0 comments on commit 291e2d3

Please sign in to comment.