Skip to content

Commit

Permalink
Merge pull request #81 from thegcat/decorator_spec
Browse files Browse the repository at this point in the history
Calculation spec
  • Loading branch information
pietia committed Jul 18, 2013
2 parents b24b58d + d9d8b1e commit ee8d62f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/decorators/activity_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ def requirements_markdown
def room_left
if object.anybody_can_join? then ""
else
left = [[object.limit_of_participants - object.participations_count, 0].max, object.limit_of_participants ].min
left = open_spots
if left == 1 then I18n.t("activities.room_left.one")
elsif left > 0 then I18n.t("activities.room_left.many", left: left)
else I18n.t("activities.room_left.none")
end
end
end

def open_spots
[[object.limit_of_participants - object.participations_count, 0].max, object.limit_of_participants ].min
end

def time
if object.anytime?
I18n.t("activities.anytime")
Expand Down
35 changes: 35 additions & 0 deletions spec/decorators/activity_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
require 'spec_helper'

describe ActivityDecorator do
let (:activity) { FactoryGirl.build_stubbed(:activity, limit_of_participants: 20) }
let (:decorator) { ActivityDecorator.new(activity) }

describe "#spots_left" do
subject { decorator.open_spots }

before do
activity.stub(:participations_count).and_return(count)
end

context "no participants" do
let(:count) { 0 }

it { should == 20 }
end

context "some participants" do
let(:count) { 5 }

it { should == 15 }
end

context "too many participants" do
let(:count) { 25 }

it { should == 0 }
end

context "you're drunk Rails, go home!" do
# it seems Rails counter caches return negative values sometimes
let(:count) { -5 }

it { should == 20 }
end
end
end

0 comments on commit ee8d62f

Please sign in to comment.