Rails Mountable Engine Creation, installation, unistallation


Apidock For Engine Class
Stack OverFlow Engine Routes Problem

Engine Points

Engine Creation command

rails plugin new blorgh --mountable

The –mountable option tells the generator that you want to create a “mountable” and namespace-isolated engine.

A file at lib/blorgh/engine.rb which is identical in function to a standard Rails application’s config/application.rb file:


module Blorgh
 class Engine < ::Rails::Engine
 end
end

Referenced From: Rails Guide Engine Creation

Structure of basic engine ( Used in Appatrip project) generators (install and uninstall) :

  1. Directory structure: a. libs/generators/gem_name/install_generator.rb b. libs/generators/gem_name/uninstall_generator.rb
  2. Usages: used to install and uninstall engine.
  3. commands: a.  rails g gem_name:install b.  rails g gem_name:uninstall

# libs/generators/gem_name/install_generator.rb
module GemName
  module Generators
   class InstallGenerator < Rails::Generators::Base
     source_root File.expand_path('../../../../app/views/gem_name', __FILE__)

     desc "Copies view to your application."

     def copy_views
       filename_pattern = File.join self.class.source_root, "*.html.erb"
       Dir.glob(filename_pattern).map {|f| File.basename f}.each do |f|
          copy_file f, "app/views/contoller_folder/#{f}"
       end
     end

     def copy_migrations
        rake("gem_name:install:migrations")
     end

    end
  end
end

 libs/generators/gem_name/uninstall_generator.rb

module GemName
  module Generators
    class UninstallGenerator < Rails::Generators::Base
      desc "Remove views and migrations from your application."

      def remove_views
        %w(map multi_map).each do |f|
          file = f.concat ".html.erb"
          remove_file "app/views/pages/content/_#{file}"
        end
      end

      def remove_migrations
        #first manually run this comman "rake db:migrate:down VERSION=version_of_migration"
        remove_file "db/migrate/20130305104326_create_maps.rb"
        remove_file "db/migrate/20130305105841_create_multi_maps.rb"
      end

    end
  end
end

Installation:


1) gem 'gem_name', :path => 'vendor/engines/gem_name'
Note: All engines must be placed inside verndor/engines directory.
2) For installing views run rails g gem_name:install
3) For removing views run rails g gem_name:uninstall
4) rake ENGINE_NAME:install:migrations (if you want manuall installation)

Non Issolated Engine:


If you don't want to create isolated namespace engine 
1. goto lib/engine_name/engine.rb file and
2. just replace this line isolate_namespace EngineName with this engine_name "your_engine_name"
 

Important To Note:


1. Make your engine view files in app/views/engine_name/ folder. If engine_name does not exists create manually.
2. Rmove test/dumy/ folder in engine. It contains dummy application.
3. Remove nbproject folder which is created by netbeans.
4. create engine_name.gemspec file on route directory of your engine .

Run rails command in Engine:

script/rails g model name
Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with: , , , , , ,
Posted in Gems and plugins

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: