From 2177282262195f9decf564b237304a6ff94c3048 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Sat, 30 Jul 2011 18:19:43 +0530 Subject: Active Resouce guide initial load --- railties/guides/source/active_resource_basics.textile | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 railties/guides/source/active_resource_basics.textile (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile new file mode 100644 index 0000000000..5df8b6612d --- /dev/null +++ b/railties/guides/source/active_resource_basics.textile @@ -0,0 +1,11 @@ +h2. Active Resource Basics + +This guide should provide you with all you need to get started managing the connection between business objects and RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics. + +endprologue. + +WARNING. This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails. + +h3. Changelog + +* July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai \ No newline at end of file -- cgit v1.2.3 From 5da1ddb7506d4dc1b314f6634e4c28e13e6eeaa2 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Sat, 30 Jul 2011 18:21:21 +0530 Subject: Introduction for active resource --- railties/guides/source/active_resource_basics.textile | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile index 5df8b6612d..5cf22f8e39 100644 --- a/railties/guides/source/active_resource_basics.textile +++ b/railties/guides/source/active_resource_basics.textile @@ -6,6 +6,10 @@ endprologue. WARNING. This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails. +h3. Introduction + +Active Resource allows you to connect with RESTful web services. So, in Rails, Resource classes inherited from +ActiveResource::Base+ and live in +app/models+. + h3. Changelog * July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai \ No newline at end of file -- cgit v1.2.3 From 95c2230f0577c8f3ef2312fe9268dc9c0ed3bf8a Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Sat, 30 Jul 2011 18:23:05 +0530 Subject: configuration for active resource --- railties/guides/source/active_resource_basics.textile | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile index 5cf22f8e39..f0cd25bfc2 100644 --- a/railties/guides/source/active_resource_basics.textile +++ b/railties/guides/source/active_resource_basics.textile @@ -10,6 +10,17 @@ h3. Introduction Active Resource allows you to connect with RESTful web services. So, in Rails, Resource classes inherited from +ActiveResource::Base+ and live in +app/models+. +h3. Configuration and Usage + +Putting Active Resource to use is very similar to Active Record. It's as simple as creating a model class +that inherits from ActiveResource::Base and providing a site class variable to it: + + +class Person < ActiveResource::Base + self.site = "http://api.people.com:3000/" +end + + h3. Changelog * July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai \ No newline at end of file -- cgit v1.2.3 From ad55b1aa86a1d8b7d6e9e35c7cce77b122bca1e1 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Sat, 30 Jul 2011 18:24:18 +0530 Subject: usages of active resouce --- railties/guides/source/active_resource_basics.textile | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile index f0cd25bfc2..64f0949475 100644 --- a/railties/guides/source/active_resource_basics.textile +++ b/railties/guides/source/active_resource_basics.textile @@ -21,6 +21,15 @@ class Person < ActiveResource::Base end +Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes +life cycle methods that operate against a persistent store. + + +# Find a person with id = 1 +ryan = Person.find(1) +Person.exists?(1) # => true + + h3. Changelog * July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai \ No newline at end of file -- cgit v1.2.3 From 61899bff17e161dbd706bfb900ac212fe90c3acd Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Mon, 1 Aug 2011 20:37:42 +0530 Subject: Active Resource - guide for reading and writing data --- railties/guides/source/active_resource_basics.textile | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile index 64f0949475..8a36622814 100644 --- a/railties/guides/source/active_resource_basics.textile +++ b/railties/guides/source/active_resource_basics.textile @@ -24,10 +24,21 @@ end Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes life cycle methods that operate against a persistent store. +h3. Reading and Writing Data + +Active Resource make request over HTTP using a standard JSON format. It mirrors the RESTful routing built into Action Controller but will also work with any other REST service that properly implements the protocol. + +h4. Read + +Read requests use the GET method and expect the JSON form of whatever resource/resources is/are being requested. + # Find a person with id = 1 ryan = Person.find(1) +# Check if a person exists with id = 1 Person.exists?(1) # => true +# Get all resources of Person class +Person.all h3. Changelog -- cgit v1.2.3 From aa9da1fe70a109f86922b4ee83039f1cae1e6584 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Mon, 1 Aug 2011 20:48:45 +0530 Subject: Active Resource - guide for create --- railties/guides/source/active_resource_basics.textile | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile index 8a36622814..e40ca4fc42 100644 --- a/railties/guides/source/active_resource_basics.textile +++ b/railties/guides/source/active_resource_basics.textile @@ -34,13 +34,22 @@ Read requests use the GET method and expect the JSON form of whatever resource/r # Find a person with id = 1 -ryan = Person.find(1) +person = Person.find(1) # Check if a person exists with id = 1 Person.exists?(1) # => true # Get all resources of Person class Person.all +h4. Create + +Creating a new resource submits the JSON form of the resource as the body of the request with HTTP POST method and parse the response into Active Resource object. + + +person = Person.create(:name => 'Vishnu') +person.id # => 1 + + h3. Changelog * July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai \ No newline at end of file -- cgit v1.2.3 From 212654be02e28ee97d75819b0f799ccb6f7b9130 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Mon, 1 Aug 2011 20:57:57 +0530 Subject: Active Resource - guide for update/save --- railties/guides/source/active_resource_basics.textile | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile index e40ca4fc42..1115b9848f 100644 --- a/railties/guides/source/active_resource_basics.textile +++ b/railties/guides/source/active_resource_basics.textile @@ -50,6 +50,16 @@ person = Person.create(:name => 'Vishnu') person.id # => 1 +h4. Update + +To update an existing resource, 'save' method is used. This method make a HTTP PUT request in JSON format. + + +person = Person.find(1) +person.name = 'Atrai' +person.save + + h3. Changelog * July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai \ No newline at end of file -- cgit v1.2.3 From 1ea8d9c38d280e6f55ac3ad1ad4d27d48dcddc15 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Mon, 1 Aug 2011 21:02:48 +0530 Subject: Active Resource - guide for destroy --- railties/guides/source/active_resource_basics.textile | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile index 1115b9848f..332d113fa7 100644 --- a/railties/guides/source/active_resource_basics.textile +++ b/railties/guides/source/active_resource_basics.textile @@ -60,6 +60,15 @@ person.name = 'Atrai' person.save +h4. Delete + +'destroy' method makes a HTTP DELETE request for an existing resource in JSON format to delete that resource. + + +person = Person.find(1) +person.destroy + + h3. Changelog * July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai \ No newline at end of file -- cgit v1.2.3 From 7963099b904031fef7d593d5be7e2061d1bcbfe8 Mon Sep 17 00:00:00 2001 From: Sukeerthi Adiga G Date: Fri, 5 Aug 2011 13:13:31 +0530 Subject: ActiveResource::Validations module basics updated --- .../guides/source/active_resource_basics.textile | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile index 332d113fa7..3294227f7b 100644 --- a/railties/guides/source/active_resource_basics.textile +++ b/railties/guides/source/active_resource_basics.textile @@ -69,6 +69,56 @@ person = Person.find(1) person.destroy +h3. Validations + +Module to support validation and errors with Active Resource objects. The module overrides Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned in the web service response. The module also adds an errors collection that mimics the interface of the errors provided by ActiveRecord::Errors. + +h4. Validating client side resources by overriding validation methods in base class + + +class Person < ActiveResource::Base + self.site = "http://api.people.com:3000/" + + protected + + def validate + errors.add("last", "has invalid characters") unless last =~ /[a-zA-Z]*/ + end +end + + +h4. Validating client side resources + +Consider a Person resource on the server requiring both a first_name and a last_name with a validates_presence_of :first_name, :last_name declaration in the model: + + +person = Person.new(:first_name => "Jim", :last_name => "") +person.save # => false (server returns an HTTP 422 status code and errors) +person.valid? # => false +person.errors.empty? # => false +person.errors.count # => 1 +person.errors.full_messages # => ["Last name can't be empty"] +person.errors[:last_name] # => ["can't be empty"] +person.last_name = "Halpert" +person.save # => true (and person is now saved to the remote service) + + +h4. Public instance methods + +ActiveResource::Validations have three public instance methods + +h5. errors() + +This will return errors object that holds all information about attribute error messages + +h5. save_with_validation(options=nil) + +This validates the resource with any local validations written in base class and then it will try to POST if there are no errors. + +h5. valid? + +Runs all the local validations and will return true if no errors. + h3. Changelog * July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai \ No newline at end of file -- cgit v1.2.3 From 9980f46ca2d250d1d3e2fac84c5dc9ca6cbab1ea Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Thu, 15 Sep 2011 00:13:29 +0530 Subject: No more changelogs inside guides --- railties/guides/source/active_resource_basics.textile | 4 ---- 1 file changed, 4 deletions(-) (limited to 'railties/guides/source/active_resource_basics.textile') diff --git a/railties/guides/source/active_resource_basics.textile b/railties/guides/source/active_resource_basics.textile index 3294227f7b..851aac1a3f 100644 --- a/railties/guides/source/active_resource_basics.textile +++ b/railties/guides/source/active_resource_basics.textile @@ -118,7 +118,3 @@ This validates the resource with any local validations written in base class and h5. valid? Runs all the local validations and will return true if no errors. - -h3. Changelog - -* July 30, 2011: Initial version by "Vishnu Atrai":http://github.com/vatrai \ No newline at end of file -- cgit v1.2.3