How to write a Rails mixer that spans a model, controller, and view

To reduce code duplication in my small Rails application, I worked on getting common code between my models into its separate module, as far as good.

The model element is quite simple, I just need to enable the module at the beginning, for example:

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 works great, now, however, I will have some controller and view code that will be distributed between these models as long as I have this for my posting material:

# 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

Basically, I plan to just include Sendable :: Controller and Sendable :: View (or equivalent) for the controller and view, but is there a cleaner way to do this? I am after a neat way to have a bunch of common code between my model, controller and view.

: , , .

+5
3

( script/generate plugin).

init.rb - :

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

, .

act_ *, (http://github.com/technoweenie/acts_as_paranoid/tree/master/init.rb, 30)

+7

, :

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

# application.rb
class ApplicationController
  include Iso
end

, , helper_method application.rb.

+6

If you are going through a plugin, check out Rails-Engines , which are designed to extend the semantics of the plugin to controllers and views in a clear way.

+1
source

All Articles