1

Update!

I have sorted out the issue with the concerns, although I definitely appreciate the ClassMethods suggestion, as it seems to improve the code quality.

The issue with the concern was (and it was a dumb mistake) I removed a concern and combined it with another one and so that concern truly did not exist. Concerns therefore appear to be working, but I was still wondering if there is a way to abstract helpers out so they are not duplicated. Right now I have a lot of lines of duplicated helper methods that I'd like to remove if possible.

Original question

I have a Rails application that I am building from scratch. Since the scope of it is going to be very large, I am splitting functionality into separate engines to hopefully make it a lot more maintainable in the long term.

That said, I haven't worked in this pattern before and a couple things are tripping me up.

The biggest issue I am coming across is this:

There are some helpers and concerns that I have defined to keep my code as DRY as possible, however you cannot share these concerns in a production environment (which I halfway expected).

I would like to not duplicate these concerns in the various engines, instead I would like to only define them in one place.

For example, a model directory in the main app (which only contains stuff that all engines end up using) contains the following files (as an example):

app \
  models \
    concerns \
      mongoidable.rb # Contains Mongoid include statements and basic set up
      versionable.rb # Contains versioning code
      searchable.rb # Contains basic searching code to set up ElasticSearch in a basic way

Etc...

An example of the concern is:

module Mongoidable
  extend ActiveSupport::Concern

  module ClassMethods
    def column_names
      fields.collect { |field| field[0] }
    end
  end

  included do
    # All Mongoid documents will be timestamped and versioned by default.
    #
    # There isn't much reason why not to.
    include Mongoid::Document
    include Mongoid::Timestamps
    include Mongoid::Userstamp
    include Mongoid::Audit::Trackable

    track_history track_create: true, track_delete: true, track_update: true
  end
end

When I try to include them in models in engines, it doesn't work, and I have a feeling this is by design, but I was wondering if there is a correct way to do this.

Should it be abstracted into a gem?

If so, what are some ideas on getting that gem set up?

Thank you very much for any assistance, I want to keep the code pretty dry since the code base is only going to expand.

Mylan Connolly
  • 494
  • 1
  • 5
  • 15
  • What is the code structure inside the app/models/concerns/*.rb file? is it module or class? – Sanket Jun 19 '14 at 14:58
  • Sorry, I added an example to the question. Thanks! – Mylan Connolly Jun 19 '14 at 15:01
  • shouldn't `column_names` be put inside a `module ClassMethods` ? (see [the introductory post](http://signalvnoise.com/posts/3372-put-chubby-models-on-a-diet-with-concerns)) – Louis Kottmann Jun 19 '14 at 15:10
  • Thank you for the tip; I browsed that article and it looks like you are entirely correct. Modified the source code to reflect this. :-). Modified original question again to reflect this. – Mylan Connolly Jun 19 '14 at 15:40

0 Answers0