Created
October 12, 2011 13:41
-
-
Save supaspoida/1281250 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
require 'ostruct' | |
module ApplicationHelper | |
def pluralize(*args, &blk) | |
result = super | |
if block_given? | |
count, *text = result.split | |
yield OpenStruct.new(text: text.join(' '), count: count) | |
nil | |
else | |
result | |
end | |
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 ApplicationHelper do | |
describe "#pluralize" do | |
context "when no block given" do | |
subject { pluralize(2, 'Cred') } | |
it { should == "2 Creds" } | |
end | |
context "when block given" do | |
subject do | |
pluralize(2, 'Awesome Cred') { |p| p } | |
end | |
it { should be_blank } | |
it "yields an object to use in the view" do | |
pluralize(2, 'Awesome Cred') do |p| | |
p.text.should == "Awesome Creds" | |
p.count.should == "2" | |
end | |
end | |
end | |
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
= pluralize 5, 'Thing' do |p| | |
%strong= p.count | |
= p.text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment