Skip to content

Instantly share code, notes, and snippets.

@onesup
Created August 18, 2013 16:11
Show Gist options
  • Save onesup/6262431 to your computer and use it in GitHub Desktop.
Save onesup/6262431 to your computer and use it in GitHub Desktop.
쿠폰 발행 관련 코드
class Coupon < ActiveRecord::Base
belongs_to :user
#published, used, new
#쿠폰 사용 약관에 "회원 탈퇴 시 쿠폰또한 삭제됩니다. 라고 명시해야함."
def self.valid?(code, user=nil)
if self.exists?(:code => code)
coupons = self.where(:code => code)
coupon = coupons.first
if coupon.user.nil? and coupon.status != "used"
result = true
elsif coupon.user.nil? and coupon.status == "used"
result = false
elsif coupon.user and coupon.status != "used"
result = coupon.can_use_for(user)
end
end
result
end
def can_use_for?(user)
if self.status != "used" && self.user.email == user.email
result = true
elsif self.status != "used" && self.user.email != user.email
result = self.user.id
elsif self.status == "used"
result = false
end
result
end
def self.made_for(user)
discount = 0.2
if User.exists?(user)
coupon = Coupon.create(:status => "published", :code => Coupon.random_code, :discount => discount)
user.coupons << coupon
else
Coupon.create(:status => "new", :code => Coupon.random_code, :discount => discount)
end
end
def self.random_code
coffee = %w(c o f f e e) * 2
letter = %w(l e t t e r) * 2
digit = %w(2 4 6 7 8 9) * 2
code = "c" + coffee.shuffle.join[0..5] + "e-l" + letter.shuffle.join[0..5] + "r-" + digit.shuffle.join[0..5]
code
end
#for test
def self.list
list = Array.new
10000.times do
list << Coupon.random_code
end
n = list.group_by(&:capitalize).map {|k,v| v.length}
n.sort
end
end
require 'spec_helper'
describe "Coupon" do
it "이 user1에게 published 된 것이라면 user1만 사용가능" do
coupon = FactoryGirl.create(:coupon, :published_to_user1)
user1 = FactoryGirl.create(:user, :user1)
coupon.can_use_for?(user1).should be_true
end
it "이 user1에게 published 된 것을 user2가 사용하면 user1의 id를 반환" do
coupon = FactoryGirl.create(:coupon, :published_to_user1)
user2 = FactoryGirl.build(:user, :user2)
coupon.can_use_for?(user2).should be_kind_of Fixnum
end
it "이 user1에게 published 된 것을 user2가 사용하려는데, user1이 레코드에 없거나 탈퇴했으면 사용가능" do
end
it "의 코드가 발행되지 않은 것이라면 거부" do
code = "coffee-letter-000000"
Coupon.valid?(code).should be_false
end
it "이 무기명 발행된것이라면 사용승락" do
coupon = FactoryGirl.create(:coupon, :published_to_anonymous)
code = coupon.code
Coupon.valid?(code).should be_true
end
it "의 코드가 user2에게 발행된것을 user1이 사용하면 id를 반환" do
coupon = FactoryGirl.create(:coupon, :published_to_user2)
user2 = FactoryGirl.build(:user, :user1)
coupon.valid?(coupon.code, user1).should be_kind_of Fixnum
end
end
FactoryGirl.define do
factory :user do
sequence :email do |n|
"user#{n}@gmail.com"
end
trait :user1 do
email "[email protected]"
end
trait :user2 do
email "[email protected]"
end
end
factory :coupon do
code Coupon.random_code
trait :published_to_anonymous do
end
trait :published_to_user1 do
association :user, :user1
end
trait :published_to_user2 do
association :user, :user2
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment