Ruby on rails 有没有办法把信息从这个街区里拿出来
所以我有了rails控制器Ruby on rails 有没有办法把信息从这个街区里拿出来,ruby-on-rails,ruby,ruby-on-rails-3,block,Ruby On Rails,Ruby,Ruby On Rails 3,Block,所以我有了rails控制器 class SomeController < ApplicationController include Something ... require_information do more_infomation "Stuff" do data :stuff_i_want_to_access, [:index] data :more_stuff_i_want_to_access, [:edit] data :
class SomeController < ApplicationController
include Something
...
require_information do
more_infomation "Stuff" do
data :stuff_i_want_to_access, [:index]
data :more_stuff_i_want_to_access, [:edit]
data :more_stuff_i_want_to_access, [:show]
end
end
如果可能的话,有什么想法吗
更新:我不认为DSL很重要,因为它只是处理权限,但这里是DSL
module Something
extend ActiveSupport::Concern
included do
class_attribute :data_information
prepend_before_filter :verify_access
end
module ClassMethods
def require_information &block
self.data_information = []
self.instance_eval &block if block_given?
end
def more_infomation *name, &block
@data = []
self.instance_eval &block if block_given?
self.data_information << { name: name, data: @data.dup }
@data.clear
end
def data *action, &block
@data.push(action)
end
end
def verify_access
# Do stuff
# I have access to data_information by self.class.data_information
end
end
modulesomething
扩展ActiveSupport::关注点
包括做
类属性:数据信息
在\u筛选器之前预结束\u:验证\u访问
结束
模块类方法
def需要_信息和块
self.data_信息=[]
self.instance\u eval&block如果给定block\u?
结束
定义更多信息*名称和块
@数据=[]
self.instance\u eval&block如果给定block\u?
self.data\u信息您可以通过以下方式访问数据:
SomeController.data_information
这将给你:
[{:name=>["Stuff"],
:data=>
[[:stuff_i_want_to_access, [:index]],
[:more_stuff_i_want_to_access, [:edit]],
[:more_stuff_i_want_to_access, [:show]]]}]
DSL实现对于共享非常重要,因为DSL实现告诉您信息存储的位置,这使其他人能够看到信息。您不需要共享DSL实现的唯一方法是,如果DSL本身提供了一种机制来访问以前存储的信息。那么问题是什么?问题是如何访问数据值。例如:stuff_i_想__访问,[:index]来自控制器外部控制器是控制器。你可以使用一个模型来存储或保存信息,我尝试过,但似乎总是得到一个空数组……这可能是因为我需要先实例化这个控制器的一个实例吗?
[{:name=>["Stuff"],
:data=>
[[:stuff_i_want_to_access, [:index]],
[:more_stuff_i_want_to_access, [:edit]],
[:more_stuff_i_want_to_access, [:show]]]}]