166 lines
3.8 KiB
HTML
166 lines
3.8 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8" />
|
||
<title></title>
|
||
<link href="style.css" rel="stylesheet" />
|
||
</head>
|
||
<body>
|
||
<h1>Rails-plugins</h1>
|
||
<h2>Generate a plugin</h2>
|
||
|
||
<p>Generate a Rails Engine plugin:</p>
|
||
|
||
<pre><code>rails plugin new myplugin --skip-bundle --full
|
||
</code></pre>
|
||
|
||
<h2>Initializers</h2>
|
||
|
||
<ul>
|
||
<li><a href="http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html">Rails::Railtie</a></li>
|
||
<li><a href="http://www.engineyard.com/blog/2010/extending-rails-3-with-railties/">EngineYard blog
|
||
post</a></li>
|
||
</ul>
|
||
|
||
<p>Subclass Railtie and provide an <code>initializer</code> method.</p>
|
||
|
||
<pre><code>module NewPlugin
|
||
class Railtie < Rails::Railtie
|
||
initializer "newplugin.initialize" do |app|
|
||
|
||
# subscribe to all rails notifications: controllers, AR, etc.
|
||
ActiveSupport::Notifications.subscribe do |*args|
|
||
event = ActiveSupport::Notifications::Event.new(*args)
|
||
puts "Got notification: #{event.inspect}"
|
||
end
|
||
|
||
end
|
||
end
|
||
end
|
||
</code></pre>
|
||
|
||
<h2>Custom routes</h2>
|
||
|
||
<ul>
|
||
<li><a href="http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper.html">ActionDispatch::Routing::Mapper</a></li>
|
||
</ul>
|
||
|
||
<p>To create custom <code>routes.rb</code> keywords:</p>
|
||
|
||
<pre><code># # routes.rb:
|
||
# myplugin_for x
|
||
#
|
||
class ActionDispatch::Routing
|
||
class Mapper
|
||
def myplugin_for(*x)
|
||
end
|
||
end
|
||
end
|
||
</code></pre>
|
||
|
||
<p>Example with a block:</p>
|
||
|
||
<pre><code># authenticated do
|
||
# resources :users
|
||
# end
|
||
#
|
||
def authenticated
|
||
constraint = lambda { |request| request... }
|
||
|
||
constraints(constraint) { yield }
|
||
end
|
||
</code></pre>
|
||
|
||
<h2>Custom generators</h2>
|
||
|
||
<ul>
|
||
<li><a href="http://guides.rubyonrails.org/generators.html">Guide: generators</a></li>
|
||
<li><a href="http://api.rubyonrails.org/classes/ActiveRecord/Generators/Base.html">ActiveRecord::Generators::Base</a></li>
|
||
</ul>
|
||
|
||
<h3>Basic</h3>
|
||
|
||
<pre><code># rails g initializer
|
||
# lib/generators/initializer_generator.rb
|
||
class InitializerGenerator < Rails::Generators::Base
|
||
def create_initializer_file
|
||
create_file "config/initializers/initializer.rb", "# Add initialization content here"
|
||
end
|
||
end
|
||
</code></pre>
|
||
|
||
<ul>
|
||
<li>Extend <code>Rails::Generators::Base</code>.</li>
|
||
<li>Each public method in the generator is executed when a generator is invoked. </li>
|
||
</ul>
|
||
|
||
<h3>Generating a generator</h3>
|
||
|
||
<pre><code>$ rails generate generator initializer
|
||
</code></pre>
|
||
|
||
<h3>NamedBase</h3>
|
||
|
||
<p>Use <code>NamedBase</code> instead if you want to take an argument. It will be available as
|
||
<code>file_name</code>.</p>
|
||
|
||
<pre><code>class InitializerGenerator < Rails::Generators::Base
|
||
def lol
|
||
puts file_name
|
||
end
|
||
end
|
||
</code></pre>
|
||
|
||
<h3>More</h3>
|
||
|
||
<pre><code>class InitializerGenerator < Rails::Generators::NamedBase
|
||
#
|
||
source_root File.expand_path("../templates", __FILE__)
|
||
desc "Description goes here."
|
||
end
|
||
</code></pre>
|
||
|
||
<h3>Generators lookup</h3>
|
||
|
||
<p>When invoking <code>rails g XXX</code>:</p>
|
||
|
||
<ul>
|
||
<li>[rails/]generators/XXX/XXX_generator.rb</li>
|
||
<li>[rails/]generators/XXX_generator.rb</li>
|
||
</ul>
|
||
|
||
<p>When invoking <code>rails g XXX:YYY</code>:</p>
|
||
|
||
<ul>
|
||
<li>[rails/]generators/XXX/YYY_generator.rb</li>
|
||
</ul>
|
||
|
||
<h2>ActiveModel 'acts as'</h2>
|
||
|
||
<pre><code># yaffle/lib/yaffle/acts_as_yaffle.rb
|
||
module Yaffle
|
||
module ActsAsYaffle
|
||
extend ActiveSupport::Concern
|
||
|
||
included do
|
||
end
|
||
|
||
module ClassMethods
|
||
def acts_as_yaffle(options = {})
|
||
# your code will go here
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
|
||
</code></pre>
|
||
|
||
|
||
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
|
||
<script src="http://cachedcommons.org/cache/prettify/1.0.0/javascripts/prettify-min.js"></script>
|
||
<script>$("pre").addClass("prettyprint");</script>
|
||
<script>prettyPrint();</script>
|
||
</body>
|
||
</html>
|