Skip to content

Latest commit

 

History

History
109 lines (75 loc) · 2.09 KB

README.md

File metadata and controls

109 lines (75 loc) · 2.09 KB

Rails Components

A guide to implementing component-based architecture in Rails implemented using:

  • Postgresql
  • ERB
  • Rspec
  • Rubocop

Table of Contents

Engines
Gems

Engines

Rails Engines are useful for components that use database models and views.

Generating a new engine
Add Rspec and related dependencies
Host application configuration

Generating a new engine

New engine: ui_core

$ rails plugin new components/ui_core --mountable -dummy-path=spec/dummy -T

Add Rspec and related dependencies

Add the following lines to components/ui_core/ui_core.gemspec

spec.add_dependency 'pg'

spec.add_development_dependency 'rspec-rails'

Component test database to use the main app database:

$ cd $RAILS_ROOT/components/ui_core/spec/dummy/config
$ rm database.yml
$ ln –s ../../../../../config/database.yml

Install Rspec:

$ cd $RAILS_ROOT/components/ui_core
$ rails generate rspec:install

Update line of generated rails helper:

From:

require File.expand_path('../../config/environment', __FILE__)

To:

require File.expand_path('../dummy/config/environment', __FILE__)

Host application configuration

Add these lines to the main app's Gemfile ($RAILS_ROOT/Gemfile):

path 'components/' do
  gem 'ui_core'
end

Mount the engine by adding these lines to the main app's routes ($RAILS_ROOT/config/routes.rb):

Rails.application.routes.draw do
  mount UiCore::Engine => /
end

Gems

Gems are best for extracting functionality that does not depend on reading/writing to the database and has no view layer

Generating a new gem
Plug into host application

Generating a new gem

New gem: parser

$ bundle gem parser

Plug into host application

Add these lines to the main app's Gemfile ($RAILS_ROOT/Gemfile):

path 'components/' do
  # ...
  gem 'parser'
end