Skip to content

Instantly share code, notes, and snippets.

@westonplatter
Created July 17, 2012 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save westonplatter/3132025 to your computer and use it in GitHub Desktop.
Save westonplatter/3132025 to your computer and use it in GitHub Desktop.
rails_engines_talk
# example take from
# https://github.com/schneems/wicked/blob/40443aea2aba0fe506b9c878fb677f08f2114200/lib/wicked/wizard.rb
require 'active_support/concerns'
module FooBar::Concerns::Controllers::OneController
extend ActiveSupport::Concerns
# controller level methods associated with a Rails-route and View
# IE, the controller structure
def add
end
def subtract
end
def mutliply
end
def divide
end
# Non Rails-route methods used in the controller methods
# for route actions, EG: rendering, redirecting, paths, etc.
# IE, the controller guts
#
# NOTE - these files are not included since it's simple to see
# how they would fit into the file/code structure.
#
include FooBar::Concerns::Controllers::OneController::Render
include FooBar::Concerns::Controllers::OneController::Redirect
include FooBar::Concerns::Controllers::OneController::Path
# First check out this link,
# http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-included
#
# The 'included do' uses the methods within it's block any time
# the 'included do' methods are callled within the MyApp Rails
# application. In other words, 'included do' links method calls in the
# Rails application to the method definitions within the Rails Egnine.
include do
helper_method :foo_bar_render, :foo_bar_redirect, :foo_bar_path
before_filter :foo_bar_setup
end
private
def foo_bar_setup
end
public
end

Transforming rails plugins into rails engines

introduction

briefly cover reasons why plugins and engines are useful

models - active_record relationships models+controllers - authentications - devise views

what are plugins? rails 2.3 style plugins

probably seen this message,

You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released

++ need to better describe the structure of plugins
++ need to better describe how Rails 2.3 plugins are loaded within rails

console message and blog post
-- how to turn off the deprecation warning

what are rails engines?

types [mountable, full] core structural components integration with rails

++ note how plugins and engines differ

transformation process with questionnaire_engine

core changes

implemented an isolated namespace
lib/qe/engine.rb

gemspec
++ need to verify gemspec dependencies

models and controllers

we wanted to leverage Ruby Mixins rather than open classing - to more explicitly control engine classes and MainApp class interconnectivity. therefore, ActiveSupport::Concern

views
assets

javascripts
stylesheets
images

++ can you precompile these in the gem?

testing framework

spec/dummy
RSpec configurations
testing tools

put some code here
require 'active_support/concerns'
module FooBar::Concerns::Models::One
extend ActiveSupport::Concerns
# HOW TO INCLUDE ACTIVE RECORD ASSOCIATIONS WITHIN MODULES THAT
# ARE CLASS X < ACITVERECORD::BASE
#
# http://my.safaribooksonline.com/book/web-development/ruby/9780132480345/advanced-active-record/ch09lev1sec10
# the 'included do' serves as a callback to the module class (IE module One)
# such that when the module is included in the class
# (IE, class SomeClass < ActiveRecord::Base), the module's
# 'has_many :child_objects' and 'belongs_to :parent_object' will be executed
# within the ActiveRecord::Base context rather than this module's context,
# which does have have class definitions for 'has_many' and 'belongs_to'.
included do
has_many :child_objects
belongs_to :parent_objects
end
# methods in the module are Instance Methods
def model_methods_treated_as_instance_methods
end
end

Gist to describe the process of adding a namespaced isolated Rails Engine
to a typical Rails application and adding methods to one of the Rails Engine's
controller and model.

Index

  1. Rails applicaiton all by itself.
  2. Rails application with the Rails Engine (FooBar) mounted.
  3. Rails applicaiton partially extending a controller/model from the Rails Engine.

1. Rails application all by itself

MyApp
 |--- controllers
 |             |--- a_controller.rb
 |             |--- b_controller.rb
 |             |--- c_controller.rb
 |
 |-------- models
 |             |--- a.rb
 |             |--- b.rb
 |             |--- c.rb
 |
 |-------- ... and other rails folders

2. Rails applicaiton with the Rails Engine (FooBar) mounted

MyApp
 |--- controllers
 |             |--- a_controller.rb
 |             |--- b_controller.rb
 |             |--- c_controller.rb
 |
 |-------- models
 |             |--- a.rb
 |             |--- b.rb
 |             |--- c.rb             
 |
 |---- config
 |       |---- application.rb ( require 'foobar' )
 |       |---- routes.rb ( mount FooBar::Engine. :at => '/foobar' )
 |
 |-------- Gemfile ... gem 'foo_bar'
 |
 |-------- ... and other rails folders


FooBar
    |--- app
    |     |--- controllers
    |     |           |--- one_controller.rb ( include FooBar::Concerns::Controllers::OneController )
    |     |           |--- two_controller.rb ( include FooBar::Concerns::Controllers::TwoController )
    |     |
    |     |-------- models
    |     |           |--- one.rb ( include FooBar::Concerns::Models::One )
    |     |           |--- two.rb ( include FooBar::Concerns::Models::Two )
    |     |--- views
    |             |--- one
    |             |     |--- add.html.erb
    |             |     |--- subtract.html.erb
    |             |     |--- multiply.html.erb
    |             |     |--- divide.html.erb
    |             |
    |             |--- two
    |                   |--- add.html.erb
    |                   |--- subtract.html.erb
    |                   |--- multiply.html.erb
    |                   |--- divide.html.erb    
    |             
    |---- lib
           |--- foo_bar.rb
           |--- foo_bar
                     |--- engine.rb
                     |--- cocnerns
                             |--- controllers
                             |           |--- one_controller.rb ( within file, extend ActiveSupport::Concern )
                             |           |--- two_controller.rb ( within file, extend ActiveSupport::Concern )
                             | 
                             |-------- models
                                         |--- one.rb ( within file, extend ActiveSupport::Concern )
                                         |--- two.rb ( within file, extend ActiveSupport::Concern ) 

3. Rails application partially extending a controller/model from the Rails Engine.

We want to add a rails engine (FooBar) and expand the functionality of the

  • FooBar::Concerns::Controllers::OnesController in a_controller.rb
  • FooBar::Concerns::Models::One in a.rb
MyApp
 |--- controllers
 |             |--- a_controller.rb ( include FooBar::Concerns::Controllers::OnesController )
 |             |--- b_controller.rb
 |             |--- c_controller.rb
 |
 |-------- models
 |             |--- a.rb ( include FooBar::Concerns::Models::One )
 |             |--- b.rb
 |             |--- c.rb             
 |
 |---- config
 |       |---- application.rb ( require 'foobar' )
 |       |---- routes.rb ( mount FooBar::Engine. :at => '/foobar' )
 |
 |-------- Gemfile ... gem 'foo_bar'
 |
 |-------- ... and other rails folders


FooBar
    |--- app
    |     |--- controllers
    |     |           |--- one_controller.rb ( include FooBar::Concerns::Controllers::OneController )
    |     |           |--- two_controller.rb ( include FooBar::Concerns::Controllers::TwoController )
    |     |
    |     |-------- models
    |     |           |--- one.rb ( include FooBar::Concerns::Models::One )
    |     |           |--- two.rb ( include FooBar::Concerns;:Models::Two )
    |     |--- views
    |             |--- one
    |             |     |--- add.html.erb
    |             |     |--- subtract.html.erb
    |             |     |--- multiply.html.erb
    |             |     |--- divide.html.erb
    |             |
    |             |--- two
    |                   |--- add.html.erb
    |                   |--- subtract.html.erb
    |                   |--- multiply.html.erb
    |                   |--- divide.html.erb    
    |             
    |---- lib
           |--- foo_bar.rb
           |--- foo_bar
                     |--- engine.rb
                     |--- concerns
                             |--- controllers
                             |           |--- one_controller.rb ( within file, extend ActiveSupport::Concern )
                             |           |--- two_controller.rb ( within file, extend ActiveSupport::Concern )
                             | 
                             |-------- models
                                         |--- one.rb ( within file, extend ActiveSupport::Concern )
                                         |--- two.rb ( within file, extend ActiveSupport::Concern ) 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment