Skip to content

stevenspiel/Rails-Components

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Guide to component-based architecture in Rails

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published