{"id":3949,"date":"2010-03-12T07:29:45","date_gmt":"2010-03-11T23:29:45","guid":{"rendered":"http:\/\/ihower.tw\/blog\/?p=3949"},"modified":"2011-04-09T01:53:55","modified_gmt":"2011-04-08T17:53:55","slug":"rails3-modularity-activesupport-concern","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/3949-rails3-modularity-activesupport-concern","title":{"rendered":"\u6df1\u5165Rails3: ActiveSupport::Concern"},"content":{"rendered":"<p>(<a href=\"http:\/\/en.ihower.tw\/post\/454873995\/rails3-activesupport-concern\">English version<\/a>)<\/p>\n<p>ActiveSupport::Concern \u662f Rails3 \u505a Modularity \u7684\u4e00\u500b\u91cd\u8981\u7684\u5c0f\u5de5\u5177\u3002\u4ed6\u7684\u4efb\u52d9\u662f\u8b93\u7ba1\u7406 modules \u4e4b\u9593\u7684 dependencies \u8b8a\u5f97\u5bb9\u6613\u3002<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u5169\u500b Modules \u6709\u4f9d\u5b58\u95dc\u4fc2\uff0cmodule Bar \u4f9d\u5b58\u65bc module Foo\uff0c\u7136\u5f8c\u6709\u4e00\u500b\u5bbf\u4e3b Host \u985e\u5225\u5e0c\u671b include Bar \u7684\u529f\u80fd\uff0c\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u5beb:<\/p>\n<pre>\r\n<code>\r\nmodule Foo\r\n   # self.included \u9019\u500b\u51fd\u5f0f\u6703\u5728 Foo \u88ab include \u6642\u57f7\u884c\r\n    def self.included(base)\r\n        base.send(:do_host_something) # \u5c0d\u5bbf\u4e3b\u505a\u67d0\u4e9b\u64cd\u4f5c\uff0c\u4f8b\u5982\u589e\u5f37\u529f\u80fd\u7b49\u7b49\r\n    end\r\nend\r\n\r\nmodule Bar\r\n    def self.included(base)\r\n        base.send(:do_host_something)\r\n    end\r\nend\r\n\r\nclass Host\r\n  include Foo, Bar\r\nend\r\n<\/code>\r\n<\/pre>\n<p>\u9019\u6709\u500b\u8a0e\u53ad\u7684\u7f3a\u9ede\u5c31\u662f\uff0c\u6211\u5011\u5fc5\u9808\u5728\u5bbf\u4e3b\u4e2d\u540c\u6642 include Foo \u8ddf Bar\uff0c\u4e5f\u5c31\u662f\u8981\u628a\u6240\u6709\u4f9d\u5b58\u7684 modules \u90fd include \u9032\u4f86\u3002\u9019\u5f88\u7cdf\u7cd5\u554a\uff0c\u70ba\u4ec0\u9ebc\u6211\u5011\u9700\u8981\u5728 Host \u88e1\u9762\u77e5\u9053\u9019\u4e9b modules \u7684\u4f9d\u5b58\u95dc\u4fc2\u5462 :\/<\/p>\n<p>\u6211\u5011\u5e0c\u671b\u80fd\u5920\u5c07 modules \u7684\u4f9d\u5b58\u95dc\u4fc2\u5beb\u5728 module \u4e2d\uff0c\u800c\u5bbf\u4e3b Host \u5c31\u53ea\u8981\u4f7f\u7528\u5c31\u597d\u4e86\u3002\u6240\u4ee5\u6211\u5011\u8a66\u8457\u6539\u5beb\u6210:<\/p>\n<pre>\r\n<code>\r\nmodule Bar\r\n  include Foo # \u56e0\u70ba Bar \u4f9d\u5b58\u65bc Foo\uff0c\u6240\u4ee5\u6211\u5011\u5728\u9019\u88e1 include \u5b83\r\n\r\n  def self.included(base)\r\n    base.send(:do_host_something)\r\n  end\r\n\r\nend\r\n\r\nclass Host\r\n  include Bar # \u53ea\u8981 include Bar \u5c31\u597d\uff0c\u4e0d\u9700\u8981\u77e5\u9053 Bar \u9084\u4f9d\u5b58\u54ea\u4e9b modules\r\nend\r\n<\/code>\r\n<\/pre>\n<p>\u9019\u6a23\u4e4d\u770b\u4e4b\u4e0b\u597d\u50cf\u6c92\u554f\u984c\uff0c\u4f46\u662f\u537b\u6709\u500b\u56b4\u91cd\u7684\u554f\u984c\u5c0e\u81f4\u7121\u6cd5\u57f7\u884c\uff0c\u56e0\u70ba Foo \u8b8a\u6210\u662f\u7531 Bar \u6240 include\uff0c\u6240\u4ee5\u5c0d Foo \u7684 self.included \u4f86\u8aaa\uff0c\u4ed6\u7684\u53c3\u6578 base \u8b8a\u6210\u4e86 Bar \u4e86\uff0c\u6240\u4ee5\u4ed6\u5c31\u6c92\u8fa6\u6cd5\u5b58\u53d6\u5230\u5bbf\u4e3b Host \u7684\u4efb\u4f55\u51fd\u5f0f\u53ca\u8b8a\u6578\uff0cdo_host_something \u6642\u5c31\u6703\u5931\u6557\u3002<\/p>\n<p>Okay\uff0cActiveSupport::Concern \u5c31\u662f\u4f86\u5e6b\u52a9\u89e3\u6c7a\u9019\u500b\u96e3\u984c\uff0c\u6211\u5011\u5e0c\u671b\u5bbf\u4e3b\u53ef\u4ee5\u4e0d\u9700\u8981\u77e5\u9053 modules \u4e4b\u9593\u7684 dependencies \u95dc\u4fc2\u3002dependencies \u95dc\u4fc2\u5beb\u5728 module \u88e1\u9762\u5c31\u597d\u4e86\u3002<\/p>\n<pre>\r\n<code>\r\nrequire 'active_support\/concern'\r\n\r\nmodule Foo\r\n    extend ActiveSupport::Concern\r\n    included do\r\n        self.send(:do_host_something)\r\n    end\r\nend\r\n\r\nmodule Bar\r\n    extend ActiveSupport::Concern\r\n    include Foo # \u56e0\u70ba Bar \u4f9d\u5b58\u65bc Foo\uff0c\u6240\u4ee5\u6211\u5011\u5728\u9019\u88e1 include \u5b83\r\n    \r\n    included do\r\n        self.send(:do_host_something)\r\n    end\r\nend\r\n\r\nclass Host\r\n  include Bar # \u53ea\u8981 include Bar \u5c31\u597d\uff0c\u4e0d\u9700\u8981\u77e5\u9053 Bar \u9084\u4f9d\u5b58\u54ea\u4e9b modules\r\nend\r\n\r\n<\/code>\r\n<\/pre>\n<p>\u5982\u6b64\u5c31\u641e\u5b9a\u4e86\u3002One more thing\uff0c\u5982\u679c\u4f60\u6709\u5b9a\u7fa9 module ClassMethods \u548c module InstanceMethods \u5728\u88e1\u9762\u7684\u8a71\uff0c\u5b83\u4e5f\u6703\u81ea\u52d5\u5e6b\u4f60\u8f09\u5165\u5230\u5bbf\u4e3b\u88e1\u9762\u53bb\uff0c\u5c31\u4e0d\u7528\u81ea\u5df1\u5beb send(:include, InstanceMethods) \u8ddf send(:extend, ClassMethods) \u4e86\u3002\u7528\u6cd5\u8209\u4f8b\uff1a<\/p>\n<pre>\r\n<code>\r\n\r\nmodule Foo\r\n    extend ActiveSupport::Concern\r\n    included do\r\n        self.send(:do_host_something)\r\n    end\r\n\r\n   module ClassMethods\r\n      def bite\r\n        # do something\r\n      end\r\n   end\r\n\r\n   module InstanceMethods\r\n      def poke\r\n         # do something\r\n      end\r\n   end\r\nend\r\n\r\n<\/code>\r\n<\/pre>\n<p>\u60f3\u77e5\u9053 ActiveSupport::Concern \u5230\u5e95\u600e\u9ebc\u5be6\u4f5c\u7684\u8a71\uff0c\u8acb\u770b <a href=\"http:\/\/github.com\/rails\/rails\/blob\/master\/activesupport\/lib\/active_support\/concern.rb\">\/activesupport\/lib\/active_support\/concern.rb<\/a>\uff0c\u53ea\u6709 29 \u884c\uff0c\u800c\u4e14 ActiveSupport::Concern \u4e5f\u6c92\u6709\u518d\u4f9d\u5b58\u5176\u4ed6\u6771\u897f\u4e86\uff0c\u563f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(English version) ActiveSupport::Concern \u662f Rails3 \u505a Mod &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/3949-rails3-modularity-activesupport-concern\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u6df1\u5165Rails3: ActiveSupport::Concern\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,31],"tags":[],"class_list":["post-3949","post","type-post","status-publish","format-standard","hentry","category-rails","category-ruby","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-11H","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/3949","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=3949"}],"version-history":[{"count":25,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/3949\/revisions"}],"predecessor-version":[{"id":5528,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/3949\/revisions\/5528"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=3949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=3949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=3949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}