Commit d28f79ad by ivan Lan

Add require_custom_file to the inherited of Series & Snapshot

parent fef8cd60
...@@ -82,7 +82,7 @@ module Shotengai ...@@ -82,7 +82,7 @@ module Shotengai
def define_related_class subclass def define_related_class subclass
# Useing Class.new could not get class_name in self.inherited # Useing Class.new could not get class_name in self.inherited
class_eval(" class_eval("
class ::#{subclass}Series < #{self.series_class}; end; class ::#{subclass}Series < #{self.series_class}; end
class ::#{subclass}Snapshot < #{self.snapshot_class}; end class ::#{subclass}Snapshot < #{self.snapshot_class}; end
") ")
subclass.instance_eval do subclass.instance_eval do
......
...@@ -43,18 +43,24 @@ module Shotengai ...@@ -43,18 +43,24 @@ module Shotengai
class << self class << self
def inherited subclass def inherited subclass
@subclass = subclass product_name = /^(.+)Series$/.match(subclass.name)[1]
@product_name = /^(.+)Series$/.match(subclass.name)[1] add_associations(subclass, product_name)
add_associations # 加载自定义文件
require_custom_file(product_name) if Rails.root
super super
end end
def add_associations def add_associations subclass, product_name
# belongs to Product # belongs to Product
@subclass.belongs_to :product, foreign_key: :shotengai_product_id, class_name: @product_name, touch: true subclass.belongs_to :product, foreign_key: :shotengai_product_id, class_name: product_name, touch: true
@subclass.belongs_to @product_name.underscore.to_sym, foreign_key: :shotengai_product_id, class_name: @product_name, touch: true subclass.belongs_to product_name.underscore.to_sym, foreign_key: :shotengai_product_id, class_name: product_name, touch: true
# has many snapshot # has many snapshot
@subclass.has_many :snapshots, class_name: "#{@product_name}Snapshot", foreign_key: :shotengai_series_id subclass.has_many :snapshots, class_name: "#{product_name}Snapshot", foreign_key: :shotengai_series_id
end
def require_custom_file product_name
file_path = Rails.root.join('app', 'models', "#{product_name}_series.rb")
require file_path if File.exist?(file_path)
end end
end end
......
...@@ -60,8 +60,15 @@ module Shotengai ...@@ -60,8 +60,15 @@ module Shotengai
# belongs to Series # belongs to Series
subclass.belongs_to :series, foreign_key: :shotengai_series_id, class_name: series_name, touch: true subclass.belongs_to :series, foreign_key: :shotengai_series_id, class_name: series_name, touch: true
subclass.belongs_to series_name.underscore.to_sym, foreign_key: :shotengai_series_id, class_name: series_name, touch: true subclass.belongs_to series_name.underscore.to_sym, foreign_key: :shotengai_series_id, class_name: series_name, touch: true
# 加载自定义文件
require_custom_file(product_name) if Rails.root
super super
end end
def require_custom_file product_name
file_path = Rails.root.join('app', 'models', "#{product_name}_snapshot.rb")
require file_path if File.exist?(file_path)
end
end end
# QUESTION: spec 赋值是在 after pay 合理? # QUESTION: spec 赋值是在 after pay 合理?
......
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