Skip to content

Instantly share code, notes, and snippets.

@nandokakimoto
Last active February 1, 2017 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nandokakimoto/bba8b095675879d09577bce5831336fb to your computer and use it in GitHub Desktop.
Save nandokakimoto/bba8b095675879d09577bce5831336fb to your computer and use it in GitHub Desktop.
blog-post-830-plan-v1
class Plan < ActiveRecord::Base
has_many :members
validates :name, presence: true
BASE_PRICE = 20
DISCOUNT = 1
# Apply discount if has more than 2 members
def price
if members.count > 2
BASE_PRICE - discount
else
BASE_PRICE
end
end
private
def discount
(members.count - 2) * DISCOUNT
end
end
require 'rails_helper'
RSpec.describe Plan, type: :model do
describe ".price" do
let(:plan) { Plan.create(name: 'basic') }
describe "has less than 2 members" do
let(:dan) { Member.create(first_name: 'Daniel') }
let(:tom) { Member.create(first_name: 'Thomas') }
it "should return base price when one member" do
plan.members << dan
expect(plan.price).to eql(20)
end
it "should return base price when two members" do
plan.members << dan
plan.members << tom
expect(plan.price).to eql(20)
end
end
describe "has more than two members" do
let(:joe) { Member.create(first_name: 'Joe') }
let(:mary) { Member.create(first_name: 'Mary') }
let(:sarah) { Member.create(first_name: 'Sarah') }
it "should give 1 discount per additional user" do
plan.members << joe
plan.members << mary
plan.members << sarah
expect(plan.price).to eql(19)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment