title: Sharing a mongoid model between 2 applications - using engine vs. plugin
body: <p>I want to share a model between 2 (maybe more in the future) of my rails apps. I couldn't find any clear suggestions, but I picked up some of the questions and answers I've read and came to a conclusion that it has to be done with a "gemmed" plugin engine. 
  I decide to go with an plugin, because I read that engine is simply a kind of a "full" plugin.</p>
  
  <p>So I created a plugin using: <code>rails plugin new my_models --skip-active-record --skip-test-unit --dummy-path=spec/dummy</code> (the options are for skipping activerecord as an ORM and using rspec for testing).</p>
  
  <p>After I created the plugin, I got the following files:</p>
  
  <pre><code>my_models.gemspec  Gemfile  Gemfile.lock  lib  MIT-LICENSE  Rakefile  README.rdoc  spec
  </code></pre>
  
  <p>I tried to include the model using the following methods:</p>
  
  <ol>
  <li>Just creating an <code>app/models</code> directory and put my model inside</li>
  <li>As suggested in <a href="http://www.smashingmagazine.com/2011/06/23/a-guide-to-starting-your-own-rails-engine-gem/">this tutorial</a> (and I could see in <a href="https://github.com/plataformatec/devise/blob/master/lib/generators/mongoid/devise_generator.rb">devise's github</a>), I created a generator in an attempt to generate the model.</li>
  </ol>
  
  <p>Both of them failed, and then I decided to go with the engine suggestion (by just adding <code>--mountable</code> to the options list of the "rails new" command), I got the full rails app structure (with app, bin, db and the rest of the directories), put my model in the app/models dir and it worked like a magic! </p>
  
  <p>As I believe I'm a programmer and not I magician, I don't to do such magics, so can you tell me what's wrong with both of my thin plugin solutions (using generator/creating a model)?? Moreover, what are the advantages of using those generators?</p>
  
  <p>I'm attaching my generator's code, maybe I miss something:</p>
  
  <pre><code>require 'rails/generators/named_base'
  require 'mongoid'
  
  module Mongoid
    module AttackGenerator
      def generate_model
        invoke "mongoid:model", [name] unless model_exists? &amp;&amp; behavior == :invoke
      end
  
      def inject_field_types
        inject_into_file model_path, migration_data, after: "include Mongoid::Document\n" if model_exists?
      end
  
      def migration_data
        field :link_url, type: String
        field :token, type: String
      end
  
      def model_exists?
        File.exists?(File.join(destination_root, model_path))
      end
  
      def model_path
        @model_path ||= File.join("app", "models", "#{file_path}.rb")
      end
    end
  end
  </code></pre>