OSDN Git Service

Regular updates
[twpd/master.git] / rails-plugins.md
1 ---
2 title: Rails plugins
3 category: Rails
4 layout: 2017/sheet
5 ---
6
7 ### Generate a plugin
8
9 Generate a Rails Engine plugin:
10
11     rails plugin new myplugin --skip-bundle --full
12
13 Initializers
14 ------------
15
16 ### Initializers
17
18 * [Rails::Railtie](http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html)
19 * [EngineYard blog 
20   post](http://www.engineyard.com/blog/2010/extending-rails-3-with-railties/)
21
22 ### Initializer method
23
24 Subclass Railtie and provide an `initializer` method.
25
26     module NewPlugin
27       class Railtie < Rails::Railtie
28         initializer "newplugin.initialize" do |app|
29      
30           # subscribe to all rails notifications: controllers, AR, etc.
31           ActiveSupport::Notifications.subscribe do |*args|
32             event = ActiveSupport::Notifications::Event.new(*args)
33             puts "Got notification: #{event.inspect}"
34           end
35      
36         end
37       end
38     end
39      
40 Custom routes
41 -------------
42
43 ### Custom routes
44
45 * [ActionDispatch::Routing::Mapper](http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper.html)
46
47 ### Creating routes
48
49 To create custom `routes.rb` keywords:
50
51     # # routes.rb:
52     # myplugin_for x
53     #
54     class ActionDispatch::Routing
55       class Mapper
56         def myplugin_for(*x)
57         end
58       end
59     end
60
61 Example with a block:
62
63     # authenticated do
64     #   resources :users
65     # end
66     #
67     def authenticated
68       constraint = lambda { |request| request... }
69
70       constraints(constraint) { yield }
71     end
72
73 Custom generators
74 -----------------
75
76 ### Custom generators
77
78 * [Guide: generators](http://guides.rubyonrails.org/generators.html)
79 * [ActiveRecord::Generators::Base](http://api.rubyonrails.org/classes/ActiveRecord/Generators/Base.html)
80
81 ### Basic
82
83     # rails g initializer
84     # lib/generators/initializer_generator.rb
85     class InitializerGenerator < Rails::Generators::Base
86       def create_initializer_file
87         create_file "config/initializers/initializer.rb", "# Add initialization content here"
88       end
89     end
90
91  * Extend `Rails::Generators::Base`.
92  * Each public method in the generator is executed when a generator is invoked. 
93
94 ### Generating a generator
95  
96     $ rails generate generator initializer
97
98 ### NamedBase
99
100 Use `NamedBase` instead if you want to take an argument. It will be available as 
101 `file_name`.
102
103     class InitializerGenerator < Rails::Generators::Base
104       def lol
105         puts file_name
106       end
107     end
108
109 ### More
110
111     class InitializerGenerator < Rails::Generators::NamedBase
112       # 
113       source_root File.expand_path("../templates", __FILE__)
114       desc "Description goes here."
115     end
116
117 ### Generators lookup
118   
119 When invoking `rails g XXX`:
120
121     [rails/]generators/XXX/XXX_generator.rb
122     [rails/]generators/XXX_generator.rb
123
124 When invoking `rails g XXX:YYY`:
125
126     [rails/]generators/XXX/YYY_generator.rb
127
128 ActiveModel 'acts as'
129 ---------------------
130
131 ### acts as
132
133     # yaffle/lib/yaffle/acts_as_yaffle.rb
134     module Yaffle
135       module ActsAsYaffle
136         extend ActiveSupport::Concern
137      
138         included do
139         end
140      
141         module ClassMethods
142           def acts_as_yaffle(options = {})
143             # your code will go here
144           end
145         end
146       end
147     end
148      
149     ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
150