如何编写跨越模型,控制器和视图的Rails mixin

时间:2020-03-05 18:54:54  来源:igfitidea点击:

为了减少我的小Rails应用程序中的代码重复,到目前为止,我一直在努力将模型之间的通用代码放入其自己的单独模块中。

该模型的工作相当简单,我只需要在开始时就包含该模块,例如:

class Iso < Sale
  include Shared::TracksSerialNumberExtension
  include Shared::OrderLines
  extend  Shared::Filtered
  include Sendable::Model

  validates_presence_of   :customer
  validates_associated    :lines

  owned_by :customer

  def initialize( params = nil )
    super
    self.created_at ||= Time.now.to_date
  end

  def after_initialize
  end

  order_lines             :despatched

  # tracks_serial_numbers   :items
  sendable :customer

  def created_at=( date )
    write_attribute( :created_at, Chronic.parse( date ) )
  end
end

一切正常,但是现在,我将拥有一些控制器和视图代码,这些代码在这些模型之间也将是通用的,到目前为止,我已经将其用于可发送内容:

# This is a module that is used for pages/forms that are can be "sent"
# either via fax, email, or printed.
module Sendable
  module Model
    def self.included( klass )
      klass.extend ClassMethods
    end

    module ClassMethods
      def sendable( class_to_send_to )
        attr_accessor :fax_number,
                      :email_address,
                      :to_be_faxed,
                      :to_be_emailed,
                      :to_be_printed

        @_class_sending_to ||= class_to_send_to

        include InstanceMethods
      end

      def class_sending_to
        @_class_sending_to
      end
    end # ClassMethods

    module InstanceMethods
      def after_initialize( )
        super
        self.to_be_faxed    = false
        self.to_be_emailed  = false
        self.to_be_printed  = false

        target_class = self.send( self.class.class_sending_to )
        if !target_class.nil?
          self.fax_number     = target_class.send( :fax_number )
          self.email_address  = target_class.send( :email_address )
        end
      end
    end
  end # Module Model
end # Module Sendable

基本上,我打算只为控制器和视图做一个包含Sendable :: Controller和Sendable :: View(或者等效项)的方法,但是,有没有更干净的方法呢?我正在以一种巧妙的方式在模型,控制器和视图之间使用一堆通用代码。

编辑:只是为了澄清,这只需要在2或者3个模型之间共享。

解决方案

回答

如果需要将该代码添加到所有模型和所有控制器中,则可以始终执行以下操作:

# maybe put this in environment.rb or in your module declaration
class ActiveRecord::Base
  include Iso
end

# application.rb
class ApplicationController
  include Iso
end

如果需要该模块的功能可用于视图,则可以使用application.rb中的helper_method声明分别公开它们。

回答

我们可以对其进行插件(使用脚本/生成插件)。

然后在init.rb中执行以下操作:

ActiveRecord::Base.send(:include, PluginName::Sendable)
ActionController::Base.send(:include, PluginName::SendableController)

并且与self.included一起工作也很好。

检查一些act_ *插件,这是一个非常常见的模式(http://github.com/technoweenie/acts_as_paranoid/tree/master/init.rb,请检查第30行)

回答

如果我们使用插件路线,请检查Rails-Engines,它们旨在将插件语义清楚地扩展到Controllers和Views。