LoginSignup
6
5

More than 5 years have passed since last update.

motorhead というA/Bテストや機能の限定公開のフレームワークを使ってみる

Posted at

motorhead(0.38)は、Ruby on Rails で、A/Bテストや機能の限定公開をするために作られたフレームワークです。

インストールと初期設定

初期設定

今回は、scaffoldで Book を作ります。

$ rails new motorhead
$ bundle install
$ rails generate scaffold Book name:string description:text price:integer
$ rake db:migrate

インストール

bundle install で motorhead のインストールします。
Gemfile の設定は下記の通りにしてみました。

Gemfile
source 'https://rubygems.org'

gem 'rails', '4.2.5'
gem 'sqlite3'
gem 'mysql2'
gem 'sass-rails'
gem 'uglifier'
gem 'coffee-rails'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder'
gem 'sdoc', '~> 0.4.0', group: :doc

gem 'motorhead', require: ['motorhead', 'motorhead/road_crew']

group :development, :test do
  gem 'byebug'
  gem 'web-console'
  gem 'spring'
end

※ MySQL の利用を設定していますが、motorheadとは関係ないです

motorhead で、Rails::Engine を作る

motorheadは、Rails::Engineを利用した機能提供を行います。
motorhead を利用して、開発したい機能を作る土台を用意します。

ジェネレーターを利用

motorhead には、ジェネレーターがあり、それを利用すると簡単に作れます。

$ rails g motorhead publisher
Running via Spring preloader in process 78099
      create
      create  publisher.gemspec
      create  MIT-LICENSE
      create  app
      create  app/controllers/publisher/application_controller.rb
      create  app/helpers/publisher/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/publisher/application.html.erb
      create  app/assets/images/publisher
      create  app/assets/images/publisher/.keep
      create  config/routes.rb
      create  lib/publisher.rb
      create  lib/tasks/publisher_tasks.rake
      create  lib/publisher/version.rb
      create  lib/publisher/engine.rb
      create  app/assets/stylesheets/publisher/application.css
      create  app/assets/javascripts/publisher/application.js
      remove  MIT-LICENSE
      remove  app/controllers/publisher/application_controller.rb
      remove  lib/tasks
        gsub  publisher.gemspec
        gsub  publisher.gemspec
      append

テンプレートだけを限定公開する

ある条件に合わせて、作った機能を公開しようと思います。
本(Book)の価格(price)が1000よりも大きければ、表示するようにします。例えばですが、Bookの発売時期や、社内ユーザのみに公開するリンクなどがありそうです。

条件の設定をする

app/engines/publisher/lib/publisher/engine.rb
require 'motorhead/engine'

module Publisher
  class Engine < ::Rails::Engine
    include Motorhead::Engine

    active_if { @book.price > 1000 }
  end
end

限定公開するテンプレートを用意する

app/engines/エンジン名/app/views/エンジン名/テンプレートファイル にファイルを置きます。今回は、試してみるために幾つか置いてみました。

1種類目

app/engines/publisher/app/views/publisher/_show.html.erb
<p style="border:1px solid red;padding: 1em;">
<%= @book.name %>は、1,000 以上です
</p>

2種類目

app/engines/publisher/app/views/publisher/footer/_show.html.erb
<p style="border:1px solid red;padding: 1em;">
<%= @book.name %>の出版社情報です
</p>

読み込んでいるmotorheadの一覧の確認

motorhead の機能として下記のような記述をすると、motorheadで読み込んでいるエンジンの一覧を表示することができます(それ以上のことはよくわかっていないです…)

app/views/books/show.html.erb
<%= render engine: 'road_crew/button', locals: {label: '限定機能'} %>

公開しているテンプレートに設定する

app/views/books/show.html.erb
<%= render engine: 'publisher/show' %>
<%= render engine: 'publisher/footer/show' %>

結果

Egines List に出ているNewPublisherNewFetureBookは、今回のとは別に用意していたEngineです

スクリーンショット 2016-01-04 0.05.30.png

まとめ :metal:

簡単に限定機能を用意することができました。
もちろん、Google Analytics などで集計したいとか、CSSはどうするの?とか考えることはまだまだありそうです。実際に試してみたら、続きを書いてみようと思います。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5