plugin 構築 ; Rails 4
暫定版
Rails 4 でプラグイン、ってのはどう構築するのか知る必要があった
gem と切り分けができてない
ゴール
Rails 4 環境に plugin を導入する
プラグイン開発はローカルのみにし、gem にしたり云々はしない
ハッシュが method_missing したときに 'f' * 500 する plugin を作る
[ 準備 ]
plugin の生成
rails plugin new sample_plugin
gemspec の修正
(これをしないと プラグイン内で bundle install したときに一々怒られる)
sample_plugin/sample_plugin.gemspec
10 s.authors = ["mat5ukawa"] 11 s.email = ["mat5ukawa@mat.jp"] 12 s.homepage = "http://localhost" 13 s.summary = "Explanation ; plugin" 14 s.description = "Building plugin on Rails"
APPROUTE/lib へ mv ※1
mv sample_plugin lib/
Gemfile へ パス指定
gem 'sample_plugin', path: 'lib/sample_plugin'
[ 構築 ]
プラグイン initilizer の意味を持つ「プラグイン名.rb」に、
プラグインファイルを require する
lib/sample_plugin/lib/sample_plugin.rb
1 require 'sample_plugin/hash' 2 module SamplePlugin 3 end
hash が method_missing したときのアクションを起こす構築をする
lib/sample_plugin/lib/sample_plugin/hash.rb
1 class Hash 2 def method_missing(method, *arguments) 3 puts "f" * 500 4 end 5 end
app 側で確認する
rails g controller top index
app/controllers/top_controller.rb
def index h = {} h.mat5ukawa end
サーバーを立ち上げて確認
rails s http://localhost:3000/top/index
ログを見る
Started GET "/top/index" for 127.0.0.1 at 2014-01-16 00:02:59 +0900 Processing by TopController#index as HTML fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff (500回出てました) Rendered top/index.html.erb within layouts/application (1.3ms) Completed 200 OK in 98ms (Views: 71.0ms | ActiveRecord: 0.0ms)
参考元(お世話になります)
Ruby on Rails Guides # 3.3 vendor/plugins ※1