Created
March 4, 2014 12:25
-
-
Save mirrec/9345597 to your computer and use it in GitHub Desktop.
extracting pure ruby object from model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support/time" | |
class DueDateCalculator | |
def initialize(season) | |
@season = season | |
end | |
def calculate | |
@season.upcoming_round.start_date - 1.day | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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