Rails,编写模块。存储设置在哪里?
Rails, writing module. Where is to store setting?
我有模特行为。
我想像这样包括:
class Building < ActiveRecord::Base
has_many :blocks
include Staticizable
check_children :blocks #here name of children model to check
...
end
行为看起来像:
module Staticizable
extend ActiveSupport::Concern
module ClassMethods
def check_children child
self.child_model = child #where is to store model name to check?
end
end
def was_staticized
(DateTime.now - self.staticized_date.to_datetime).to_i
end
def staticized?
if @child_model.present?
ap self.send(@child_model)
else
if staticize_period > was_staticized
true
else
false
end
end
end
def staticized
self.staticized_date = DateTime.now
save
end
end
所以我需要知道在哪里可以存储型号名称以检查 children。做这样的事情的正确方法是什么?
终于找到答案了,不确定是不是最好的方法,如果不是,请告诉我:
module Staticizable
extend ActiveSupport::Concern
included do
class << self;
attr_accessor :has_child_custom
end
end
module ClassMethods
def check_children child
self.has_child_custom = child
end
end
def was_staticized
unless staticized_date
return false
end
(DateTime.now - self.staticized_date.to_datetime).to_i
end
def staticized?
if self.class.has_child_custom.present?
self.send(self.class.has_child_custom).each do |child|
unless child.staticized?
return false
end
end
else
if self.staticize_period > was_staticized
true
else
false
end
end
end
def staticized
self.staticized_date = DateTime.now
save
end
end
我还在 /config/initializers 中使用
创建了一个文件
ActiveRecord::Base.send :include, Staticizable
并在模型中设置:
class Building < ActiveRecord::Base
check_children :blocks
...
end
我有模特行为。
我想像这样包括:
class Building < ActiveRecord::Base
has_many :blocks
include Staticizable
check_children :blocks #here name of children model to check
...
end
行为看起来像:
module Staticizable
extend ActiveSupport::Concern
module ClassMethods
def check_children child
self.child_model = child #where is to store model name to check?
end
end
def was_staticized
(DateTime.now - self.staticized_date.to_datetime).to_i
end
def staticized?
if @child_model.present?
ap self.send(@child_model)
else
if staticize_period > was_staticized
true
else
false
end
end
end
def staticized
self.staticized_date = DateTime.now
save
end
end
所以我需要知道在哪里可以存储型号名称以检查 children。做这样的事情的正确方法是什么?
终于找到答案了,不确定是不是最好的方法,如果不是,请告诉我:
module Staticizable
extend ActiveSupport::Concern
included do
class << self;
attr_accessor :has_child_custom
end
end
module ClassMethods
def check_children child
self.has_child_custom = child
end
end
def was_staticized
unless staticized_date
return false
end
(DateTime.now - self.staticized_date.to_datetime).to_i
end
def staticized?
if self.class.has_child_custom.present?
self.send(self.class.has_child_custom).each do |child|
unless child.staticized?
return false
end
end
else
if self.staticize_period > was_staticized
true
else
false
end
end
end
def staticized
self.staticized_date = DateTime.now
save
end
end
我还在 /config/initializers 中使用
创建了一个文件ActiveRecord::Base.send :include, Staticizable
并在模型中设置:
class Building < ActiveRecord::Base
check_children :blocks
...
end