1

Basically I want to implement a simple Rails extension to define the seriousness of methods in my controller so that I can restrict usage of them appropriately. For example I'd define the default restful actions as so in an abstract superclass:

view_methods :index, :show
edit_methods :new, :create, :edit, :update
destroy_methods :destroy

I'd then down in a non-abstract controller call:

edit_methods :sort

to add in the sort method on that particular controller as being an edit level method.

I could then use a before_filter to check the level of the action currently being performed, and abort it if my logic determines that the current user can't do it.

Trouble is, I'm having trouble working out how to set up this kind of structure. I've tried something like this so far:

class ApplicationController

  @@view_methods = Array.new
  @@edit_methods = Array.new
  @@destroy_methods = Array.new

  def self.view_methods(*view_methods)
    class_variable_set(:@@view_methods, class_variable_get(:@@view_methods) << view_methods.to_a)
  end

  def self.edit_methods(*edit_methods)
    class_variable_set(:@@edit_methods, self.class_variable_get(:@@edit_methods) << edit_methods.to_a)
  end

  def self.destroy_methods(*destroy_methods)
    @@destroy_methods << destroy_methods.to_a
  end

  def self.testing
    return @@edit_methods
  end


  view_methods :index, :show
  edit_methods :new, :create, :edit, :update
  destroy_methods :destroy

end

The three methods above are different on purpose, just to show you what I've tried. The third one works, but returns the same results no matter what controller I test. Probably because the class variables are stored in the application controller so are changed globally.

Any help would be greatly appreciated.

4

2 回答 2

3

问题是您的类变量是继承的,但指向Array. 如果你更新一个,它也会在所有继承了Array.

ActiveSupport通过使用几种方法扩展类来定义可继承的类属性,从而解决了这个问题。它们在 Rails 内部随处使用。一个例子:Class

class ApplicationController
  class_inheritable_array :view_method_list
  self.view_method_list = []

  def self.view_methods(*view_methods)
    self.view_method_list = view_methods  # view_methods are added
  end

  view_methods :index, :show
end

现在您可以设置默认值并在ApplicationController以后覆盖它们。

class MyController < ApplicationController
  view_method :my_method
end

ApplicationController.view_method_list #=> [:index, :show]
MyController.view_method_list #=> [:index, :show, :my_method]

您甚至可以将view_method_list用作控制器上的实例方法(例如MyController.new.view_method_list)。

在您的示例中,您没有定义从列表中删除方法的方法,但想法是执行以下操作(以防万一):

# given the code above...
class MyController
  self.view_method_list.delete :show
end
MyController.view_method_list #=> [:index, :my_method]
于 2009-07-02T07:38:51.863 回答
0

我把它变成了这样的插件:

module ThreatLevel

  def self.included(base)
    base.send :extend, ClassMethods
  end

  module ClassMethods
    def roger_that!
      class_inheritable_array :view_method_list, :edit_method_list, :destroy_method_list

      self.view_method_list = Array.new
      self.edit_method_list = Array.new
      self.destroy_method_list = Array.new

      def self.view_methods(*view_methods)
        self.view_method_list = view_methods
      end

      def self.edit_methods(*edit_methods)
        self.edit_method_list = edit_methods
      end

      def self.destroy_methods(*destroy_methods)
        self.destroy_method_list = destroy_methods
      end

      view_methods :index, :show
      edit_methods :new, :create, :edit, :update
      destroy_methods :destroy
    end
  end
end

ActionController::Base.send :include, ThreatLevel

打电话给roger_that!在您希望它生效的 super_controller 上就可以了。

于 2009-07-03T01:45:21.347 回答