개발 중에 나는 ActiveRecord::Base
클래스 내에 메소드를 포함 시켜서 모든 모델을 수집하려고 시도한다. 그래서 그들은 모델을 구성 할 수 있고 그 모델을 글로벌 배열에 추가 할 수있는 고리를 제공 할 것이다.레일 엔진이 캐싱하지 않는다
module EngineName
module ActiveRecordExtensions
extend ActiveSupport::Concern
included do
def self.inherited(klass) #:nodoc:
klass.class_eval do
def self.config_block(&block)
abstract_model = EngineName::AbstractModel.new(self)
abstract_model.instance_eval(&block) if block
EngineName::Models.add(abstract_model)
end
end
end
end
end
end
내 EngineName::Models
클래스는 모든 모델을 보유하고 래퍼입니다. 각
module EngineName
class Models
class << self
def all
@models ||= []
end
alias_method :models, :all
def navigation
@models - excluded_navigation_models
end
def routed
@models - excluded_route_models
end
# Creates an array of models if none exists. Appends new models
# if the instance variable already exists.
def register(klass)
if @models.nil?
@models = [klass]
else
@models << klass unless @models.include?(klass) || excluded_models.include?(klass)
end
end
alias_method :add, :register
end
end
end
하지만, 내 모델 내의
config_block
메소드가 호출되는 새로 고침 차례로 모델을 내 전역 배열 내에서 반복해서 같은 모델을 추가합니다.
아래에서 볼 수 있듯이 모든 모델을 반복 할 때마다 계속해서 추가됩니다.
내 엔진 내에서 특정 클래스를 캐시하는 방법은 없나요? 또는 모델 자체에 후크가있는 모델을 등록하는 방식에 결함이 있습니까?
나는 이것을 시험해보고 어떻게되는지 알려줄 것이다. 고마워! 나는 그것을 올바른 것으로 표시 하겠지만, 먼저 시도해보고 싶습니다. 마음에 들지 않으면 :) – Garrett
이 작품은 더 논리적이라고 느낍니다. 특정 장소에서. 다시 한 번 감사드립니다! – Garrett