Skip to content

Instantly share code, notes, and snippets.

@nandokakimoto

nandokakimoto/plan.rb Secret

Last active Feb 1, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.