Skip to content

Commit

Permalink
Added best_in_place_if and associated spec
Browse files Browse the repository at this point in the history
  • Loading branch information
troya2 committed Oct 24, 2011
1 parent 8f55c10 commit 473c77e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/best_in_place/helper.rb
Expand Up @@ -39,6 +39,14 @@ def best_in_place(object, field, opts = {})
out << "</span>"
raw out
end

def best_in_place_if(condition, object, field, opts={})
if condition
best_in_place(object, field, opts)
else
object.send field
end
end
end
end

35 changes: 35 additions & 0 deletions spec/helpers/best_in_place_spec.rb
Expand Up @@ -191,4 +191,39 @@
end
end
end

describe "#best_in_place_if" do
context "when the parameters are valid" do
before(:each) do
@output = "Some Value"
@field = :somefield
@object = mock("object", @field => @output)
@options = mock("options")
end
context "when the condition is true" do
before {@condition = true}
context "when the options parameter is left off" do
it "should call best_in_place with the rest of the parameters and empty options" do
helper.should_receive(:best_in_place).with(@object, @field, {})
helper.best_in_place_if @condition, @object, @field
end
end
context "when the options parameter is included" do
it "should call best_in_place with the rest of the parameters" do
helper.should_receive(:best_in_place).with(@object, @field, @options)
helper.best_in_place_if @condition, @object, @field, @options
end
end
end
context "when the condition is false" do
before {@condition = false}
it "should return the value of the field when the options value is left off" do
helper.best_in_place_if(@condition, @object, @field).should eq @output
end
it "should return the value of the field when the options value is included" do
helper.best_in_place_if(@condition, @object, @field, @options).should eq @output
end
end
end
end
end

0 comments on commit 473c77e

Please sign in to comment.