-
-
Save nandokakimoto/bba8b095675879d09577bce5831336fb to your computer and use it in GitHub Desktop.
blog-post-830-plan-v1
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 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 |
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 '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