代码之家  ›  专栏  ›  技术社区  ›  Brendon Muir

Ruby on Rails模式,用于设置类默认值并允许对子类进行重写

  •  1
  • Brendon Muir  · 技术社区  · 17 年前

    基本上,我希望实现一个简单的Rails扩展来定义控制器中方法的严重性,以便适当地限制它们的使用。例如,我将在抽象超类中定义默认的RESTful操作:

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

    然后我会在一个非抽象的控制器调用中关闭:

    edit_methods :sort
    

    将该特定控制器上的排序方法添加为编辑级方法。

    然后,我可以使用before_过滤器检查当前正在执行的操作的级别,如果我的逻辑确定当前用户无法执行该操作,则中止该操作。

    问题是,我很难想出如何建立这种结构。到目前为止,我已经尝试过类似的方法:

    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
    

    上面的三种方法是不同的,只是为了向你展示我所尝试过的。第三个可以工作,但返回相同的结果,无论我测试的是哪个控制器。可能是因为类变量存储在应用程序控制器中,所以全局更改。

    任何帮助都将不胜感激。

    2 回复  |  直到 17 年前
        1
  •  3
  •   molf    17 年前

    问题是类变量是继承的,但指向 Array . 如果更新一个,它也将在继承 数组 .

    ActiveSupport 提供一个 solution 通过扩展 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]
    
        2
  •  0
  •   Brendon Muir    17 年前

    我把它变成了这样一个插件:

    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
    

    打电话给罗杰!在你想让它生效的超级控制器上,这是一个技巧。

    推荐文章