新紅寶石\ 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時。我做錯了什麼?
你是什麼意思都進不去呢,該變量不存在? –