Skip to content
Max Ivak edited this page Mar 7, 2018 · 1 revision

Create modules for OptimaCMS

This guide shows how to create your own engine (or so-called module) to extend the functionality of OptimaCMS.

In this simple example, the engine will add news.

This module will add some pages to the CMS admin area:

  • /cmsadmin/news - index
  • /cmsadmin/news/:id/ - show news item
  • and so on

Generate engine

rails plugin new cms_module_news --dummy-path=spec/dummy --mountable

Routes

# /engine/config/routes.rb

CmsModuleNews::Engine.routes do
  scope '/cmsadmin' do
    scope module: 'admin', as: 'admin' do
      resources :news
    end
  end
end

The engine will have an isolated namespace, i.e.

# lib/cms_module_news/engine.rb

module CmsModuleNews
  class Engine < ::Rails::Engine
    isolate_namespace CmsModuleNews

    ...
  end
end

Engine functionality

Write the functionality of your engine as usual in models/controllers/views.

Controller:

# app/controllers/admin/news_controller.rb

class CmsModuleNews::Admin::NewsController < Optimacms::Admin::AdminBaseController
  def index
    # your code
  end

  # your code here
end

View:

# app/views/cms_module_news/admin/news/index.html.haml

%h1 News

list of news...

Migrations

Generator for copying migrations

lib/generators/optimacms_news/install_generator.rb

require 'rails/generators'
require 'rails/generators/migration'
require 'rails/generators/active_record'


module OptimacmsNews
  module Generators
    class InstallGenerator < ::Rails::Generators::Base
      include Rails::Generators::Migration

      desc "Install options"
      source_root File.expand_path("../templates", __FILE__)



      def self.next_migration_number(path)
        #Time.now.utc.strftime("%Y%m%d%H%M%S")
        ActiveRecord::Generators::Base.next_migration_number(path)
      end

      def create_migrations
        migration_template 'migrations/create_news.rb', 'db/migrate/create_news.rb'
      end

    end
  end
end

Templates for migrations:

lib/generators/optimacms_news/templates/migrations/create_news.rb

class CreateNews < ActiveRecord::Migration
  def change
    create_table :news do |t|
      t.string :title, :null => false

# other fields here
# ....

    end

  end
end

Build and push gem

Build and push your gem to be available for the world.

Run from the engine's directory:

gem build cms_module_news.gemspec

install locally:

gem install ./cms_module_news-0.0.1.gem

and push to rubygems.org:

gem push cms_module_news-0.0.1.gem

Integrate the new module to main app

Use the created module in the main app.

Gemfile:

gem 'optimacms'
gem 'cms_module_news'

# all other gems

migrations:

rails g optimacms_news:install

This will copy migration files to the main application.

run rake task to apply migrations:

rake db:migrate

routes.rb:

Rails.application.routes.draw do
  # cms_module_news devise
  devise_for :cms_admin_users, Optimacms::Devise.config

  # app's routes
  #get '/', to: 'home#index', as: :home

  #resources :services

  # CMS and modules - the LAST ROWS
  mount CmsModuleNews::Engine => "/", :as => "cms_news"
  mount Optimacms::Engine => "/", :as => "cms"

end

Add links to the admin menu:

# lib/optimacms/admin_menu/admin_menu.rb

module Optimacms
  module AdminMenu
    class AdminMenu
      include Optimacms::Concerns::AdminMenu::AdminMenu

      def self.get_menu_custom

        [
            {title: 'News', route: nil, submenu: [
                {title: 'News', url: CmsModuleNews::Engine.routes.url_helpers.admin_news_index_path}
            ]
            }

        ]
      end

    end
  end
end

Make admin_menu.rb file autoloaded:

# config/application.rb
...

module Dummy
  class Application < Rails::Application
    ...
    #
    config.autoload_paths += Dir["#{config.root}/lib/**/"]

  end
end


Clone this wiki locally