Skip to content

Instantly share code, notes, and snippets.

@mirrec
Created March 4, 2014 12:25
Show Gist options
  • Save mirrec/9345597 to your computer and use it in GitHub Desktop.
Save mirrec/9345597 to your computer and use it in GitHub Desktop.
extracting pure ruby object from model
require "active_support/time"
class DueDateCalculator
def initialize(season)
@season = season
end
def calculate
@season.upcoming_round.start_date - 1.day
end
end
require_relative "../../app/services/due_date_calculator"
describe DueDateCalculator do
describe "#calculate" do
let(:round) { double(:round, :start_date => Date.new(2013, 12, 28)) }
let(:season) { double(:season, :upcoming_round => round) }
subject { described_class.new(season) }
it "is one date before start of upcoming_round" do
subject.calculate.should eq Date.new(2013, 12, 27)
end
end
end
class Season < ActiveRecord::Base
# ...
def upcoming_round
future_rounds.first
end
private
def future_rounds
rounds.to_a.select { |round| Date.today < round.start_date }
end
# ...
end
class SeasonTicket < ActiveRecord::Base
# ...
def due_date
@due_date ||= DueDateCalculator.new(season).calculate
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment