{"id":1719,"date":"2008-04-07T03:56:04","date_gmt":"2008-04-06T19:56:04","guid":{"rendered":"http:\/\/ihower.idv.tw\/blog\/?p=1719"},"modified":"2015-09-05T10:47:37","modified_gmt":"2015-09-05T02:47:37","slug":"rails2-activesupport","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/1719-rails2-activesupport","title":{"rendered":"\u5c0f\u63a2 Rails ActiveSupport"},"content":{"rendered":"<p>Update(2008\/4\/9):  \u9019\u7bc7 <a href=\"http:\/\/errtheblog.com\/posts\/42-rails-rubyisms-advent\">RAILS RUBYISMS ADVENT<\/a>\u4e5f\u53ef\u4ee5\u4e00\u770b\u3002<\/p>\n<p>ActiveSupport \u662f Rails \u7684\u5de5\u5177\u7bb1\u3002\u6700\u8fd1\u5728\u770b Advanced Rails, O&#8217;Reilly \u4e00\u66f8\uff0c\u6709\u5e7e\u6a23\u6771\u897f\u503c\u5f97\u8a18\u4e0a\u4e00\u7b46\uff1a<\/p>\n<h3>JSON<\/h3>\n<p>\u6211\u5011\u6709 Object#to_json\uff0c\u7269\u4ef6\u5982 array,hash \u7b49\u90fd\u53ef\u4ee5\u547c\u53eb to_json \u8f49 JSON \u683c\u5f0f\uff0c\u975e\u5e38\u65b9\u4fbf\u8207 JavaScript \u505a\u929c\u63a5\u3002<\/p>\n<h3>Blank<\/h3>\n<p>\u6240\u6709\u7684\u7269\u4ef6\u90fd\u52a0\u4e0a\u4e86 blank? \u9019\u500b\u51fd\u5f0f\uff0c\u56de\u50b3 true \u5982\u679c\u662f 1. \u7a7a\u5b57\u4e32 2. \u53ea\u542b\u7a7a\u767d\u7684\u5b57\u4e32 3. false 4. nil 5. empty array [] 6. empty hash {}\u3002\u6240\u4ee5\u5225\u518d\u5beb ( s.nil? || s.empty? ) \u5566\u3002<\/p>\n<h3>Class Attribute Accessors<\/h3>\n<p>\u53ef\u7528\u5ba3\u544a\u7684\u65b9\u5f0f\u5b9a\u7fa9 Class Attribute\uff0c\u5982<\/p>\n<pre>\n  class Foo\n     cattr_accessor :bar\n     self.bar = \"\"\n  end\n<\/pre>\n<p>\u9019\u6a23\u6703\u5b9a\u7fa9\u51fa\u4f86\u7684 C.bar \u5373 @@bar<\/p>\n<h3>Class Inheritable Attributes<\/h3>\n<p>Class Attribute \u662f\u6574\u500b\u985e\u5225\u7e7c\u627f\u9ad4\u7cfb\u5171\u7528\uff0c\u9019\u5728\u6211\u5011\u5beb ActiveRecord \u76f8\u95dc plugin \u6642\u975e\u5e38\u4e0d\u9069\u7528\uff0c\u56e0\u70ba\u6240\u6709\u7684 model \u90fd\u7e7c\u627f\u81ea ActiveRecord\uff0c\u4f46\u662f\u5404\u81ea\u53c8\u8981\u6709\u4e0d\u540c\u7684 Class Attributes \u503c\u3002\u6700\u5e38\u898b\u7684\u4f7f\u7528\u72c0\u6cc1\u5c31\u662f plugin \u4e86\uff0cmodel A \u548c model B \u90fd include \u67d0\u500b plugin\uff0c\u4f46\u662f\u9019\u500b plugin \u7684\u8a2d\u5b9a\u503c\u8981\u4e0d\u4e00\u6a23\u3002\u62ff\u5927\u5bb6\u90fd\u5728\u7528\u7684 attactment_fu \u8209\u4f8b\uff1a<\/p>\n<pre>\nclass UploadImage < ActiveRecord::Base\n  has_attachment :content_type => :image, :storage => :file_system\nend\n<\/pre>\n<p>\u7ffb\u51fa has_attachment \u7684 source code \u4f60\u5c31\u770b\u5230\u9019\u62db\u4e86\uff1a<\/p>\n<pre>\ndef has_attachment(options = {})\n      ......\n     class_inheritable_accessor :attachment_options\n     self.attachment_options = options    # \u9019\u88e1\u7684 self \u6307\u7684\u662f UploadImage\n      ......\nend\n<\/pre>\n<p>\u9664\u4e86 class_inheritable_accessor(<em>syms) \uff0c\u9084\u6709 class_inheritable_array(<\/em>syms) \u548cclass_inheritable_hash(*syms) \u7b49\u3002<\/p>\n<p>Class Attribute Accessors \u7684\u539f\u7406\u53ef\u4ee5\u8acb\u53c3\u8003 <a href=\"\/blog\/archives\/1696\">class instance variables<\/a> \u9019\u7bc7\u3002<\/p>\n<h3>Date and Time conversions<\/h3>\n<p>\u4e0d\u9700\u8981\u6bcf\u6b21\u5beb Helper \u7528 strftime\uff0c\u6211\u5011\u53ef\u5728 environment.rb \u65b0\u589e\u81ea\u8a02\u7684 format\uff0c\u4f8b\u5982<\/p>\n<pre>\n ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!( :foo => '%m\/%d %l:%M %p')\n<\/pre>\n<p>\u9019\u6a23\u5c31\u53ef\u4ee5\u5c0d Time \u7269\u4ef6\u547c\u53eb to_s(:foo)\uff0c\u5167\u5efa\u9084\u6709 :default, :short, :long, :db \u7b49\u7b49\u3002<\/p>\n<h3>alias_method_chain<\/h3>\n<p>\u5728 Rails source code \u5341\u5206\u5e38\u898b\uff1a<\/p>\n<pre>\n  alias_method_chain :target, :feature\n<\/pre>\n<p>\u7b49\u540c\u65bc<\/p>\n<pre>\n  alias_method :target_without_feature, :target\n  alias_method :target, :target_with_feature\n<\/pre>\n<h3>Delegation<\/h3>\n<p>\u5c07 methods \u50b3\u7d66\u53e6\u4e00\u500b object<\/p>\n<pre>\n  class Account < ActiveRecord::Base\n      has_one :foo\n      delegate :free?, :to => :foo\n  end\n<\/pre>\n<p>\u9019\u6a23 account.free? \u5c31\u6703\u547c\u53eb account.foo.free? \u8003\u616e foo \u53ef\u80fd nil\uff0c\u6211\u5011\u53ef\u4ee5\u591a\u4e00\u500b\u6aa2\u67e5\uff1a<\/p>\n<pre>\n  delegate :free?, :to => \"something.nil? ? false : something\"\n<\/pre>\n<p>\u751a\u81f3\u5169\u5c64\uff0c\u5047\u8a2d foo \u6709 bar\uff1a<\/p>\n<pre>\n  delegate :free?, :to => \"foo.bar\"\n<\/pre>\n<p>\u9019\u6a23 account.free? \u5c31\u6703\u547c\u53eb account.foo.bar.free?<\/p>\n<h3>#Object#returning<\/h3>\n<p>\u8b93\u4f60\u57f7\u884c\u4e00\u4e9b\u64cd\u4f5c\u7136\u5f8c\u50b3\u56de\uff1a<\/p>\n<pre>\n returning(User.new) do |u|\n   u.name = \"Foo\"\n end\n<\/pre>\n<h3>#Object#with_options<\/h3>\n<p>\u6700\u5e38\u7528\u5728 routes.rb\uff0c\u4e0d\u904e\u5176\u5be6\u4efb\u610f\u7269\u4ef6\u90fd\u53ef\u4ee5\u7528\uff0c\u4ed6\u6703\u5c07\u53c3\u6578\u81ea\u52d5 merge \u5230 method call \u7684 hash \u53c3\u6578\uff1a<\/p>\n<pre>\n map.with_options( :controller => \"people\" ) do |p|\n   p.root :action => \"index\"\n   p.about :action => \"about\"\n end\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Update(2008\/4\/9): \u9019\u7bc7 RAILS RUBYISMS ADVENT\u4e5f\u53ef\u4ee5\u4e00\u770b\u3002 Active &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/1719-rails2-activesupport\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u5c0f\u63a2 Rails ActiveSupport\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":[5,53],"tags":[],"class_list":["post-1719","post","type-post","status-publish","format-standard","hentry","category-programming","category-rails","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-rJ","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1719","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=1719"}],"version-history":[{"count":2,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1719\/revisions"}],"predecessor-version":[{"id":8278,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1719\/revisions\/8278"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=1719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=1719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=1719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}