Commit 762444fe by ivan Lan

Fix the nested inheriting about series and snapshot

parent daa37e9f
......@@ -16,3 +16,4 @@ example_id | status | run_time |
./spec/shotengai/models_spec.rb[1:2:1:2] | passed | 0.3256 seconds |
./spec/shotengai/models_spec.rb[1:2:2:1] | passed | 0.62601 seconds |
./spec/shotengai/models_spec.rb[1:2:2:2] | passed | 0.45201 seconds |
./spec/shotengai/models_spec.rb[1:3:1] | passed | 0.48308 seconds |
......@@ -64,6 +64,13 @@ module Shotengai
end
class << self
def series_class
Shotengai::Series
end
def snapshot_class
Shotengai::Snapshot
end
# TODO: ::#{subclass}Series 增加命名规则定义 降低耦合性??
def inherited(subclass)
# 创建相关 series 与 snapshot
......@@ -75,8 +82,8 @@ module Shotengai
def define_related_class subclass
# Useing Class.new could not get class_name in self.inherited
class_eval("
class ::#{subclass}Series < Shotengai::Series; end;
class ::#{subclass}Snapshot < Shotengai::Snapshot; end
class ::#{subclass}Series < #{self.series_class}; end;
class ::#{subclass}Snapshot < #{self.snapshot_class}; end
")
subclass.instance_eval do
def series_class; "#{self.name}Series".constantize ; end
......
......@@ -266,6 +266,29 @@ RSpec.describe 'Shotengai Models' do
end
end
describe 'Nested Inherited' do
before do
class Good < Shotengai::Product; end
class NestedGood < Good; end
class GoodSeries
def a
'outside a'
end
end
class NestedGoodSeries < GoodSeries
def a
'nested a'
end
end
end
it 'does something' do
expect(GoodSeries.new.a).to eq('outside a')
expect(NestedGoodSeries.new.a).to eq('nested a')
end
end
after do
ActiveRecord::Migration[5.1].subclasses.each do |migrate|
migrate.migrate(:down)
......@@ -273,6 +296,3 @@ RSpec.describe 'Shotengai Models' do
end
end
# ActiveRecord::Migration[5.1].subclasses.each do |migrate|
# migrate.migrate(:down)
# end
\ No newline at end of file
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