Commit fbaf667c by ivan Lan

Use class_name as attributes instead of class itself

parent 6697ee3a
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:2] | passed | 0.24106 seconds |
./spec/shotengai/models_spec.rb[1:1:2:1] | passed | 0.19389 seconds |
./spec/shotengai/models_spec.rb[1:1:2:2] | passed | 0.19885 seconds |
./spec/shotengai/models_spec.rb[1:1:2:3] | passed | 0.22058 seconds |
./spec/shotengai/models_spec.rb[1:1:3:1] | passed | 0.202 seconds |
./spec/shotengai/models_spec.rb[1:1:3:2] | passed | 0.20022 seconds |
./spec/shotengai/models_spec.rb[1:1:3:3] | passed | 0.20972 seconds |
./spec/shotengai/models_spec.rb[1:1:4:1] | passed | 0.23258 seconds |
./spec/shotengai/models_spec.rb[1:1:5:1] | passed | 0.23735 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:1] | passed | 0.23552 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:2] | passed | 0.25856 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.19221 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.17745 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.19322 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.2012 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.19514 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.22609 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:2:1] | passed | 0.46901 seconds |
......
require "shotengai/version"
require 'rails'
require 'active_record'
Dir['./lib/shotengai/*'].each { |f| require f }
Dir['./lib/shotengai/*.rb'].each { |f| require f }
module Shotengai
# Your code goes here...
......
......@@ -6,7 +6,8 @@ module Shotengai
end
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 # 为子类
raise ArgumentError.new('You can only buy the class inherit from Shotengai::Order')
end
......
......@@ -24,7 +24,8 @@ module Shotengai
default_scope { where(status: 'cart') }
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
has_many :snapshots, -> {
where(type: good_classes.map { |good_class| "#{good_class.name}Snapshot" })
......
......@@ -41,7 +41,6 @@ module Shotengai
end
def fill_snapshot
p 'cmm 222'
ActiveRecord::Base.transaction {
self.snapshots.each(&:copy_info)
}
......@@ -80,7 +79,8 @@ module Shotengai
super
end
def can_buy *good_classes
def can_buy *good_class_names
good_classes = good_class_names.map { |name| Object.const_get(name) }
# 所有snapshot
has_many :snapshots, -> {
where(type: good_classes.map { |good_class| "#{good_class.name}Snapshot" })
......@@ -102,7 +102,7 @@ module Shotengai
)
end
self.cart_class.can_buy *good_classes
self.cart_class.can_buy *good_class_names
end
end
end
......
......@@ -29,12 +29,12 @@ RSpec.describe 'Shotengai Models' do
class OtherGood < Shotengai::Product; end
class TestOrder < Shotengai::Order
can_buy ::TestGood
can_buy 'TestGood'
end
class TestBuyer < ActiveRecord::Base
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)
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