Skip to content

Commit

Permalink
Merge pull request #144 from robotmay/score-adjustments
Browse files Browse the repository at this point in the history
Scores decrement automatically after 2 days
  • Loading branch information
Robert May committed Aug 13, 2013
2 parents 55c7267 + 86ba05a commit b6f749e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/models/photograph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ def set_highest_rank
end

def increment_score(by = 1)
Photograph.rankings.increment(id, by) do
set_highest_rank
end
Photograph.rankings.increment(id, by)
set_highest_rank
Worker.perform_in(2.days, 'Photograph', id, :decrement_score, by)
end

def decrement_score(by = 1)
Expand Down
8 changes: 8 additions & 0 deletions app/workers/photograph_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class PhotographWorker
include Sidekiq::Worker

def perform(photograph_id, method, *args)
photo = Photograph.find(photograph_id)
photo.send(method, *args)
end
end
8 changes: 8 additions & 0 deletions app/workers/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Worker
include Sidekiq::Worker

def perform(klass, id, method, *args)
model = klass.constantize.find(id)
model.send(method, *args)
end
end
2 changes: 2 additions & 0 deletions spec/models/favourite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
let(:user) { favourite.user }
let(:counter) { double("counter", increment: true, decrement: true) }

before { Photograph.any_instance.stub(:set_highest_rank) { true } }

it { should belong_to(:user) }
it { should belong_to(:photograph) }
it { should have_many(:notifications) }
Expand Down
12 changes: 11 additions & 1 deletion spec/models/photograph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Photograph do
let(:user) { User.make! }
let(:photograph) { Photograph.make(user: user) }
let(:photograph) { Photograph.make!(user: user) }
subject { photograph }

describe "associations" do
Expand Down Expand Up @@ -104,6 +104,16 @@
end
end

describe "score" do
describe "#increment_score" do
it "enqueues a score decrement" do
expect {
photograph.increment_score(1)
}.to change(Worker.jobs, :size).by(1)
end
end
end

describe "defaults" do
let(:photograph) { Photograph.make!(user: user) }
let(:user) { User.make! }
Expand Down
1 change: 1 addition & 0 deletions spec/support/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'sidekiq/testing'
13 changes: 13 additions & 0 deletions spec/workers/worker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe Worker do
subject { Worker.new }
let(:photograph) { Photograph.make }
before { Photograph.stub(:find) { photograph } }
before { Photograph.stub(:increment_score) { true } }

it "runs any method" do
photograph.should_receive(:increment_score)
subject.perform('Photograph', 1, :increment_score, 1)
end
end

0 comments on commit b6f749e

Please sign in to comment.