Skip to content

Instantly share code, notes, and snippets.

@supaspoida
Created October 12, 2011 13:41
Show Gist options
  • Save supaspoida/1281250 to your computer and use it in GitHub Desktop.
Save supaspoida/1281250 to your computer and use it in GitHub Desktop.
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
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
= 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