Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save daemianmack/3151794 to your computer and use it in GitHub Desktop.
Save daemianmack/3151794 to your computer and use it in GitHub Desktop.
using ActiveSupport::Concerns to extend Rails

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 ) 
@bingxie
Copy link

bingxie commented Aug 9, 2014

I just followed this instruction, but I got: uninitialized constant Concerns::Models exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment