Commit fbaf667c by ivan Lan

Use class_name as attributes instead of class itself

parent 6697ee3a
example_id | status | run_time | example_id | status | run_time |
------------------------------------------ | ------ | --------------- | ------------------------------------------ | ------ | --------------- |
./spec/shotengai/models_spec.rb[1:1:1:1] | passed | 0.36278 seconds | ./spec/shotengai/models_spec.rb[1:1:1:1] | passed | 0.36006 seconds |
./spec/shotengai/models_spec.rb[1:1:1:2] | passed | 0.24106 seconds | ./spec/shotengai/models_spec.rb[1:1:1:2] | passed | 0.19221 seconds |
./spec/shotengai/models_spec.rb[1:1:2:1] | passed | 0.19389 seconds | ./spec/shotengai/models_spec.rb[1:1:2:1] | passed | 0.19696 seconds |
./spec/shotengai/models_spec.rb[1:1:2:2] | passed | 0.19885 seconds | ./spec/shotengai/models_spec.rb[1:1:2:2] | passed | 0.17745 seconds |
./spec/shotengai/models_spec.rb[1:1:2:3] | passed | 0.22058 seconds | ./spec/shotengai/models_spec.rb[1:1:2:3] | passed | 0.17437 seconds |
./spec/shotengai/models_spec.rb[1:1:3:1] | passed | 0.202 seconds | ./spec/shotengai/models_spec.rb[1:1:3:1] | passed | 0.19322 seconds |
./spec/shotengai/models_spec.rb[1:1:3:2] | passed | 0.20022 seconds | ./spec/shotengai/models_spec.rb[1:1:3:2] | passed | 0.1998 seconds |
./spec/shotengai/models_spec.rb[1:1:3:3] | passed | 0.20972 seconds | ./spec/shotengai/models_spec.rb[1:1:3:3] | passed | 0.2012 seconds |
./spec/shotengai/models_spec.rb[1:1:4:1] | passed | 0.23258 seconds | ./spec/shotengai/models_spec.rb[1:1:4:1] | passed | 0.22549 seconds |
./spec/shotengai/models_spec.rb[1:1:5:1] | passed | 0.23735 seconds | ./spec/shotengai/models_spec.rb[1:1:5:1] | passed | 0.19514 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:1] | passed | 0.23552 seconds | ./spec/shotengai/models_spec.rb[1:1:5:2:1] | passed | 0.25054 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:2] | passed | 0.25856 seconds | ./spec/shotengai/models_spec.rb[1:1:5:2:2] | passed | 0.22609 seconds |
./spec/shotengai/models_spec.rb[1:2:1:1] | passed | 0.22996 seconds | ./spec/shotengai/models_spec.rb[1:2:1:1] | passed | 0.22996 seconds |
./spec/shotengai/models_spec.rb[1:2:1:2] | passed | 0.2075 seconds | ./spec/shotengai/models_spec.rb[1:2:1:2] | passed | 0.2075 seconds |
./spec/shotengai/models_spec.rb[1:2:2:1] | passed | 0.46901 seconds | ./spec/shotengai/models_spec.rb[1:2:2:1] | passed | 0.46901 seconds |
......
require "shotengai/version" require "shotengai/version"
require 'rails' require 'rails'
require 'active_record' require 'active_record'
Dir['./lib/shotengai/*'].each { |f| require f } Dir['./lib/shotengai/*.rb'].each { |f| require f }
module Shotengai module Shotengai
# Your code goes here... # Your code goes here...
......
...@@ -6,7 +6,8 @@ module Shotengai ...@@ -6,7 +6,8 @@ module Shotengai
end end
class_methods do class_methods do
def can_shopping_with klass, options={} def can_shopping_with klass_name, options={}
klass = Object.const_get(klass_name)
unless Shotengai::Order <=> klass # 为子类 unless Shotengai::Order <=> klass # 为子类
raise ArgumentError.new('You can only buy the class inherit from Shotengai::Order') raise ArgumentError.new('You can only buy the class inherit from Shotengai::Order')
end end
......
...@@ -24,7 +24,8 @@ module Shotengai ...@@ -24,7 +24,8 @@ module Shotengai
default_scope { where(status: 'cart') } default_scope { where(status: 'cart') }
class << self class << self
def can_buy *good_classes def can_buy *good_class_names
good_classes = good_class_names.map { |name| Object.const_get(name) }
# 所有snapshot # 所有snapshot
has_many :snapshots, -> { has_many :snapshots, -> {
where(type: good_classes.map { |good_class| "#{good_class.name}Snapshot" }) where(type: good_classes.map { |good_class| "#{good_class.name}Snapshot" })
......
...@@ -41,7 +41,6 @@ module Shotengai ...@@ -41,7 +41,6 @@ module Shotengai
end end
def fill_snapshot def fill_snapshot
p 'cmm 222'
ActiveRecord::Base.transaction { ActiveRecord::Base.transaction {
self.snapshots.each(&:copy_info) self.snapshots.each(&:copy_info)
} }
...@@ -80,7 +79,8 @@ module Shotengai ...@@ -80,7 +79,8 @@ module Shotengai
super super
end end
def can_buy *good_classes def can_buy *good_class_names
good_classes = good_class_names.map { |name| Object.const_get(name) }
# 所有snapshot # 所有snapshot
has_many :snapshots, -> { has_many :snapshots, -> {
where(type: good_classes.map { |good_class| "#{good_class.name}Snapshot" }) where(type: good_classes.map { |good_class| "#{good_class.name}Snapshot" })
...@@ -102,7 +102,7 @@ module Shotengai ...@@ -102,7 +102,7 @@ module Shotengai
) )
end end
self.cart_class.can_buy *good_classes self.cart_class.can_buy *good_class_names
end end
end end
end end
......
...@@ -29,12 +29,12 @@ RSpec.describe 'Shotengai Models' do ...@@ -29,12 +29,12 @@ RSpec.describe 'Shotengai Models' do
class OtherGood < Shotengai::Product; end class OtherGood < Shotengai::Product; end
class TestOrder < Shotengai::Order class TestOrder < Shotengai::Order
can_buy ::TestGood can_buy 'TestGood'
end end
class TestBuyer < ActiveRecord::Base class TestBuyer < ActiveRecord::Base
include Shotengai::Buyer include Shotengai::Buyer
can_shopping_with ::TestOrder can_shopping_with 'TestOrder'
ActiveRecord::Base.connection.create_table(:test_buyers) unless ActiveRecord::Base.connection.table_exists?(:test_buyers) ActiveRecord::Base.connection.create_table(:test_buyers) unless ActiveRecord::Base.connection.table_exists?(:test_buyers)
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment