diff options
author | Mikel Lindsaar <raasdnil@gmail.com> | 2010-03-28 14:44:34 +1100 |
---|---|---|
committer | Mikel Lindsaar <raasdnil@gmail.com> | 2010-03-28 14:44:34 +1100 |
commit | 2bcc2ebf44b59e46c104c92d621e8051c97bfcf5 (patch) | |
tree | d2a3f04fd3020c1b5d88847af62d52f2d5e5bd61 /activeresource | |
parent | f5774e3e3f70a3acfa559b9ff889e9417fb71d4b (diff) | |
parent | 8398f21880a952769ccd6437a4344922fe596dab (diff) | |
download | rails-2bcc2ebf44b59e46c104c92d621e8051c97bfcf5.tar.gz rails-2bcc2ebf44b59e46c104c92d621e8051c97bfcf5.tar.bz2 rails-2bcc2ebf44b59e46c104c92d621e8051c97bfcf5.zip |
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'activeresource')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 49 | ||||
-rw-r--r-- | activeresource/lib/active_resource/railtie.rb | 4 | ||||
-rw-r--r-- | activeresource/test/setter_trap.rb | 2 |
3 files changed, 53 insertions, 2 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index b841108bd1..67296a171a 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -625,6 +625,22 @@ module ActiveResource "#{prefix(prefix_options)}#{collection_name}/#{id}.#{format.extension}#{query_string(query_options)}" end + # Gets the new element path for REST resources. + # + # ==== Options + # * +prefix_options+ - A hash to add a prefix to the request for nested URLs (e.g., <tt>:account_id => 19</tt> + # would yield a URL like <tt>/accounts/19/purchases/new.xml</tt>). + # + # ==== Examples + # Post.new_element_path + # # => /posts/new.xml + # + # Comment.collection_path(:post_id => 5) + # # => /posts/5/comments/new.xml + def new_element_path(prefix_options = {}) + "#{prefix(prefix_options)}#{collection_name}/new.#{format.extension}" + end + # Gets the collection path for the REST resources. If the +query_options+ parameter is omitted, Rails # will split from the +prefix_options+. # @@ -653,6 +669,19 @@ module ActiveResource alias_method :set_primary_key, :primary_key= #:nodoc: + # Builds a new, unsaved record using the default values from the remote server so + # that it can be used with RESTful forms. + # + # ==== Options + # * +attributes+ - A hash that overrides the default values from the server. + # + # Returns the new resource instance. + # + def build(attributes = {}) + attrs = connection.get("#{new_element_path}").merge(attributes) + self.new(attrs) + end + # Creates a new resource instance and makes a request to the remote service # that it be saved, making it equivalent to the following simultaneous calls: # @@ -989,6 +1018,22 @@ module ActiveResource end alias :new_record? :new? + # Returns +true+ if this object has been saved, otherwise returns +false+. + # + # ==== Examples + # persisted = Computer.create(:brand => 'Apple', :make => 'MacBook', :vendor => 'MacMall') + # persisted.persisted? # => true + # + # not_persisted = Computer.new(:brand => 'IBM', :make => 'Thinkpad', :vendor => 'IBM') + # not_persisted.persisted? # => false + # + # not_persisted.save + # not_persisted.persisted? # => true + # + def persisted? + !new? + end + # Gets the <tt>\id</tt> attribute of the resource. def id attributes[self.class.primary_key] @@ -1346,6 +1391,10 @@ module ActiveResource self.class.element_path(to_param, options || prefix_options) end + def new_element_path + self.class.new_element_path(prefix_options) + end + def collection_path(options = nil) self.class.collection_path(options || prefix_options) end diff --git a/activeresource/lib/active_resource/railtie.rb b/activeresource/lib/active_resource/railtie.rb index 27c88415f6..aa878c7212 100644 --- a/activeresource/lib/active_resource/railtie.rb +++ b/activeresource/lib/active_resource/railtie.rb @@ -3,10 +3,10 @@ require "rails" module ActiveResource class Railtie < Rails::Railtie - railtie_name :active_resource + config.active_resource = ActiveSupport::OrderedOptions.new require "active_resource/railties/log_subscriber" - log_subscriber ActiveResource::Railties::LogSubscriber.new + log_subscriber :active_resource, ActiveResource::Railties::LogSubscriber.new initializer "active_resource.set_configs" do |app| app.config.active_resource.each do |k,v| diff --git a/activeresource/test/setter_trap.rb b/activeresource/test/setter_trap.rb index 7cfd9ca111..437fbdad32 100644 --- a/activeresource/test/setter_trap.rb +++ b/activeresource/test/setter_trap.rb @@ -1,3 +1,5 @@ +require 'abstract_unit' + class SetterTrap < ActiveSupport::BasicObject class << self def rollback_sets(obj) |