Skip to content

Instantly share code, notes, and snippets.

@mtanzi
Forked from maxivak/readme.md
Created February 1, 2021 22:36
Show Gist options
  • Save mtanzi/fab4ee08d54a4a051fa7ad640d968c3a to your computer and use it in GitHub Desktop.
Save mtanzi/fab4ee08d54a4a051fa7ad640d968c3a to your computer and use it in GitHub Desktop.

Revisions

  1. @maxivak maxivak revised this gist May 10, 2018. 1 changed file with 53 additions and 2 deletions.
    55 changes: 53 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -475,14 +475,65 @@ Place Gem's assets in folder 'mygem' so that they will be distinct from main app



    * include assets you want to be precompiled in Gem's:
    config/initializers/assets.rb (this file is part of the Gem)
    * include asset files to be precompiled:

    define precompiled assets in Gem:

    config/initializers/assets.rb
    ```
    Rails.application.config.assets.precompile += %w( mygem/style1.css )
    Rails.application.config.assets.precompile += %w( mygem/mygem1.js )
    ```

    /lib/mygem/engine.rb

    ```
    module Mygem
    class Engine < ::Rails::Engine
    isolate_namespace Optimacms
    # for Rails 5
    config.enable_dependency_loading = false
    config.eager_load_paths += %W( #{Mygem::Engine.root}/lib )
    config.watchable_dirs['lib'] = [:rb] if Rails.env.development?
    config.watchable_dirs['app/helpers'] = [:rb] if Rails.env.development?
    config.autoload_paths += Dir["#{Mygem::Engine.root}/app/helpers/"]
    config.autoload_paths += Dir["#{Mygem::Engine.root}/lib/"]
    config.autoload_paths += Dir["#{Mygem::Engine.root}/lib/**/"]
    #config.autoload_paths += Dir["#{Mygem::Engine.root}/lib/concerns/"]
    #config.autoload_paths += Dir["#{Mygem::Engine.root}/lib/concerns/**/"]
    config.autoload_paths += Dir["#{Mygem::Engine.root}/lib/mygem/"]
    config.before_initialize do
    ActiveSupport.on_load :action_controller do
    include ::Optimacms::Mycontroller
    ::ActionController::Base.helper Optimacms::Engine.helpers
    end
    end
    initializer "optimacms assets precompile" do |app|
    app.config.assets.precompile += %w(mygem/mygem1.js.css mygem/style1.css )
    end
    end
    end
    ```



    ### Use assets in Gem
  2. @maxivak maxivak revised this gist May 9, 2018. 1 changed file with 2 additions and 9 deletions.
    11 changes: 2 additions & 9 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -499,13 +499,6 @@ Use Gem's assets in the Gem the same way as in usual Rails app.
    ```

    * in view:

    app/assets/stylesheets







    @@ -623,7 +616,7 @@ To fix this you have to change url(..) with asset-url(..) in css files in the mo
    References:
    * font-awesome package with Rails asset pipeline:
    ** https://stackoverflow.com/questions/48572272/rails-asset-pipeline-working-with-fonts-and-yarn/49202226#49202226
    ** https://stackoverflow.com/questions/42059621/rails-5-rails-assets-fontawesome-does-not-load-fonts/49202142#49202142
    * https://stackoverflow.com/questions/48572272/rails-asset-pipeline-working-with-fonts-and-yarn/49202226#49202226
    * https://stackoverflow.com/questions/42059621/rails-5-rails-assets-fontawesome-does-not-load-fonts/49202142#49202142
  3. @maxivak maxivak revised this gist May 9, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -457,7 +457,7 @@ A gem may come with assets - css, js or image files.
    We want to use assets included in gem from main application.


    * *Gem/Engine structure*:
    * **Gem/Engine structure**:

    * package.json

  4. @maxivak maxivak revised this gist May 9, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -457,7 +457,7 @@ A gem may come with assets - css, js or image files.
    We want to use assets included in gem from main application.


    * '''Gem/Engine structure''':
    * *Gem/Engine structure*:

    * package.json

  5. @maxivak maxivak revised this gist May 9, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -450,13 +450,14 @@ It will include all of the helpers from engine's directory. Take into account th

    <a name="assets"/>

    # Access assets from Engine
    # Access Engine's assets from main app

    A gem may come with assets - css, js or image files.

    We want to use assets included in gem from main application.

    Gem/Engine structure:

    * '''Gem/Engine structure''':

    * package.json

  6. @maxivak maxivak revised this gist May 9, 2018. 1 changed file with 171 additions and 1 deletion.
    172 changes: 171 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -454,5 +454,175 @@ It will include all of the helpers from engine's directory. Take into account th

    A gem may come with assets - css, js or image files.

    We want to access assets in gem from main application.
    We want to use assets included in gem from main application.

    Gem/Engine structure:

    * package.json

    * app/assets/images/mygem/
    * img1.jpg

    * app/assets/javascripts/mygem/
    * mygem1.js

    * app/assets/stylesheets/mygem/
    * style1.scss


    Place Gem's assets in folder 'mygem' so that they will be distinct from main app's assets.



    * include assets you want to be precompiled in Gem's:
    config/initializers/assets.rb (this file is part of the Gem)
    ```
    Rails.application.config.assets.precompile += %w( mygem/style1.css )
    Rails.application.config.assets.precompile += %w( mygem/mygem1.js )
    ```



    ### Use assets in Gem

    Use Gem's assets in the Gem the same way as in usual Rails app.



    * in view `app/views/mygem/folder1/example.html.haml`:
    ```
    = javascript_include_tag "mygem/mygem1.js"
    = stylesheet_link_tag 'mygem/style1', media: 'all'
    ```

    * in view:

    app/assets/stylesheets







    ### Use Gem's assets in main application.

    * main app's js file `app/assets/stylesheets/myapp.js`
    ```
    //= require mygem/mygem1.js
    ```


    * main app's scss file `app/assets/stylesheets/myapp.scss`
    ```
    ...
    @import "mygem/style1";
    ```



    ### Use npm packages in gem
    A gem may include node packages with yarn.

    In order to use js, css, image, font files in a node module with Rails assets pipeline we need to add those files to assets precompile paths:

    in Gem file config/initializers/assets.rb:

    ```
    Rails.application.config.assets.paths << Mygem::Engine.root.join('node_modules')
    ```


    Now you can use files from a package in gem's assets files.


    * package.json - this file is part of the Gem
    ```
    {
    "name": "mygem",
    "version": "1.0.0",
    "main": "index.js",
    "repository": "[email protected]:maxivak/mygem.git",
    "author": "mmx <[email protected]>",
    "license": "MIT",
    "dependencies": {
    "jquery": "3.2.1",
    "jquery-ui-dist": "1.12.1",
    "jquery-ujs": "1.2.2",
    "bootstrap": "4.1.0",
    "popper.js": "1.14.3",
    "font-awesome": "4.7.0"
    }
    }
    ```



    * use assets from node modules in Gem

    * app/assets/javascripts/mygem/mygem1.js

    ```
    //= require jquery/dist/jquery
    //= require jquery-ujs
    //= require jquery-ui-dist/jquery-ui
    //= require popper.js/dist/umd/popper.js
    //= require bootstrap/dist/js/bootstrap
    ````
    * app/assets/stylesheets/mygem/style1.scss
    ```
    ...
    // Bootstrap and its default variables
    @import "bootstrap/scss/bootstrap";


    ```
    * IMPORTANT! You may have problems if a node module refers to assets without using Rails asset helpers.
    For example,
    * node module includes css file which refers to images:
    * css/some_module.css
    ```
    .somestyle{
    background-image: url("../img/back1.jpg");
    }

    ```
    * module has images im `img` folder.
    Then it will not work in Rails.
    To fix this you have to change url(..) with asset-url(..) in css files in the module:
    * rename css files to scss
    * scss/some_module.scss:
    ```
    .somestyle{
    background-image: asset-url("module-name/img/back1.jpg");
    }

    ```
    References:
    * font-awesome package with Rails asset pipeline:
    ** https://stackoverflow.com/questions/48572272/rails-asset-pipeline-working-with-fonts-and-yarn/49202226#49202226
    ** https://stackoverflow.com/questions/42059621/rails-5-rails-assets-fontawesome-does-not-load-fonts/49202142#49202142
  7. @maxivak maxivak revised this gist May 9, 2018. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@
    - [Add functionality to controller](#controllers)
    - [Improving (Extending or overriding) Engine functionality](#extend-engine-class)
    - [Helpers](#helpers)
    - [Assets](#assets)


    ## Paths
    @@ -442,4 +443,16 @@ class ApplicationController < ActionController::Base
    end
    ```

    It will include all of the helpers from engine's directory. Take into account that this does not include helpers defined in controllers with helper_method or other similar solutions, only helpers defined in the helpers directory will be included.
    It will include all of the helpers from engine's directory. Take into account that this does not include helpers defined in controllers with helper_method or other similar solutions, only helpers defined in the helpers directory will be included.




    <a name="assets"/>

    # Access assets from Engine

    A gem may come with assets - css, js or image files.

    We want to access assets in gem from main application.

  8. @maxivak maxivak revised this gist May 9, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -216,7 +216,9 @@ end
    ```



    <a name="extend-engine-class"/>

    ## Improving (Extending or overriding) Engine functionality

    A common task after including Engine in your Rails app is extending some classes (models, controllers, other classes) defined in the Engine.
  9. @maxivak maxivak revised this gist May 5, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -419,6 +419,7 @@ Myengine::Mymodel.send(:include, MyengineExtensions)


    <a name="helpers"/>

    ## Use helpers defined in the Engine

    * Isolated engine's helpers
  10. @maxivak maxivak revised this gist May 5, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -158,6 +158,7 @@ Find more in [discussion on stackoverflow](http://stackoverflow.com/questions/75


    <a name="controllers"/>

    ## Controller

    * We want to create a method in the Engine which will extend functionality of our app controller like this:
  11. @maxivak maxivak revised this gist May 5, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,7 @@ yaml_obj = YAML.load(gem_root + "/file_name.yaml")


    <a name="routes"/>

    ## Routes

    Routes inside an engine are isolated from the application by default.
  12. @maxivak maxivak revised this gist May 5, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@ yaml_obj = YAML.load(gem_root + "/file_name.yaml")

    <a name="routes"/>
    ## Routes

    Routes inside an engine are isolated from the application by default.
    The application and its engines can have routes with the same names.

  13. @maxivak maxivak revised this gist Sep 25, 2017. No changes.
  14. @maxivak maxivak revised this gist Sep 25, 2017. 1 changed file with 56 additions and 0 deletions.
    56 changes: 56 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -359,6 +359,62 @@ end
    ```


    ### Extend Engine model class using ActiveSupport::Concern

    We have a model defined in Engine (gem).

    ```ruby
    # myengine/app/models/myengine/mymodel.rb
    module Myengine
    class Myclass < ActiveRecord::Base



    end
    end


    ```

    One way to reopen a class in main app is defining an extension in app/extensions folder.
    ```ruby
    # myapp/app/extensions/myengine/mymodel.rb

    require Myengine::Engine.root.join('app/models/myengine/mymodel')


    module MyengineExtensions
    extend ActiveSupport::Concern

    included do

    before_validation :_before_validation
    after_save :_after_save

    def _before_validation

    end

    def _after_save

    end
    end

    module InstanceMethods

    end

    module ClassMethods

    end
    end

    Myengine::Mymodel.send(:include, MyengineExtensions)


    ```


    <a name="helpers"/>
    ## Use helpers defined in the Engine

  15. @maxivak maxivak revised this gist Nov 7, 2016. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,24 @@
    # Integrate Gem/Engine and main Rails app

    ## Overview
    - [Paths](#paths)
    - [Routes](#routes)
    - [Add functionality to controller](#controllers)
    - [Improving (Extending or overriding) Engine functionality](#extend-engine-class)
    - [Helpers](#helpers)


    ## Paths

    * access gem root path from the app

    ```
    spec = Gem::Specification.find_by_name("your_gem_name")
    gem_root = spec.gem_dir
    yaml_obj = YAML.load(gem_root + "/file_name.yaml")
    ```


    <a name="routes"/>
    ## Routes
    Routes inside an engine are isolated from the application by default.
  16. @maxivak maxivak revised this gist May 28, 2016. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -172,6 +172,13 @@ require 'active_support/concern'
    module UsefulController
    module Useful
    extend ActiveSupport::Concern
    included do
    # method to add to controller
    def newmethod1
    ...
    end
    end
    module ClassMethods
  17. @maxivak maxivak revised this gist May 28, 2016. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    ## Overview
    - [Routes](#routes)
    - [Add functionality to controller](#controllers)
    - [Improving (Extending or overriding) Engine functionality](#extend-engine-class)
    - [Helpers](#helpers)

    @@ -141,7 +142,55 @@ Now you can access routes from app and engine just using:
    Find more in [discussion on stackoverflow](http://stackoverflow.com/questions/7588870/engine-routes-in-application-controller#answer-7622755).


    <a name="controllers"/>
    ## Controller

    * We want to create a method in the Engine which will extend functionality of our app controller like this:

    ```
    # Main app controller
    class MyController < ApplicationController
    include UsefulController::Useful
    add_smth_useful :opt1, {opt1: v1, opt2: v2}
    end
    ```


    * Create method add_smth_useful in the Engine which will add action `myaction` to the main app controller


    ```
    # Engine: app/controllers/useful_controller.rb
    require 'active_support/concern'
    module UsefulController
    module Useful
    extend ActiveSupport::Concern
    module ClassMethods
    def add_smth_useful(arg1, options = {})
    # add method to the controller
    define_method("myaction") do
    # action code
    # render ...
    end
    end
    end
    end
    end
    ```


    <a name="extend-engine-class"/>
  18. @maxivak maxivak revised this gist May 28, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -290,7 +290,8 @@ end
    ```


    ## User helpers defined in the Engine
    <a name="helpers"/>
    ## Use helpers defined in the Engine

    * Isolated engine's helpers

  19. @maxivak maxivak revised this gist May 28, 2016. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    ## Overview
    - [Routes](#routes)
    - [Improving (Extending or overriding) Engine functionality](#extend-engine-class)
    - [Helpers](#helpers)

    <a name="routes"/>
    ## Routes
    @@ -287,3 +288,26 @@ module Myengine
    end

    ```


    ## User helpers defined in the Engine

    * Isolated engine's helpers

    Sometimes you may want to isolate engine, but use helpers that are defined for it. If you want to share just a few specific helpers you can add them to application's helpers in ApplicationController:

    ```
    class ApplicationController < ActionController::Base
    helper MyEngine::SharedEngineHelper
    end
    ```

    If you want to include all of the engine's helpers, you can use the helper method on an engine's instance:

    ```
    class ApplicationController < ActionController::Base
    helper MyEngine::Engine.helpers
    end
    ```

    It will include all of the helpers from engine's directory. Take into account that this does not include helpers defined in controllers with helper_method or other similar solutions, only helpers defined in the helpers directory will be included.
  20. @maxivak maxivak revised this gist Aug 3, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -182,7 +182,7 @@ end

    in the Engine:
    ```ruby
    # Blorgh/app/models/article.rb
    # Myengine/app/models/article.rb

    class Article < ActiveRecord::Base
    has_many :comments
  21. @maxivak maxivak revised this gist Aug 3, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ Routes inside an engine are isolated from the application by default.
    The application and its engines can have routes with the same names.


    Defined routes in engine:
    Routes defined in engine:
    ```ruby
    # /myengine/config/routes.rb

  22. @maxivak maxivak revised this gist Jul 22, 2015. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,10 @@
    <a name="routes"/>
    ## Routes
    Routes inside an engine are isolated from the application by default.
    the application and its engines can have routes with the same names.
    The application and its engines can have routes with the same names.


    Routes in Engine:
    Defined routes in engine:
    ```ruby
    # /myengine/config/routes.rb

    @@ -21,7 +21,7 @@ end

    ```

    Mount Engine to the main app:
    Mount engine to the main app:
    ```ruby
    # config/routes.rb

    @@ -46,11 +46,13 @@ http://mysite.com/myengine/articles
    ### Access Engine's routes from the main app


    Access engine route from the main app view:
    Access engine's route from the main app view:
    ```ruby

    # use the name specified in mount in 'as': mount Myengine::Engine => "/myenginepath", :as => "myengine"
    = link_to 'Articles from Engine', myengine.articles_path


    # this will try to find route defined in the main app, NOT from engine
    = link_to 'Articles', articles_path

  23. @maxivak maxivak revised this gist Jul 22, 2015. 1 changed file with 90 additions and 6 deletions.
    96 changes: 90 additions & 6 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@
    Routes inside an engine are isolated from the application by default.
    the application and its engines can have routes with the same names.

    ### Access Engine's routes from the main app

    Routes in Engine:
    ```ruby
    @@ -31,30 +30,115 @@ Rails.application.routes.draw do

    #
    mount Myengine::Engine => "/myengine", :as => "myengine"


    end

    ```

    Engine's pages will have URL prefix 'myengine/'. For example, URL for articles page from Engine:
    ```
    http://mysite.com/myengine/articles
    ```

    Access Engine rooute from the main app view:


    ### Access Engine's routes from the main app


    Access engine route from the main app view:
    ```ruby

    = link_to 'Articles', myengine.articles_path
    = link_to 'Articles from Engine', myengine.articles_path

    # this will try to find route defined in the main app, NOT from engine
    = link_to 'Articles', articles_path

    ```

    from any placei n app (in controller, or in a class in lib/ )
    from any place in app (in controller, or in a class in lib/ )
    ```ruby

    Myenginename::Engine.routes.url_helpers.pagename_path
    Myengine::Engine.routes.url_helpers.articles_path

    ```


    ### Access main app's routes in Engine

    routes defined in app:
    ```ruby
    # config/routes.rb

    Rails.application.routes.draw do
    resources :users

    # mount engine
    mount Myengine::Engine => "/", :as => "myengine"

    end

    ```


    Access app's routes from engine's view:
    ```ruby
    # myengine/app/views/somedir/someview.html.haml

    = link_to 'Users', main_app.users_path


    ```





    ### Merge engine and app routes

    If you want the two sets of routes to be merged, you can use a non-isolated engine.

    Removing the isolated_namespace in the engine definition:
    ```ruby
    # myengine/lib/myengine/engine.rb

    module Myengine
    class Engine < Rails::Engine
    #isolate_namespace Myengine # remove this line
    ...
    end
    end
    ```

    Define routes in Engine with Rails.application.routes.draw, NOT Myengine::Engine.routes.draw:
    ```ruby
    # myengine/config/routes.rb

    Rails.application.routes.draw do
    resources :articles

    end
    ```

    Remove `mount` in the main app:
    ```ruby
    App::Application.routes.draw do
    #mount Myengine::Engine => "/myengine" # remove this line
    end

    ```

    Now you can access routes from app and engine just using:
    ```
    = link_to 'Articles', articles_path
    ```



    Find more in [discussion on stackoverflow](http://stackoverflow.com/questions/7588870/engine-routes-in-application-controller#answer-7622755).





    <a name="extend-engine-class"/>
  24. @maxivak maxivak revised this gist Jul 22, 2015. 1 changed file with 54 additions and 0 deletions.
    54 changes: 54 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,62 @@
    # Integrate Gem/Engine and main Rails app

    ## Overview
    - [Routes](#routes)
    - [Improving (Extending or overriding) Engine functionality](#extend-engine-class)

    <a name="routes"/>
    ## Routes
    Routes inside an engine are isolated from the application by default.
    the application and its engines can have routes with the same names.

    ### Access Engine's routes from the main app

    Routes in Engine:
    ```ruby
    # /myengine/config/routes.rb

    Myengine::Engine.routes do
    resources :articles

    end

    ```

    Mount Engine to the main app:
    ```ruby
    # config/routes.rb

    Rails.application.routes.draw do
    # app routes

    #
    mount Myengine::Engine => "/myengine", :as => "myengine"


    end


    ```

    Access Engine rooute from the main app view:
    ```ruby

    = link_to 'Articles', myengine.articles_path

    ```

    from any placei n app (in controller, or in a class in lib/ )
    ```ruby

    Myenginename::Engine.routes.url_helpers.pagename_path

    ```


    ### Access main app's routes in Engine



    <a name="extend-engine-class"/>
    ## Improving (Extending or overriding) Engine functionality

  25. @maxivak maxivak revised this gist Jul 20, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -81,7 +81,7 @@ We have a class defined in Engine:
    module Myengine
    module Mymodule
    class Myclass
    include Optimacms::Concerns::AdminMenu::AdminMenu
    include Myengine::Concerns::Mymodule::Myclass


    end
    @@ -96,7 +96,7 @@ Add concern to Engine:
    ```ruby
    # myengine/lib/concerns/mymodule/myclass.rb

    module Optimacms::Concerns::AdminMenu::AdminMenu
    module Myengine::Concerns::Mymodule::Myclass
    extend ActiveSupport::Concern

    included do
  26. @maxivak maxivak revised this gist Jul 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -130,7 +130,7 @@ Use concern in the main app:
    module Myengine
    module Mymodule
    class Myclass
    include Optimacms::Concerns::AdminMenu::AdminMenu
    include Myengine::Concerns::Mymodule::Myclass

    # override class method
    def self.my_class_method
  27. @maxivak maxivak revised this gist Jul 20, 2015. 1 changed file with 74 additions and 0 deletions.
    74 changes: 74 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -73,3 +73,77 @@ end
    ```


    ### Extend Engine class using ActiveSupport::Concern

    We have a class defined in Engine:
    ```ruby
    # myengine/lib/myengine/mymodule/myclass.rb
    module Myengine
    module Mymodule
    class Myclass
    include Optimacms::Concerns::AdminMenu::AdminMenu


    end
    end
    end


    ```

    Add concern to Engine:

    ```ruby
    # myengine/lib/concerns/mymodule/myclass.rb

    module Optimacms::Concerns::AdminMenu::AdminMenu
    extend ActiveSupport::Concern

    included do

    end

    def my_object_method
    'it is engine'
    end

    module ClassMethods
    # will be overridden in the main app
    def my_class_method
    []
    end


    end
    end



    ```


    Use concern in the main app:

    ```ruby
    # myapp/lib/myengine/mymodule/myclass.rb

    module Myengine
    module Mymodule
    class Myclass
    include Optimacms::Concerns::AdminMenu::AdminMenu

    # override class method
    def self.my_class_method
    ['1', '2', '3']
    end

    # override object method
    def my_object_method
    'this is app'
    end

    end
    end
    end

    ```
  28. @maxivak maxivak revised this gist Jul 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    ## Overview
    - [Improving (Extending or overriding) Engine functionality](#extend-engine-class)

    (#extend-engine-class)
    <a name="extend-engine-class"/>
    ## Improving (Extending or overriding) Engine functionality

    A common task after including Engine in your Rails app is extending some classes (models, controllers, other classes) defined in the Engine.
  29. @maxivak maxivak revised this gist Jul 20, 2015. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    # Integrate Gem and main Rails app
    # Integrate Gem/Engine and main Rails app

    ## Improving (Extending or overriding) Gem functionality
    ## Overview
    - [Improving (Extending or overriding) Engine functionality](#extend-engine-class)

    (#extend-engine-class)
    ## Improving (Extending or overriding) Engine functionality

    A common task after including Engine in your Rails app is extending some classes (models, controllers, other classes) defined in the Engine.

  30. @maxivak maxivak revised this gist Jul 20, 2015. 1 changed file with 58 additions and 0 deletions.
    58 changes: 58 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,68 @@

    A common task after including Engine in your Rails app is extending some classes (models, controllers, other classes) defined in the Engine.

    It can be done using Decorator pattern.

    There are two options of extending a class defined in Engine:
    - use Class@class_eval
    - use ActiveSupport::Concern


    For simple class modifications, use Class#class_eval.

    For complex class modifications, consider using ActiveSupport::Concern.

    Read more in [Rails guides](http://edgeguides.rubyonrails.org/engines.html).


    ### use Class#class_eval to override class defined in Gem/Engine

    ```ruby
    # lib/myengine/engine.rb
    module Myengine
    class Engine < ::Rails::Engine
    isolate_namespace Myengine

    config.to_prepare do
    Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
    require_dependency(c)
    end
    end
    end
    end

    ```

    in the Engine:
    ```ruby
    # Blorgh/app/models/article.rb

    class Article < ActiveRecord::Base
    has_many :comments

    def summary
    "#{title}"
    end
    end

    ```

    in the main app:
    ```ruby
    # MyApp/app/decorators/models/blorgh/article_decorator.rb

    Myengine::Article.class_eval do
    # add new method
    def time_since_created
    Time.current - created_at
    end

    # override the method
    def summary
    "#{title} - #{truncate(text)}"
    end

    end
    ```