<p>In an effort to reduce code duplication in my little Rails app, I've been working on getting common code between my models into it's own separate module, so far so good.</p>

<p>The model stuff is fairly easy, I just have to include the module at the beginning, e.g.:</p>

<pre><code>class Iso &lt; 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
</code></pre>

<p>This is working fine, now however, I'm going to have some controller and view code that's going to be common between these models as well, so far I have this for my sendable stuff:</p>

<pre><code># 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
</code></pre>

<p>Basically I'm planning on just doing an include Sendable::Controller, and Sendable::View (or the equivalent) for the controller and the view, but, is there a cleaner way to do this?  I 'm after a neat way to have a bunch of common code between my model, controller, and view.</p>

<p>Edit: Just to clarify, this just has to be shared across 2 or 3 models.</p>

[{'answer_id': 68934, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 3, 'selected': False, 'text': '<p>If that code needs to get added to all models and all controllers, you could always do the following:</p>\n\n<pre><code># maybe put this in environment.rb or in your module declaration\nclass ActiveRecord::Base\n  include Iso\nend\n\n# application.rb\nclass ApplicationController\n  include Iso\nend\n</code></pre>\n\n<p>If you needed functions from this module available to the views, you could  expose them individually with <code>helper_method</code> declarations in application.rb.</p>\n'}, {'answer_id': 69190, 'author': 'nikz', 'author_id': 3977, 'author_profile': 'https://Stackoverflow.com/users/3977', 'pm_score': 4, 'selected': True, 'text': '<p>You could pluginize it (use script/generate plugin).</p>\n\n<p>Then in your init.rb just do something like:</p>\n\n<pre><code>ActiveRecord::Base.send(:include, PluginName::Sendable)\nActionController::Base.send(:include, PluginName::SendableController)\n</code></pre>\n\n<p>And along with your self.included that should work just fine.</p>\n\n<p>Check out some of the acts_* plugins, it\'s a pretty common pattern (<a href="http://github.com/technoweenie/acts_as_paranoid/tree/master/init.rb" rel="nofollow noreferrer">http://github.com/technoweenie/acts_as_paranoid/tree/master/init.rb</a>, check line 30)</p>\n'}, {'answer_id': 69405, 'author': 'user11058', 'author_id': 11058, 'author_profile': 'https://Stackoverflow.com/users/11058', 'pm_score': 1, 'selected': False, 'text': '<p>If you do go the plugin route, do check out <a href="http://rails-engines.org/" rel="nofollow noreferrer">Rails-Engines</a>, which are intended to extend plugin semantics to Controllers and Views in a clear way. </p>\n'}]
['https://Stackoverflow.com/questions/68391', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/841/']