{"id":4873,"date":"2010-10-21T07:04:34","date_gmt":"2010-10-20T23:04:34","guid":{"rendered":"http:\/\/ihower.tw\/blog\/?p=4873"},"modified":"2011-04-09T01:48:23","modified_gmt":"2011-04-08T17:48:23","slug":"rails3-railtie-and-plugins","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/4873-rails3-railtie-and-plugins","title":{"rendered":"Rails3: Railtie \u548c Plugins \u7cfb\u7d71"},"content":{"rendered":"<p>Rails3 \u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b0\u7684\u65b9\u6cd5\u4f86\u64f4\u5145 Rails\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u7b97\u662f\u4e86\u89e3 <a href=\"http:\/\/api.rubyonrails.org\/classes\/Rails\/Railtie.html\">Railtie<\/a> \u5427\u3002(\u6211\u500b\u4eba\u5c0d <a href=\"http:\/\/guides.rubyonrails.org\/generators.html\">Generator<\/a> \u6bd4\u8f03\u6c92\u8208\u8da3)<\/p>\n<p>Railtie \u662f Rails \u7684\u6838\u5fc3\u7a0b\u5f0f\uff0c\u63d0\u4f9b\u4e86 hooks \u4f86\u4fee\u6539\u555f\u52d5\u6642\u7684\u8f09\u5165\u6d41\u7a0b\u3002Rails \u7684\u4e3b\u8981\u5143\u4ef6 (Action Mailer, Action Controller, Action View, Active Record, Active Resource) \u90fd\u6709 Railtie \u4f86\u5404\u81ea\u8ca0\u8cac\u81ea\u5df1\u7684\u8f09\u5165\u6d41\u7a0b\u3002\u9019\u6a23\u8a2d\u8a08\u7684\u597d\u8655\u6709 1. \u4e3b\u8981\u5143\u4ef6\u4e5f\u662f\u53ef\u4ee5\u62bd\u63db\u7684(\u4f8b\u5982\u63db ORM) 2. \u8981\u5728 Rails \u88e1 hook \u8b8a\u5f97\u5341\u5206\u4e7e\u6de8\uff0c\u53ea\u8981\u64b0\u5beb railtie \u5373\u53ef\uff0c\u4e0d\u50cf\u4e4b\u524d\u53ea\u80fd\u7528 alias_method_chain\u3002<\/p>\n<p>\u4e0d\u904e\uff0c\u64f4\u5145 Rails \u4e0d\u4e00\u5b9a\u6703\u7528\u5230 Railtie\u3002\u6703\u9700\u8981\u5be6\u4f5c\u7684\u60c5\u5883\u662f\u4f60\u8981\u5728 Rails \u6846\u67b6\u555f\u52d5\u6d41\u7a0b\u4e2d\u505a\u4e9b\u4e92\u52d5\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>1. \u5efa\u7acb initializers<br \/>\n2. \u65b0\u589e generator<br \/>\n3. \u4fee\u6539 Rails config.*<br \/>\n4. \u8a02\u95b1 Rails +ActiveSupport::Notifications+<br \/>\n5. \u65b0\u589e rake tasks<\/p>\n<p>\u56e0\u6b64\uff0c\u6211\u6b78\u985e\u51fa\u5beb Rails Plugin \u7684\u8003\u91cf\u6709 1. \u8981\u4e0d\u8981\u5beb\u6210 Gem 2. \u9700\u4e0d\u9700\u8981\u7528\u5230 Railstie\uff1a<\/p>\n<h3>\u50b3\u7d71 vender\/plugin<\/h3>\n<p>\u50b3\u7d71\u7684 plugin \u5beb\u6cd5\u4f9d\u7136\u9069\u7528\uff0cRails \u9084\u662f\u6703\u53bb\u8f09\u5165 plugin \u76ee\u9304\u4e0b\u7684 init.rb\u3002\u6709\u8da3\u7684\u662f\uff0c\u5728 Rails \u5167\u90e8\u662f\u628a plugin \u81ea\u52d5\u5305\u88dd\u6210\u4e00\u500b Rails::Plugin \u985e\u5225\uff0c\u800c\u9019\u500b Plugin class \u4e5f\u662f\u7e7c\u627f\u81ea Rails::Railtie \u7684\u3002<\/p>\n<h3>\u5305\u6210 Rubygem<\/h3>\n<p>\u81ea\u5f9e\u6709\u4e86 <a href=\"http:\/\/ihower.tw\/blog\/archives\/4464\">Bundler<\/a> \u4e4b\u5f8c\uff0c\u5305\u6210 Gem \u53ef\u4ee5\u8aaa\u662f\u6700\u4f73\u5be6\u52d9\u4e86\u3002\u900f\u904e\u6a19\u6e96\u7684 Gem \u683c\u5f0f\uff0c\u53ef\u4ee5 1.\u8a2d\u5b9a dependcncies 2. \u6709\u7248\u865f version 3. \u65b9\u4fbf\u5206\u4eab\u53ca\u5b89\u88dd<\/p>\n<p>\u5982\u4f55\u5305\u6210 Gem \u8acb\u53c3\u8003 <a href=\"http:\/\/ihower.tw\/blog\/archives\/4496\">Rubygems \u5957\u4ef6\u7ba1\u7406\u5de5\u5177<\/a> \u9019\u7bc7\uff0c\u5f88\u7c21\u55ae\u7684\u3002<\/p>\n<h3>\u6c92\u7528\u5230 railtie<\/h3>\n<p>Rails3 \u67b6\u69cb\u5e2b wycats \u958b\u793a\uff1a&#8221;\u5982\u679c\u4f60\u6c92\u6709\u8981 hook \u5728 Rails lifecycle \u4e4b\u4e2d\uff0c\u4e0d\u8981\u7528 Railtie\u3002\u5c31\u5982\u540c\u4e00\u822c\u7684 Ruby library\uff0c\u4f60\u5148 require \u4f60\u9700\u8981\u7684 Rails \u5143\u4ef6\uff0c\u7136\u5f8c\u518d\u4fee\u6539\u6216\u64f4\u5145\u5373\u53ef&#8221; (\u4f8b\u5982 override \u6216 include something)<\/p>\n<pre>\r\n<code>\r\n# in your_lib.rb\r\nrequire \"active_record\" \r\nrequire \"your_lib\/extensions\" \r\nclass ActiveRecord::Base\r\n  include YourLib::Extensions\r\nend\r\n<\/code>\r\n<\/pre>\n<h3>\u7528\u5230 railtie<\/h3>\n<p>\u5982\u679c\u4f60\u9700\u8981 hook \u6216\u8f09\u5165 rake, generator \u7b49\uff0c\u90a3\u5c31\u9700\u8981\u7528\u5230 railtie \u4e86\uff0c\u8a72\u600e\u9ebc\u5beb\u5462?<\/p>\n<p>\u57fa\u672c\u7684\u5beb\u6cd5\u5982\u4e0b:<\/p>\n<pre>\r\n<code>\r\n# my_new_gem\/my_new_gem.rb\r\nrequire 'rails'\r\nclass MyCoolRailtie < Rails::Railtie\r\n\r\n  # console \u6642\u8f09\u5165\r\n  console do \r\n    Foo.console_mode!\r\n  end\r\n\r\n  # \u8f09\u5165 generator\r\n  generators do\r\n    require 'path\/to\/generator'\r\n  end\r\n\r\n  # \u8f09\u5165 rake tasks\r\n  rake_tasks do\r\n    require 'path\/to\/railtie.tasks'\r\n  end\r\n\r\n  # \u5efa\u7acb config\/initializers\r\n  initializer \"my_cool_railtie.boot_foo\" do\r\n    Foo.boot(Bar)\r\n  end\r\n\r\nend\r\n<\/code>\r\n<\/pre>\n<p>\u9664\u4e86\u4e0a\u8ff0\u9019\u4e9b\uff0c\u9084\u6709\u66f4\u591a\u7d30\u5fae\u7684 hook \u5982 config.after _initialize, <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActionDispatch\/MiddlewareStack.html\">config.middlewares<\/a>, before_configuration, before_eager_load, before_initialize, to_prepare \u7b49\u7b49\u65cf\u7e41\u4e0d\u53ca\u80cc\u8f09\uff0c\u5982\u679c\u6c92\u4ed4\u7d30\u7814\u7a76 Rails \u7684\u555f\u52d5\u6d41\u7a0b\u6050\u6015\u4e5f\u641e\u4e0d\u6e05\u695a\u6240\u6709\u7684\u5dee\u7570\u3002<\/p>\n<p>\u5982\u679c\u4f60\u7684 gem \u4e0d\u53ea\u7d66 rails \u7528\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb:<\/p>\n<pre>\r\n<code>\r\n# lib\/my_new_gem\/my_cool_railtie.rb\r\nmodule MyNewGem\r\n  class MyCoolRailtie &lt; ::Rails::Railtie\r\n    # Railtie code here\r\n  end\r\nend\r\n\r\n# lib\/my_new_gem.rb\r\nrequire 'my_new_gem\/my_cool_railtie.rb' if defined?(Rails)\r\n<\/code>\r\n<\/pre>\n<p>\u8acb\u671f\u5f85\u4e0b\u96c6 \"Rails3: Engine \u548c Plugins \u7cfb\u7d71\"\uff0c<a href=\"http:\/\/api.rubyonrails.org\/classes\/Rails\/Engine.html\">Rails::Engine<\/a> \u53ef\u4ee5\u8b93\u4f60\u5305\u88dd\u51fa\u7368\u7acb\u7684 App \u5143\u4ef6\u3002<\/p>\n<h3>\u53c3\u8003\u8cc7\u6599<\/h3>\n<p><a href=\"http:\/\/weblog.rubyonrails.org\/2010\/2\/9\/plugin-authors-toward-a-better-future\">Plugin Authors: Toward a Better Future<\/a><br \/>\n<a href=\"http:\/\/www.themodestrubyist.com\/2010\/03\/01\/rails-3-plugins---part-1---the-big-picture\/\">Rails 3 Plugins - Part 1 - The Big Picture<\/a><br \/>\n<a href=\"http:\/\/www.engineyard.com\/blog\/2010\/extending-rails-3-with-railties\/\">Extending Rails 3 with Railties<\/a><br \/>\n<a href=\"http:\/\/api.rubyonrails.org\/classes\/Rails\/Railtie.html\">Class Rails::Railtie<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rails3 \u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b0\u7684\u65b9\u6cd5\u4f86\u64f4\u5145 Rails\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u7b97\u662f\u4e86\u89e3 Railtie \u5427\u3002(\u6211\u500b\u4eba\u5c0d Ge &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/4873-rails3-railtie-and-plugins\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Rails3: Railtie \u548c Plugins \u7cfb\u7d71\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[53],"tags":[],"class_list":["post-4873","post","type-post","status-publish","format-standard","hentry","category-rails","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-1gB","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=4873"}],"version-history":[{"count":32,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4873\/revisions"}],"predecessor-version":[{"id":5511,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4873\/revisions\/5511"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=4873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=4873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=4873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}