Skip to content

Commit

Permalink
Allow a hit count to be passed to punch
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcrown committed Nov 12, 2015
1 parent 0cc483d commit dd5b60c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
5 changes: 3 additions & 2 deletions lib/punching_bag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ module PunchingBag
require 'punching_bag/acts_as_punchable'
require 'voight_kampff'

def self.punch(punchable, request=nil)
def self.punch(punchable, request = nil, count = 1)
if request.try(:bot?)
false
else
p = Punch.new
p.punchable = punchable
p.hits = count
p.save ? p : false
end
end
Expand All @@ -22,4 +23,4 @@ def self.average_time(*punches)
end
Time.zone.at(total_time / hits)
end
end
end
5 changes: 3 additions & 2 deletions lib/punching_bag/acts_as_punchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def hits(since=nil)
punches.after(since).sum(:hits)
end

def punch(request=nil)
PunchingBag.punch(self, request)
def punch(request=nil, options = {})
count = options[:count] || 1
PunchingBag.punch(self, request, count)
end
end

Expand Down
Binary file modified spec/internal/db/combustion_test.sqlite
Binary file not shown.
32 changes: 24 additions & 8 deletions spec/lib/punching_bag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@

describe PunchingBag do
let(:article) { Article.create title: 'Hector', content: 'Ding, ding ding... ding. Ding. DING. DING! ' }
let(:human_request) { OpenStruct.new(bot?: false) }
let(:bot_request) { OpenStruct.new(bot?: true) }

subject { PunchingBag }

describe '.punch' do
it 'does nothing when the request is from a bot' do
expect(PunchingBag.punch(article, bot_request)).to be false
let(:request) { nil }

context 'when request is from a bot' do
let(:request) { instance_double(ActionDispatch::Request, bot?: true) }

it 'does nothing' do
expect(PunchingBag.punch(article, request)).to be false
end
end

context 'when the request is valid' do
let(:request) { instance_double(ActionDispatch::Request, bot?: false) }

it 'creates a new punch' do
expect { PunchingBag.punch(article, request) }.to change { Punch.count }.by 1
end
end

it 'creates a new punch when the request is valid' do
expect { PunchingBag.punch(article, human_request) }.to change { Punch.count }.by 1
context 'when there is no request' do
it 'creates a new punch' do
expect { PunchingBag.punch(article) }.to change { Punch.count }.by 1
end
end

it 'creates a new punch when there is no request' do
expect { PunchingBag.punch(article) }.to change { Punch.count }.by 1
context 'when count is more than one' do
it 'creates a new punch with a higher count' do
expect { PunchingBag.punch(article, nil, 2) }.to change { Punch.sum(:hits) }.by 2
end
end
end

Expand Down
8 changes: 7 additions & 1 deletion spec/models/punchable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
it 'incleases hits by one' do
expect { subject.punch }.to change { subject.hits }.by 1
end

context 'when count is set to two' do
it 'increases hits by two' do
expect { subject.punch(nil, count: 2) }.to change { subject.hits }.by 2
end
end
end
end

Expand Down Expand Up @@ -54,4 +60,4 @@
its('sort_by_popularity.second') { should eql article_1 }
end
end
end
end

0 comments on commit dd5b60c

Please sign in to comment.