新红宝石\ Rails的,可耻的是我:(红宝石混入最佳实践
我正在开发用于个人用途(简单的管理面板)的发动机。我想要什么,是要能够配置我的主要应用程序的模型,像这样:
class User < ActiveRecord::Base
include Entropy::Configurable
entropy_config do
form_caption 'Editing user'
end
end
然后在发动机的模板,这样做:
<h1><%= @object.entropy_config :form_caption %></h1>
引擎模块:
module Entropy
module Configurable
def self.included(base)
## to call entropy_config in model class
base.send :extend, ClassMethods
end
def entropy_config(arg)
## ... I'm missing this part
end
module ClassMethods
@@config = { ... }
def entropy_config (&block)
class_eval &block
end
def form_caption(arg)
// skipping class identification
@@config[:user][:form_caption] = arg
end
end
end
end
问题是我无法从Configurable模块访问@@ config,实际上当我在@object上调用entropy_config时。我做错了什么?
你是什么意思都进不去呢,该变量不存在? –