Commit 2cbf27e2 by ivan Lan

Use constantize instead of Object.const_get

parent 04c3d4e2
example_id | status | run_time | example_id | status | run_time |
------------------------------------------ | ------ | --------------- | ------------------------------------------ | ------ | --------------- |
./spec/shotengai/models_spec.rb[1:1:1:1] | failed | 0.20529 seconds | ./spec/shotengai/models_spec.rb[1:1:1:1] | passed | 0.44582 seconds |
./spec/shotengai/models_spec.rb[1:1:1:2] | failed | 0.16928 seconds | ./spec/shotengai/models_spec.rb[1:1:1:2] | passed | 0.20555 seconds |
./spec/shotengai/models_spec.rb[1:1:2:1] | failed | 0.16917 seconds | ./spec/shotengai/models_spec.rb[1:1:2:1] | passed | 0.20124 seconds |
./spec/shotengai/models_spec.rb[1:1:2:2] | failed | 0.1559 seconds | ./spec/shotengai/models_spec.rb[1:1:2:2] | passed | 0.17955 seconds |
./spec/shotengai/models_spec.rb[1:1:2:3] | failed | 0.16818 seconds | ./spec/shotengai/models_spec.rb[1:1:2:3] | passed | 0.17157 seconds |
./spec/shotengai/models_spec.rb[1:1:3:1] | failed | 0.17072 seconds | ./spec/shotengai/models_spec.rb[1:1:3:1] | passed | 0.19801 seconds |
./spec/shotengai/models_spec.rb[1:1:3:2] | failed | 0.17477 seconds | ./spec/shotengai/models_spec.rb[1:1:3:2] | passed | 0.18172 seconds |
./spec/shotengai/models_spec.rb[1:1:3:3] | failed | 0.16544 seconds | ./spec/shotengai/models_spec.rb[1:1:3:3] | passed | 0.17998 seconds |
./spec/shotengai/models_spec.rb[1:1:4:1] | failed | 0.1618 seconds | ./spec/shotengai/models_spec.rb[1:1:4:1] | passed | 0.23986 seconds |
./spec/shotengai/models_spec.rb[1:1:5:1] | failed | 0.15564 seconds | ./spec/shotengai/models_spec.rb[1:1:5:1] | passed | 0.17938 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:1] | failed | 0.15182 seconds | ./spec/shotengai/models_spec.rb[1:1:5:2:1] | passed | 0.24027 seconds |
./spec/shotengai/models_spec.rb[1:1:5:2:2] | failed | 0.16236 seconds | ./spec/shotengai/models_spec.rb[1:1:5:2:2] | passed | 0.21941 seconds |
./spec/shotengai/models_spec.rb[1:2:1:1] | failed | 0.17459 seconds | ./spec/shotengai/models_spec.rb[1:2:1:1] | passed | 0.20203 seconds |
./spec/shotengai/models_spec.rb[1:2:1:2] | failed | 0.18331 seconds | ./spec/shotengai/models_spec.rb[1:2:1:2] | passed | 0.21369 seconds |
./spec/shotengai/models_spec.rb[1:2:2:1] | passed | 0.61428 seconds | ./spec/shotengai/models_spec.rb[1:2:2:1] | passed | 0.43436 seconds |
./spec/shotengai/models_spec.rb[1:2:2:2] | failed | 0.15561 seconds | ./spec/shotengai/models_spec.rb[1:2:2:2] | passed | 0.28966 seconds |
...@@ -7,7 +7,7 @@ module Shotengai ...@@ -7,7 +7,7 @@ module Shotengai
class_methods do class_methods do
def can_shopping_with klass_name, options={} def can_shopping_with klass_name, options={}
klass = Object.const_get(klass_name) klass = klass_name.constantize
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
......
...@@ -84,7 +84,7 @@ module Shotengai ...@@ -84,7 +84,7 @@ module Shotengai
end end
def can_buy *good_class_names def can_buy *good_class_names
good_classes = good_class_names.map { |name| Object.const_get(name) } good_classes = good_class_names.map(&:constantize)
# 所有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" })
......
...@@ -53,8 +53,8 @@ module Shotengai ...@@ -53,8 +53,8 @@ module Shotengai
class ::#{subclass}Snapshot < Shotengai::Snapshot; end class ::#{subclass}Snapshot < Shotengai::Snapshot; end
") ")
subclass.instance_eval do subclass.instance_eval do
def series_class; Object.const_get "#{self.name}Series" ; end def series_class; "#{self.name}Series".constantize ; end
def snapshot_class; Object.const_get "#{self.name}Snapshot"; end def snapshot_class; "#{self.name}Snapshot".constantize; end
end end
end end
......
...@@ -15,7 +15,6 @@ c.exec_update('drop database if exists shotengai_test;') ...@@ -15,7 +15,6 @@ c.exec_update('drop database if exists shotengai_test;')
c.exec_update('create database shotengai_test') c.exec_update('create database shotengai_test')
c.exec_update('use shotengai_test') c.exec_update('use shotengai_test')
# 如何通过activerecord 建database
RSpec.describe 'Shotengai Models' do RSpec.describe 'Shotengai Models' do
before do before do
ActiveRecord::Migration[5.1].subclasses.each do |migrate| ActiveRecord::Migration[5.1].subclasses.each do |migrate|
......
require "bundler/setup" require "bundler/setup"
require 'rails' require 'rails'
require 'active_record'
require 'shotengai' require 'shotengai'
require 'support/factory_girl' require 'support/factory_girl'
# Dir['../support/**/*.rb'].each { |f| require f } # Dir['../support/**/*.rb'].each { |f| require f }
......
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