cheatsheets/rails-plugins.html

356 lines
9.6 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang='en' class='no-js '>
<head>
<meta charset='utf-8'>
<meta content='width=device-width, initial-scale=1.0' name='viewport'>
<link href='./assets/favicon.png' rel='shortcut icon'>
<meta content='/rails-plugins.html' name='app:pageurl'>
<title>Rails plugins cheatsheet</title>
<meta content='Rails plugins cheatsheet' property='og:title'>
<meta content='Rails plugins cheatsheet' property='twitter:title'>
<meta content='article' property='og:type'>
<meta content='https://assets.devhints.io/previews/rails-plugins.jpg?t=20220707130803' property='og:image'>
<meta content='https://assets.devhints.io/previews/rails-plugins.jpg?t=20220707130803' property='twitter:image'>
<meta content='900' property='og:image:width'>
<meta content='471' property='og:image:height'>
<meta content="The one-page guide to Rails plugins: usage, examples, links, snippets, and more." name="description">
<meta content="The one-page guide to Rails plugins: usage, examples, links, snippets, and more." property="og:description">
<meta content="The one-page guide to Rails plugins: usage, examples, links, snippets, and more." property="twitter:description">
<link rel="canonical" href="https://devhints.io/rails-plugins">
<meta name="og:url" content="https://devhints.io/rails-plugins">
<meta content='Devhints.io cheatsheets' property='og:site_name'>
<meta content='Rails' property='article:section'>
<script async src='https://www.googletagmanager.com/gtag/js?id=UA-106902774-1'></script>
<script>
window.dataLayer=window.dataLayer||[];
function gtag(){dataLayer.push(arguments)};
gtag('js',new Date());
gtag('config','UA-106902774-1');
</script>
<meta property='page:depth' content='1'>
<script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js')})(document.documentElement)</script>
<script>(function(H){H.className=H.className.replace(/\bNoJs\b/,'WithJs')})(document.documentElement)</script>
<script>(function(d,s){if(window.Promise&&[].includes&&Object.assign&&window.Map)return;var js,sc=d.getElementsByTagName(s)[0];js=d.createElement(s);js.src='https://cdn.polyfill.io/v2/polyfill.min.js';sc.parentNode.insertBefore(js, sc);}(document,'script'))</script>
<!--[if lt IE 9]><script src='https://cdnjs.cloudflare.com/ajax/libs/nwmatcher/1.2.5/nwmatcher.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/json2/20140204/json2.js'></script><script src='https://cdn.rawgit.com/gisu/selectivizr/1.0.3/selectivizr.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script><![endif]-->
<style>html{opacity:0}</style>
<link rel="stylesheet" href="./assets/2015/style.css?t=20220707130803">
<link href="./assets/style.css?t=20220707130803" rel="stylesheet" />
<link href="./assets/print.css?t=20220707130803" rel="stylesheet" media="print" />
</head>
<body>
<div class='all'>
<div class='site-header'>
<div class='container'>
This is <a href="."><em>Devhints.io cheatsheets</em></a> &mdash; a collection of cheatsheets I've written.
</div>
</div>
<script type='application/ld+json'>
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://google.com/article"
},
"headline": "Rails plugins cheatsheet",
"image": [ "https://assets.devhints.io/previews/rails-plugins.jpg?t=20220707130803" ],
"description": "The one-page guide to Rails plugins: usage, examples, links, snippets, and more."
}
</script>
<script type='application/ld+json'>
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [{
"@type": "ListItem",
"position": 1,
"item": {
"@id": "https://devhints.io/#rails",
"name": "Rails"
}
},{
"@type": "ListItem",
"position": 2,
"item": {
"@id": "https://devhints.io/rails-plugins",
"name": "Rails plugins"
}
}]
}
</script>
<div class='post-list -single -cheatsheet'>
<div class='post-item'>
<div class='post-headline -cheatsheet'>
<p class='prelude'><span></span></p>
<h1><span>Rails plugins</span></h1>
<div class='pubbox'>
<div class='HeadlinePub' role='complementary'>
<script async src='https://pubsrv.devhints.io/carbon.js?serve=CE7IK5QM&placement=devhintsio&cd=pubsrv.devhints.io/s' id="_carbonads_js"></script>
<span class='placeholder -one'></span>
<span class='placeholder -two'></span>
<span class='placeholder -three'></span>
<span class='placeholder -four'></span>
</div>
</div>
</div>
<div class='post-content -cheatsheet'>
<h2 id="generate-a-plugin">Generate a plugin</h2>
<p>Generate a Rails Engine plugin:</p>
<pre><code>rails plugin new myplugin --skip-bundle --full
</code></pre>
<h2 id="initializers">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 &lt; 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 id="custom-routes">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 id="custom-generators">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 id="basic">Basic</h3>
<pre><code># rails g initializer
# lib/generators/initializer_generator.rb
class InitializerGenerator &lt; 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 id="generating-a-generator">Generating a generator</h3>
<pre><code>$ rails generate generator initializer
</code></pre>
<h3 id="namedbase">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 &lt; Rails::Generators::Base
def lol
puts file_name
end
end
</code></pre>
<h3 id="more">More</h3>
<pre><code>class InitializerGenerator &lt; Rails::Generators::NamedBase
#
  source_root File.expand_path("../templates", __FILE__)
desc "Description goes here."
end
</code></pre>
<h3 id="generators-lookup">Generators lookup</h3>
<p>When invoking <code>rails g XXX</code>:</p>
<pre><code>[rails/]generators/XXX/XXX_generator.rb
[rails/]generators/XXX_generator.rb
</code></pre>
<p>When invoking <code>rails g XXX:YYY</code>:</p>
<pre><code>[rails/]generators/XXX/YYY_generator.rb
</code></pre>
<h2 id="activemodel-acts-as">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>
</div>
<ul class="social-list ">
<li class="facebook link hint--bottom" data-hint="Share on Facebook"><a href="https://www.facebook.com/sharer/sharer.php?u=https://devhints.io/rails-plugins.html" target="share"><span class="text"></span></a></li>
<li class="twitter link hint--bottom" data-hint="Share on Twitter"><a href="https://twitter.com/intent/tweet?text=The%20ultimate%20cheatsheet%20for%20Rails%20plugins.%20https://devhints.io/rails-plugins.html" target="share"><span class="text"></span></a></li>
</ul>
</div>
</div>
<div class="about-the-site">
<div class="container">
<p class='blurb'>
<strong><a href=".">Devhints.io cheatsheets</a></strong> is a collection of cheatsheets I've written over the years.
Suggestions and corrections? <a href='https://github.com/rstacruz/cheatsheets/issues/907'>Send them in</a>.
<i class='fleuron'></i>
I'm <a href="http://ricostacruz.com">Rico Sta. Cruz</a>.
Check out my <a href="http://ricostacruz.com/til">Today I learned blog</a> for more.
</p>
<p class='back'>
<a class='big-button -back -slim' href='.#toc'></a>
</p>
<p>
</p>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/highlight.min.js"></script>
<script src="https://cdn.rawgit.com/rstacruz/unorphan/v1.0.1/index.js"></script>
<script>hljs.initHighlightingOnLoad()</script>
<script>unorphan('h1, h2, h3, p, li, .unorphan')</script>
</body>
</html>