简体   繁体   中英

Is there a way to information out of this block

so i have my rails controller

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

The require_information DSL is something I created for us to have some custom permissions in the application but the problem is I need to access that data from somewhere else. Is there a way to access the 3 data elements from another location. I tried

SomeController.class_variables
=> []
SomeController.require_information
=> nil
SomeController.require_information2
NoMethodError: undefined method `require_permissionsd' for SomeController:Class

Any ideas if this is even possible

UPDATE: I didnt think the DSL was important because its just dealing with permissions but here is the 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

You can access the data with:

SomeController.data_information

which will give you:

[{:name=>["Stuff"],
  :data=>
   [[:stuff_i_want_to_access, [:index]],
    [:more_stuff_i_want_to_access, [:edit]],
    [:more_stuff_i_want_to_access, [:show]]]}]

The DSL implementation was important to share because it's the DSL implementation that tells you where the information is being stored, which is what enables others to see it. The only way you wouldn't have needed to share the DSL implementation is if the DSL had provided a mechanism in the DSL itself to access the information stored previously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM