{"id":4940,"date":"2010-10-25T07:08:25","date_gmt":"2010-10-24T23:08:25","guid":{"rendered":"http:\/\/ihower.tw\/blog\/?p=4940"},"modified":"2011-04-09T01:46:20","modified_gmt":"2011-04-08T17:46:20","slug":"rails3-activemodel","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/4940-rails3-activemodel","title":{"rendered":"Rails3: ActiveModel \u5be6\u4f5c"},"content":{"rendered":"<p>\u6211\u5728 <a href=\"http:\/\/ihower.tw\/blog\/archives\/3653\">Rails3 Beta \u767c\u4f48: \u91cd\u9ede\u5c0e\u89bd<\/a> \u4e2d\u63d0\u5230\u904e ActiveModel\uff0c\u4eca\u5929\u8b93\u6211\u5011\u66f4\u6df1\u5165\u770b\u770b\u4ec0\u9ebc\u6642\u5019\u6703\u7528\u5230\uff0c\u4ee5\u53ca\u600e\u9ebc\u4f7f\u7528\u5b83\u3002<\/p>\n<p>ActiveModel \u5b9a\u7fa9\u4e86\u4e00\u7d44\u4ecb\u9762\u53ef\u4ee5\u8207 ActionPack helpers \u929c\u63a5\uff0c\u4e5f\u63d0\u4f9b\u4e86\u8a31\u591a\u73fe\u6210\u7684 Module \u529f\u80fd\u3002\u4efb\u4f55 Class \u53ea\u8981\u5be6\u4f5c\u4e86 ActiveModel \u7684\u4ecb\u9762\uff0c\u7136\u5f8c\u9078\u914d include \u4f60\u9700\u8981\u7684 Module\uff0c\u5c31\u53ef\u4ee5\u8207 Rails3 helpers \u929c\u63a5\u5728\u4e00\u8d77\u4e86\u3002<\/p>\n<p>\u4ec0\u9ebc\u6642\u5019\u6703\u7528\u5230 ActiveModel \u5462? \u4f8b\u5982\u4f60\u60f3\u8981\u63db\u4e0d\u540c\u7684 ORM \u800c\u4e0d\u4f7f\u7528 ActiveRecord\uff0c\u6216\u662f\u4f60\u6c92\u6709 backend storage \u53ea\u662f\u60f3\u8981\u62ff\u4f86\u505a\u5176\u4ed6\u52d5\u4f5c(\u4f8b\u5982\u55ae\u7d14\u7684\u806f\u7d61\u8868\u55ae\u7528\u4f86\u5bc4\u4fe1)\u3002\u5728 <a href=\"http:\/\/www.slideshare.net\/pauldix\/building-web-service-clients-with-activemodel\">Building Web Service Clients with ActiveModel<\/a> \u9019\u7bc7\u6295\u5f71\u7247\u4e2d\uff0cPaul Dix \u4e5f\u5c07 ActiveModel \u61c9\u7528\u5728 SOA \u67b6\u69cb\u7684 client-side model (\u5c31\u4e0d\u8981\u7528 <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveResource\/Base.html\">ActiveResource<\/a> \u5427\uff0c\u7528 ActiveModel \u914d\u4e0a <a href=\"\/blog\/archives\/2941\">HTTP  client library<\/a> \u81ea\u5df1\u5beb\u4e00\u500b\u6bd4\u8f03\u5408\u7528)\u3002<\/p>\n<p>\u8b93\u6211\u5011\u4f86\u770b\u770b ActiveModel \u5b9a\u7fa9\u7684\u4ecb\u9762\u5427\uff0c\u4f60\u7684 Model \u5f97\u5be6\u4f5c\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p>1. to_model, to_key, to_param, persisted?<br \/>\n2. model_name \u4ee5\u53ca model_name.human, model_name.partial_path, model_name.singular, model_name.plural<br \/>\n3. valid?, errors \u4ee5\u53ca errors[], errors.full_messages<\/p>\n<p>\u9019\u6a23\u624d\u80fd\u8207 ActionPack helpers \u63a5\u8d77\u4f86(\u9019\u4e9b helpers \u4f8b\u5982 link_to, form_for \u7b49\u7b49)\u3002\u6240\u5e78\u8981\u9810\u8a2d\u5be6\u4f5c\u9019\u6a23\u4ecb\u9762\u975e\u5e38\u5bb9\u6613\uff0c\u9664\u4e86 persisted? \u4e4b\u5916\uff0cActiveModel \u90fd\u6709\u63d0\u4f9b\u73fe\u6210\u7684 Module \u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre>\r\n<code>\r\nclass YourModel\r\n    extend ActiveModel::Naming\r\n    include ActiveModel::Conversion\r\n    include ActiveModel::Validations\r\n     \r\n    def persisted?\r\n        false\r\n    end\r\nend\r\n<\/code>\r\n<\/pre>\n<p>\u5176\u4e2d persisted? \u7684\u5be6\u4f5c\u5f97\u4f9d\u7167\u4f60\u7684 backend storage \u800c\u5b9a\u3002\u5982\u679c\u4f60\u7684 Model \u6c92\u6709 backend storage\uff0c\u90a3\u9ebc\u5c31\u662f false\u3002<br \/>\n\u800c ActiveModel::Validations \u63d0\u4f9b\u4e86\u4f60\u719f\u6089\u7684\u5404\u7a2e validation \u65b9\u6cd5\uff0c\u4f8b\u5982 validates_presence_of, validates_format_of \u7b49\u7b49 (\u9664\u4e86 validates_uniqueness_of \u4e4b\u5916\uff0c\u539f\u56e0\u4e0d\u8b49\u81ea\u660e)\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4e0d\u60f3\u8981\u6574\u5957\u7684 validation \u6846\u67b6\uff0c\u4e5f\u53ef\u4ee5\u4e0d include ActiveModel::Validations\u3002\u4f60\u53ef\u4ee5\u8b93 valid? \u6046\u771f\uff0c\u7136\u5f8c\u4f7f\u7528 <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Errors.html\">ActiveModel::Errors<\/a> \u5b9a\u7fa9 errors \u65b9\u6cd5\u5373\u53ef\u6eff\u8db3\u4ecb\u9762\u8981\u6c42\uff1a<\/p>\n<pre>\r\n<code>\r\nclass YourModel\r\n    extend ActiveModel::Naming\r\n    include ActiveModel::Conversion\r\n\r\n    def valid?() true end\u2028\u2028\r\n\r\n    def errors\u2028    \r\n        @errors ||= ActiveModel::Errors.new(self)\u2028  \r\n    end\r\n\r\n    def persisted?\r\n        false\r\n    end\r\nend\r\n<\/code>\r\n<\/pre>\n<p>\u4ee5\u4e0a\u9019\u4e9b\u4ecb\u9762 ActiveModel \u4e5f\u6709\u63d0\u4f9b <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Lint\/Tests.html\">Lint Tests<\/a> \u53ef\u4ee5\u6e2c\u8a66\uff0c\u53ea\u8981 include \u5b83\u5373\u53ef\uff1a<\/p>\n<pre>\r\n<code>\r\nrequire 'test_helper'\r\nclass YourModelTest < ActiveSupport::TestCase\r\n  include ActiveModel::Lint::Tests\r\nend\r\n<\/code>\r\n<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f ActiveModel \u4ecb\u9762\u7684\u6700\u4f4e\u8981\u6c42\u4e86\uff0c\u900f\u904e ActiveModel \u5167\u5efa\u7684 <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Naming.html\">Naming<\/a>, <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Conversion.html\">Conversion<\/a>, <a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Validations.html\">Validations<\/a> module \u5c31\u53ef\u4ee5\u8f15\u6613\u5be6\u4f5c\u51fa\u4f86\u3002ActiveModel \u9084\u6709\u63d0\u4f9b\u8a31\u591a\u597d\u7528\u7684 Module \u4f86\u64f4\u5145\uff0c\u5305\u62ec\u6709\uff1a<\/p>\n<ul>\n<li><a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/AttributeMethods.html\">AttributeMethods<\/a><\/li>\n<li><a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Callbacks.html\">Callback<\/a><\/li>\n<li><a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Dirty.html\">Dirty<\/a><\/li>\n<li><a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/MassAssignmentSecurity\/ClassMethods.html\">MassAssignmentSecurity<\/a><\/li>\n<li><a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Observer.html\">Observing<\/a><\/li>\n<li><a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Serialization.html\">Serialization<\/a><\/li>\n<li><a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveModel\/Translation.html\">Translation<\/a><\/h3>\n<\/ul>\n<p>\u5c31\u4e0d\u4e00\u4e00\u8a73\u8ff0\u4e86\uff0c\u8a73\u7d30\u7684\u7528\u6cd5\u8acb\u770b\u539f\u59cb\u78bc\u53ca\u6587\u4ef6\u8aaa\u660e\u3002\u6700\u5f8c\u63d0\u4e00\u4e0b MassAssignment\u3002<\/p>\n<h3>MassAssignmentSecurity<\/h3>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u5be6\u4f5c MassAssignment \u529f\u80fd\uff0c\u5c31\u662f\u6709 YourModel.new(params[:model]) \u9019\u6a23\u7684\u4ecb\u9762\uff0c\u4f60\u53ef\u4ee5\u9019\u6a23\u5be6\u4f5c\uff1a<\/p>\n<pre>\r\n<code>\r\nclass YourModel\r\n    # ...\r\n    def initialize(attributes = {})\r\n        if attributes.present?\r\n          attributes.each { |k, v| send(\"#{k}=\", v) if respond_to?(\"#{k}=\") }\r\n        end\r\n      end\r\nend\r\n<\/code>\r\n<\/pre>\n<p>\u4f46\u662f\u63d0\u5230 MassAssignment\uff0c\u5c31\u53ef\u80fd\u6703\u6709 MassAssignment Security \u7684\u9700\u6c42\uff0c\u6211\u5011\u60f3\u8981\u4fdd\u8b77\u4e00\u4e9b\u7279\u5b9a\u7684\u5c6c\u6027\u4e0d\u80fd\u900f\u904e Mass Assignment \u4f86\u8a2d\u5b9a\u503c\u3002<br \/>\nActiveModel \u63d0\u4f9b\u4e86 MassAssignmentSecurity \u9019\u500b Module \u8b93\u6211\u5011\u53ef\u4ee5\u7372\u5f97 attr_protected \u548c attr_accessible \u65b9\u6cd5\u4f86\u4fdd\u8b77\u7279\u5b9a\u5c6c\u6027\u3002\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>\r\n<code>\r\nclass YourModel\r\n    # ...\r\n    include ActiveModel::MassAssignmentSecurity\r\n    \r\n    def initialize(attributes = {})\r\n        if attributes.present?\r\n          sanitize_for_mass_assignment(attributes).each { |k, v| send(\"#{k}=\", v) if respond_to?(\"#{k}=\") }\r\n        end\r\n    end\r\nend\r\n<\/code>\r\n<\/pre>\n<h3>\u53c3\u8003\u8cc7\u6599<\/h3>\n<ul>\n<li><a herf=\"http:\/\/asciicasts.com\/episodes\/219-active-model\">ASCIIcasts: Active Model<\/a>: \u793a\u7bc4\u5be6\u4f5c\u4e00\u500b  Message model \u7528\u4f86\u5bc4\u4fe1\u800c\u4e0d\u662f\u5b58\u9032\u8cc7\u6599\u5eab<\/li>\n<li>Building Web Service Clients with ActiveModel: <a href=\"http:\/\/www.slideshare.net\/pauldix\/building-web-service-clients-with-activemodel\">\u6295\u5f71\u7247<\/a> \u53ca <a href=\"http:\/\/www.softdevtube.com\/2010\/06\/29\/building-web-service-clients-with-activemodel\/\">\u5f71\u7247<\/a><\/li>\n<li><a href=\"http:\/\/www.engineyard.com\/blog\/2010\/rails-and-merb-merge-orm-agnosticism-part-5-of-6\/\">Rails and Merb Merge: ORM Agnosticism (Part 5 of 6)<\/a>: ActiveModel \u8a2d\u8a08\u8aaa\u660e<\/li>\n<li><a href=\"http:\/\/yehudakatz.com\/2010\/01\/10\/activemodel-make-any-ruby-object-feel-like-activerecord\/\">ActiveModel: Make Any Ruby Object Feel Like ActiveRecord<\/a>: \u6700\u65e9\u7684 ActiveModel \u8a2d\u8a08\u8aaa\u660e\uff0c\u6ce8\u610f\u5230\u4ecb\u9762\u7565\u6709\u6539\uff0c\u6700\u65e9\u7684 new_record? \u548c destroyed? \u88ab\u7c21\u5316\u6210 persisted? <\/li>\n<li><a href=\"http:\/\/github.com\/novagile\/basic_active_model\">BasicActiveModel<\/a>: \u4e00\u500b\u57fa\u672c\u7684 ActiveModel \u5be6\u4f5c\u7bc4\u4f8b<\/li>\n<li><a href=\"http:\/\/github.com\/maccman\/supermodel\">SuperModel<\/a>: \u4e00\u500b Ruby in-memory models \u7684\u61c9\u7528\u5be6\u4f5c<\/li>\n<li><a href=\"http:\/\/github.com\/plataformatec\/mail_form>MailForm<\/a>: \u4e00\u500b Mail form \u7684\u61c9\u7528<\/li>\n<li><a href=\"http:\/\/forrst.com\/posts\/Contact_form_using_Active_Model-u1G>Contact form<\/a>: \u4e00\u500b\u66f4\u7c21\u55ae\u7684 Contact form \u61c9\u7528<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u5728 Rails3 Beta \u767c\u4f48: \u91cd\u9ede\u5c0e\u89bd \u4e2d\u63d0\u5230\u904e ActiveModel\uff0c\u4eca\u5929\u8b93\u6211\u5011\u66f4\u6df1\u5165\u770b\u770b\u4ec0\u9ebc\u6642\u5019 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/4940-rails3-activemodel\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Rails3: ActiveModel \u5be6\u4f5c\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-4940","post","type-post","status-publish","format-standard","hentry","category-rails","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-1hG","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4940","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=4940"}],"version-history":[{"count":33,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4940\/revisions"}],"predecessor-version":[{"id":5509,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4940\/revisions\/5509"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=4940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=4940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=4940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}