title: How to have one concern include another concern with a parameter
body: <p>Suppose you have this code from the documentation of ActiveSupport::Concern, but you want to the included block of Foo to have something different depending on the module or class that including Foo.</p>
  
  <p>In the specific problem I'm trying to solve, I have a set of validations for addresses, but the address fields will be named home_zip_code or work_zip_code, and I want the inclusion of the validation concern to know the prefix of the zip_code field.</p>
  
  <pre><code>require 'active_support/concern'
  
  module Foo
    extend ActiveSupport::Concern
    included do
      # have some_value be accessible 
      def self.method_injected_by_foo
        ...
      end
    end
  end
  
  module Bar
    extend ActiveSupport::Concern
    # set some_value that will used when Foo is included 
    include Foo
  
    included do
      self.method_injected_by_foo
    end
  end
  
  class Host
    include Bar # It works, now Bar takes care of its dependencies
  end
  </code></pre>
  
  <p>I've placed this discussion here: <a href="http://forum.railsonmaui.com/t/how-make-a-concern-parameterized/173" rel="noreferrer">http://forum.railsonmaui.com/t/how-make-a-concern-parameterized/173</a></p>
  
  <p>The following 2 options work. I'm wondering which is preferable.</p>
  
  <h2>A Concern That Uses a Class Method</h2>
  
  <p>This is the concern that needs to be "parameterized":</p>
  
  <pre><code>module Addressable
    extend ActiveSupport::Concern
  
    included do
      zip_field = "#{address_prefix}_zip_code".to_sym
  
      zip_code_regexp = /^\d{5}(?:[-\s]\d{4})?$/
  
      validates zip_field, format: zip_code_regexp, allow_blank: true
  end
  </code></pre>
  
  <p>I found 2 ways to set the address_prefix before including the Addressable concern.</p>
  
  <h2>When the concern module is included in the class</h2>
  
  <p>The class method needs to be defined before including the concern</p>
  
  <pre><code>cattr_accessor :address_prefix
  self.address_prefix = "home"
  include Addressable
  </code></pre>
  
  <p>or like this</p>
  
  <pre><code>def self.address_prefix
      "home"
  end
  include Addressable
  </code></pre>
  
  <h2>When the concern module is included in another module</h2>
  
  <p>The trick here is to override <code>self.append_features</code> and to add the method. </p>
  
  <pre><code>  def self.append_features(base)
      base.class_eval do
        def self.address_prefix
          "home"
        end
      end
      super
    end
  </code></pre>
  
  <p>or </p>
  
  <pre><code>  def self.append_features(base)
      base.cattr_accessor :address_prefix
      base.address_prefix = "home"
      super
    end
  </code></pre>
  
  <h2>Questions</h2>
  
  <ol>
  <li>What preferable, the <code>cattr_accessor</code> way or defining the class method?</li>
  <li>For the concern within a concern situation, is overriding <code>self.append_features</code> the correct hook?</li>
  <li>Is <code>class_eval</code> the right call to create the method, rather than <code>class exec</code>? Or really doesn't matter if the code doesn't need access to instance variables. <a href="http://www.ruby-doc.org/core-2.1.5/Module.html#method-i-class_eval" rel="noreferrer">Module docs here</a>.</li>
  <li>How could I include this concern twice, say for a prefix of "work" and a prefix of "home" so the validations would apply to both. Clearly setting the class method on the including class would not work. Or maybe it would if the method is redefined between inclusions? Any cleaner way?</li>
  </ol>