ãã¤ã³ãã·ã¹ãã (2) -- ãã°ã¤ã³ãã¤ã³ãã®ä»ä¸
2013/09/20
ååã«å¼ãç¶ããããã¤ã³ãã·ã¹ãã ãã®å®è£ ãç¶ãã¾ãã
Reward
ã¢ãã«
ç§ãã¡ã®ç®ä¸ã®æ¸æ¡äºé
ã¯ãCustomer#points
ãã©ãå®è£
ãããã§ããããã«æãã¤ãã®ã¯ customers
ãã¼ãã«ã«æ´æ°åã® points
ã«ã©ã ã追å ãã¦ãã®å¤ãå¢æ¸ãããã¨ããæ¹æ³ã§ããããã¤ã³ãã®å¢æ¸å±¥æ´ãè¨é²ãããªãã¨ããåé¡ãããã¾ãã
ããã§ãæ°ãã« rewards
ãã¼ãã«ãä½ã£ã¦ customers
ãã¼ãã«ã¨é¢é£ä»ããrewards
ãã¼ãã«ã® points
ã«ã©ã ã«ãã¤ã³ãã®å¢æ¸é¡ãè¨é²ãããã¨ã«ãã¾ããcustomer_id
ã§ã¬ã³ã¼ããçµãè¾¼ãã ä¸ã§ points
ã«ã©ã ã®å¤ãåè¨ããã°é¡§å®¢ã®ä¿æãã¤ã³ããå¾ããã¨ãã§ãã¾ãã
$ rails g model reward
db/migrate/..._create_rewards.rb
ã次ã®ããã«ä¿®æ£ãã¦ã
class CreateCustomers < ActiveRecord::Migration def change create_table :rewards do |t| t.references :customer t.integer :points t.timestamps end end end
ãã¹ãç°å¢ã®ããã®ãã¼ã¿ãã¼ã¹ãæºåãã¾ãã
$ rake db:migrate $ rake db:test:prepare
Customer
ã¢ãã«ã¨ Reward
ã¢ãã«ãé¢é£ä»ãã¾ãã
app/models/customer.rb
ã次ã®ããã«ä¿®æ£ãã¾ãï¼5è¡ç®æ¿å
¥ï¼ã
require 'nkf' require 'bcrypt' class Customer < ActiveRecord::Base has_many :rewards attr_accessor :password # (çç¥) end
app/models/reward.rb
ã次ã®ããã«ä¿®æ£ãã¾ãï¼2è¡ç®æ¿å
¥ï¼ã
class Reward < ActiveRecord::Base belongs_to :customer end
ã¨ãããã Reward
ã¢ãã«ã®ãã¹ãã¯æ¸ããªããã¨ã«ãã¾ããåé¤ãã¦ããã¾ãããã
$ rm spec/models/reward_spec.rb
Customer#points
ã¡ã½ããã®ãã¹ãã¨å®è£
èªè ã®ä¸ã«ã¯ãã®ç¨åº¦ãªããã¹ããªã©æ¸ããªãã¦ãå®è£ ã§ããæ¹ãããã£ãããã¨æãã¾ãããæ¬é£è¼ã¯ RSpec ããã¼ããªã®ã§ãããã©ãããããã«ãã¹ããæ¸ãã¦ããå®è£ ãã¾ãããã
spec/models/customer_spec.rb
ã«æ¬¡ã®ã¨ã°ã¶ã³ãã«ãæ¿å
¥ãã¾ãï¼password=
ã¡ã½ããã®æ¬¡ã«ï¼ã
describe Customer, '#points' do let(:customer) { create(:customer, username: 'taro') } specify 'é¢é£ä»ããããRewardã®pointsãåè¨ãã¦è¿ã' do customer.rewards.create(points: 1) customer.rewards.create(points: 5) customer.rewards.create(points: -2) expect(customer.points).to eq(4) end end
ãã¹ãã®å®è¡çµæï¼
Pending: Customer.authenticate ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã # Customer#pointsãæªå®è£ # ./spec/models/customer_spec.rb:120 Failures: 1) Customer#points é¢é£ä»ããããRewardã®pointsãåè¨ãã¦è¿ã Failure/Error: expect(customer.points).to eq(4) NoMethodError: undefined method `points' for #<Customer:0xb990b0c8> # ./spec/models/customer_spec.rb:92:in `block (2 levels) in <top (required)>'
app/models/customer.rb
ã次ã®ããã«ä¿®æ£ãã¾ãï¼11-13è¡æ¿å
¥ï¼ã
require 'nkf' require 'bcrypt' class Customer < ActiveRecord::Base has_many :rewards attr_accessor :password # (çç¥) def points rewards.sum(:points) end class << self def authenticate(username, password) customer = find_by_username(username) if customer.try(:password_digest) && BCrypt::Password.new(customer.password_digest) == password customer else nil end end end end
Customer#points
ã¡ã½ããã次ã®ããã«å®è£
ãã¦ãã¾ãï¼
rewards.sum(:points)
ããã¯æ¬¡ã®ãã㪠SQL æãçºè¡ãã¦å¤ãåå¾ããã®ã¨åãã§ãï¼Customer
ãªãã¸ã§ã¯ãã® ID ã 7 ã¨ããå ´åï¼ã
SELECT SUM(points) FROM rewards WHERE customer_id = 7
ãã㧠Customer#points
ã¡ã½ããã®ã¨ã°ã¶ã³ãã«ã¯éãã¾ãã
ãã°ã¤ã³ãã¤ã³ãã®å®è£ ã«æ»ã
ãã³ãã£ã³ã°ã«ãªã£ã¦ããã¨ã°ã¶ã³ãã«ã«æ»ãã¾ããç¾å¨ã¯ã次ã®ããã«ãªã£ã¦ãã¾ãï¼
specify 'ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã' do pending('Customer#pointsãæªå®è£ ') customer.stub(:points).and_return(0) expect { Customer.authenticate(customer.username, 'correct_password') }.to change { customer.points }.by(1) end
ããã次ã®ããã«å¤æ´ãã¾ãï¼
specify 'ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã' do expect { Customer.authenticate(customer.username, 'correct_password') }.to change { customer.points }.by(1) end
ãã¹ãã®å®è¡çµæï¼
Failures: 1) Customer.authenticate ãã°ã¤ã³ã«æåããã¨ãã¦ã¼ã¶ã¼ã®ä¿æãã¤ã³ãã1å¢ãã Failure/Error: expect { result should have been changed by 1, but was changed by 0 # ./spec/models/customer_spec.rb:121:in `block (2 levels) in <top (required)>'
app/models/customer.rb
ã次ã®ããã«ä¿®æ£ãã¾ãï¼11-13è¡æ¿å
¥ï¼ã
require 'nkf' require 'bcrypt' class Customer < ActiveRecord::Base # (çç¥) class << self def authenticate(username, password) customer = find_by_username(username) if customer.try(:password_digest) && BCrypt::Password.new(customer.password_digest) == password customer.rewards.create(points: 1) customer else nil end end end end
æ¿å ¥ãããã®ã¯æ¬¡ã®ã³ã¼ãã§ã:
customer.rewards.create(points: 1)
ããã§ããã®é¡§å®¢ã«ãã¤ã³ãã1ç¹ä»ä¸ããã¾ãã
次åã¯
次åã¯ããã¤ã³ãã·ã¹ãã ã®ç¬¬2ã®ä»æ§ããã°ã¤ã³ãã¤ã³ãã¯ã¦ã¼ã¶ã¼ãã¨ã«1æ¥1åããä¸ããããªããã®ãã¹ããæ¸ãã¦å®è£ ãã¾ããã§ã¯ãã¾ãã