Skip to content

Commit

Permalink
Renamed the main method to "show"
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain Hecker committed Dec 10, 2008
1 parent 049da5d commit 85d34d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ h1. NoValueHelper

h2. The Problem

This Ruby on Rails plugin tries to solve a common pattern when displaying
values from the database. If you want to display a nice message like 'no value'
This Ruby on Rails plugin tries to solve a common pattern when showing
values from the database. If you want to show a nice message like 'no value'
when an optional attribute has been left empty, you usually need to do the
same thing over and over again:

Expand All @@ -17,11 +17,11 @@ h2. The Solution

So this plugin tries to shorten this:

<%= display(@user.name) %>
<%= show(@user.name) %>

Or the second example:

<%= display(:link_to, @user.daddy) { @user.daddy.name } %>
<%= show(:link_to, @user.daddy) { @user.daddy.name } %>

Don't worry, NoMethodErrors will be caught for you. That is why we use a block
in this case.
Expand Down
2 changes: 1 addition & 1 deletion lib/no_value_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module NoValueHelper
@@no_value_text = lambda { %Q{<em class="no_value">#{I18n.t(:no_value, :default => "no value")}</em>} }
@@no_value_check_method = lambda { |value| value.blank? }

def display(*args, &block)
def show(*args, &block)
if block_given?
value = yield
else
Expand Down
32 changes: 16 additions & 16 deletions spec/no_value_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class NoValueHelperSpec < Spec::ExampleGroup

include NoValueHelper

describe "The display-method" do
describe "The show-method" do

before do
@no_value = %Q{<em class="no_value">no value</em>}
Expand All @@ -14,56 +14,56 @@ class NoValueHelperSpec < Spec::ExampleGroup
describe "without a block given" do

it "should return the first argument, when no other parameters have been given" do
display("foo").should == "foo"
show("foo").should == "foo"
end

it "should pass the first argument to the method given as second argument" do
display("foo", :first_helper).should == "foo has been modified"
show("foo", :first_helper).should == "foo has been modified"
end

it "should pass the first argument and all the last arguments to the method given as second argument" do
display("foo", :second_helper, "bar").should == "foo and bar"
show("foo", :second_helper, "bar").should == "foo and bar"
end

it "should return 'no value' when the first argument is blank" do
display("").should == @no_value
show("").should == @no_value
end

it "should ignore further arguments if the first argument is blank" do
display("", :first_helper).should == @no_value
show("", :first_helper).should == @no_value
end

end

describe "with a block given" do

it "should return the return value of the block" do
display{"foo"}.should == "foo"
show{"foo"}.should == "foo"
end

it "should pass the value of the block to the method given as first argument" do
display(:first_helper) { "foo" }.should == "foo has been modified"
show(:first_helper) { "foo" }.should == "foo has been modified"
end

it "should pass the value of the block and all the last arguments to the method given as first argument" do
display(:second_helper, "bar") { "foo" }.should == "foo and bar"
show(:second_helper, "bar") { "foo" }.should == "foo and bar"
end

it "should return 'no value' when the value of the block is blank" do
display { "" }.should == @no_value
show { "" }.should == @no_value
end

it "should return 'no value' when the value of the block is blank, ignoring other arguments" do
display(:first_helper) { "" }.should == @no_value
show(:first_helper) { "" }.should == @no_value
end

it "should return 'no value' when a NoMethodError occurs in the block" do
display { raise NoMethodError }.should == @no_value
display { @foo.bar }.should == @no_value
show { raise NoMethodError }.should == @no_value
show { @foo.bar }.should == @no_value
end

it "should still raise if another exception raises" do
lambda { display { raise "something else" } }.should raise_error
lambda { show { raise "something else" } }.should raise_error
end

end
Expand All @@ -72,12 +72,12 @@ class NoValueHelperSpec < Spec::ExampleGroup

it "should change the 'no value' string" do
@@no_value_text = lambda { "was empty" }
display("").should == "was empty"
show("").should == "was empty"
end

it "should change the check method" do
@@no_value_check_method = lambda { |value| value.nil? }
display("").should == ""
show("").should == ""
end

before(:each) do
Expand Down

0 comments on commit 85d34d0

Please sign in to comment.