• Stars
    star
    181
  • Rank 212,110 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Rails Engine framework that helps safe and rapid feature prototyping

Motorhead

Motorhead is a prototyping framework for Rails. It's something akin to "feature toggle". It can be used for "A/B testing" as well. But essentially, the main purpose of this framework is to provide a way to rapidly and safely deliver new features to the production environment.

Installation

Bundle into your Rails app.

Features

Isolated Engine

Motorhead helps you mounting specially creafted Isolated Engines onto the main Rails app. An engine can contain whole MVC components, which means that you can encapsulate everything that are needed for your new feature under one single directory. This helps your team creating multiple new features simultaneously without causing code conflict.

Conditional Execution

Each Motorhead engine can be configured to be enabled/disabled. The condition is not just a flag but can be a Ruby Proc which will be dynamically evaluated on each request in the controller context.

Error-proof

If any RuntimeError happens inside an engine on the production environment, Motorhead absorbs the error and executes the appropriate fallback code so that the end users would never even notice the occurrence of the error.

Extending Action Methods in the Main App

Motorhead provides an interface to override the main app's controller actions.

Partially Extending Views in the Main App

Motorhead provides a hook to partially overwrite any part of your existing view.

Structure

main_app
├── Gemfile
├── Gemfile.lock
├── Rakefile
├── app
│   ├── assets
│   ├── controllers
│   ├── engines
│   │   ├── my_awesome_new_feature
│   │   │   ├── app
│   │   │   │   ├── assets
│   │   │   │   ├── controllers
│   │   │   │   │   └── my_awesome_new_feature
│   │   │   │   │       └── welcome_controller.rb
│   │   │   │   ├── helpers
│   │   │   │   │   └── my_awesome_new_feature
│   │   │   │   │       └── welcome_helper.rb
│   │   │   │   ├── mailers
│   │   │   │   ├── models
│   │   │   │   └── views
│   │   │   │       ├── layouts
│   │   │   │       │   └── my_awesome_new_feature
│   │   │   │       │       └── application.html.erb
│   │   │   │       └── my_awesome_new_feature
│   │   │   │           └── welcome
│   │   │   ├── config
│   │   │   │   └── routes.rb
│   │   │   ├── lib
│   │   │   │   ├── my_awesome_new_feature
│   │   │   │   │   └── engine.rb
│   │   │   │   ├── my_awesome_new_feature.rb
│   │   │   │   └── tasks
│   │   │   ├── my_awesome_new_feature.gemspec
│   │   │   └── test
│   │   └── yet_another_new_feature
│   │       ├── app
│   │       ...
│   ├── helpers
│   ├── mailers
│   ├── models
│   └── views
├── bin
├── config
├── db
...

Components

lib/ENGINE_NAME/engine.rb

Put active_if directive inside the Engine class. The whole engine will only be executed if the block is evaluated to be truthy on runtime.

Example:

# app/engines/my_awesome_new_feature/config/routes.rb
module MyAwesomeNewFeature
  class Engine < ::Rails::Engine
    include Motorhead::Engine

    # this whole engine will be executed only when logged in as admin users
    active_if { current_user.admin? }
  end
end

You can configure a path to mount the Engine via mount_at directive. This value is defaulted to "/", which means the Engine routes will be mixed into the main app's routes.

Example:

# app/engines/my_awesome_new_feature/config/routes.rb
module MyAwesomeNewFeature
  class Engine < ::Rails::Engine
    include Motorhead::Engine

    # all routes inside this Engine will be prefixed by "/harley"
    mount_at 'harley'
  end
end

routes.rb

All routes in engines' routes.rb will be automatically prepended to the main app's Routes.

Controllers

Controllers can be a normal Engine controller. Or, you can craft a controller class inheriting a controller that exists in the main app, and overriding some action methods. From inside the actions in engines, you can call the main app's action via super.

Example:

# app/engines/my_awesome_new_feature/app/controllers/my_awesome_new_feature/welcome_controller.rb
class MyAwesomeNewFeature::WelcomeController < ::WelcomeController
  include Motorhead::Controller

  def index
    # invoking the main app's action first
    super

    # adding some more business logic
    @notifications = Notification.for current_user
  end
end

Views

When an engine renders the views, it looks up app/views/ENGINE_NAME/ directory first, then the main app's view directory next. This way you can overwrite views per template/partial file. Also, Motorhead adds new :engine option to render method, which enables you to explicitly inject a piece of HTML from an engine into any place of the app. render :engine takes an `ENGINE_NAME/view_path' parameter.

Example:

# app/engines/my_awesome_new_feature/app/views/my_awesome_new_feature/welcome/_index.html.haml
.new_feature
  Some contents for new feature

# app/views/welcome/index.html.haml
= render engine: 'my_awesome_new_feature/welcome/index' do
  Some contents that will be shown by default

Generators

Motorhead provides some handy code generators.

Generating an engine

% rails g motorhead ENGINE_NAME

Example:

% rails g motorhead my_awesome_new_feature

This generates a motorhead Engine in ~/app/engines/my_awesome_new_feature/ directory.

Generating an engine + a controller extension that extends an existing controller

% rails g motorhead ENGINE_NAME CONTROLLER_NAME [action action] [options]

Example:

% rails g motorhead my_awesome_new_feature welcome index

This generates a motorhead Engine in ~/app/engines/my_awesome_new_feature/ directory. Plus, a controller that extends WelcomeController and implements index action inside the Engine.

Managing Engines

Motorhead includes a tiny admin console Engine call "RoadCrew" through which you can easily enable/disable each mounted Engine. To use this, require 'motorhead/road_crew' when bundling motorhead gem in your Gemfile:

# Gemfile
gem 'motorhead', path: '~/src/motorhead', require: ['motorhead', 'motorhead/road_crew']

then render the road_crew/button Engine partial to put a button inside somewhere in your view (usually in the header or footer?):

# app/views/layouts/application.html.erb
<%= render engine: 'road_crew/button' %>

You might also want to configure the RoadCrew Engine's active_if directive for security.

# config/initializers/mh_road_crew.rb
RoadCrew::Engine.active_if { current_user.admin? }

Contributing

Pull requests are welcome on GitHub at https://github.com/amatsuda/motorhead.

Todo

  • Better generator

  • Model extension

  • Documentation

Special Thanks

This product is deeply inspired by Chanko.

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

jb

A simple and fast JSON API template engine for Ruby on Rails
Ruby
1,235
star
2

active_decorator

ORM agnostic truly Object-Oriented view helper for Rails 4, 5, 6, and 7
Ruby
1,051
star
3

traceroute

A Rake task gem that helps you find the unused routes and controller actions for your Rails 3+ app
Ruby
877
star
4

heavens_door

Capybara test scenario recorder for Rails
JavaScript
863
star
5

database_rewinder

minimalist's tiny and ultra-fast database cleaner
Ruby
807
star
6

stateful_enum

A very simple state machine plugin built on top of ActiveRecord::Enum
Ruby
606
star
7

kaminari_themes

HTML
354
star
8

erd

A Rails engine for drawing your app's ER diagram
Ruby
332
star
9

html5_validators

A gem/plugin for Rails 3, Rails 4, Rails 5, and Rails 6 that enables client-side validation using ActiveModel + HTML5 Form Validation
Ruby
304
star
10

i18n_generators

A pack of Rails generators gem plugin that generates Rails 3 and Rails 2 I18n locale files for almost every known locale.
Ruby
285
star
11

himl

HTML-based Indented Markup Language for Ruby
Ruby
235
star
12

still_life

Rails upgrade's best friend
Ruby
216
star
13

gem-src

Gem.post_install { `git clone gem_source src` }
Ruby
208
star
14

nested_scaffold

Nested scaffold generator for Rails 4.2 and 5
Ruby
176
star
15

roundabout

A Rails Engine that generates a page transition diagram for your Rails app from request specs
Ruby
153
star
16

rfd

Ruby on Files & Directories
Ruby
152
star
17

routes_lazy_routes

A boot time booster for Ruby on Rails that defers loading the whole bloody routes so the app can spin up quickly 🤘
Ruby
141
star
18

string_template

A template engine for Rails, focusing on speed, using Ruby's String interpolation syntax
Ruby
125
star
19

kawaii_validation

An ActiveRecord extension that adds more kawaii validation syntax
Ruby
117
star
20

interactive_rspec

RSpec on IRB
Ruby
86
star
21

hocus_pocus

A magical isolated engine gem for Rails 3.1+
Ruby
80
star
22

ljax_rails

render :partial lazy-loader for Rails
Ruby
66
star
23

everywhere

Hash condition syntax for AR query everywhere!
Ruby
58
star
24

kaminari_example

A tutorial project for the basic and advanced usage of Kaminari paginator
Ruby
45
star
25

async_partial

Ruby
33
star
26

turbo_partial

Ruby
27
star
27

future_records

Ruby
25
star
28

lightweight_attributes

Ruby
24
star
29

more_optimized_resolver

Ruby
23
star
30

turbo_urls

Ruby
22
star
31

teriyaki

Automatically imports *_path definitions from config/routes.rb for acceptance testing
Ruby
22
star
32

kawaii_association

An ActiveRecord DSL extension that provides kawaii association syntax
Ruby
21
star
33

arel_ruby

ARel Ruby visitor
Ruby
20
star
34

activerecord-refinements

ActiveRecord + Ruby 2.0 refinements
Ruby
20
star
35

polymorphic_url_cache

Ruby
17
star
36

gem_i

A RubyGems plugin that explicitly aliases `gem i` to `gem install` to avoid ambiguity
Ruby
17
star
37

speed_king

Ruby
14
star
38

nested_layouts

The only fork of "nested_layouts" Rails plugin in Github that correctly bug fixed for Rails 2.3
Ruby
13
star
39

rspec-refinements

RSpec + Ruby 2.0 refinements
Ruby
10
star
40

arenai

Ruby
8
star
41

activecalendar

Rails 2.2.2 ready javascript calendar date renderer
JavaScript
8
star
42

bundler-squash

Ruby
8
star
43

rbenv-gem-shared

Ruby
8
star
44

tatsuzine

Live coded app at Rails勉強会@東京#59
Ruby
8
star
45

bot_for_ruby-lang

Ruby
7
star
46

factory_factory

a script that transfers existing AR models into factories for factory_girl
Shell
6
star
47

automagic

Ruby
6
star
48

snowman_meltdown

A simple middleware for Rails 3 to vanish _snowman parameter☃☃☃
Ruby
5
star
49

activesupport-refinements

Ruby
4
star
50

webdb073_tutorial

WEB+DB Press Vol. 73 特集2「詳解Rails 4」のチュートリアルのサンプルコード
Ruby
4
star
51

gitrockets

Ruby
3
star
52

rails3_hands_on

東京Ruby会議03のワークショップ「Rails 3ハンズオン」のサンプルアプリケーション
Ruby
3
star
53

prsnt

prsnt prttyp
3
star
54

git_commands

3
star
55

internationalization

Ruby
3
star
56

qwik

qwik
2
star
57

hfrails

hfrails
2
star
58

atode_yomu

A gem plugin that cleverly installs rdoc and ri for the latest versions of already installed gems
Ruby
2
star
59

gem-diet

Ruby
2
star
60

action_args_with_rbs

Ruby
1
star