OSDN Git Service

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