Class ReactiveResource::Base
In: lib/reactive_resource/base.rb
Parent: ActiveResource::Base

The class that all ReactiveResourse resources should inherit from. This class fixes and patches over a lot of the broken stuff in Active Resource, and smoothes out the differences between the client-side Rails REST stuff and the server-side Rails REST stuff. It also adds support for ActiveRecord-like associations.

Methods

Attributes

associations  [RW]  Holds all the associations that have been declared for this class

Public Class methods

Generates the URL prefix that the belongs_to parameters and associations refer to. For example, a license with params :lawyer_id => 2 will return ‘lawyers/2/’ and a phone with params :address_id => 2, :lawyer_id => 3 will return ‘lawyers/3/addresses/2/’.

Add a parent-child relationship between attribute and this class. This allows parameters like attribute_id to contribute to generating nested urls. options is a hash of extra parameters:

:class_name
Override the class name of the target of the association. By default, this is based on the attribute name.

Returns all of the belongs_to associations this class has.

belongs_to in ReactiveResource works a little differently than ActiveRecord. Because we have to deal with full class hierachies in order to generate the full URL (as mentioned in association_prefix), we have to treat the belongs_to associations on objects that this object belongs_to as if they exist on this object itself. This method merges in all of this class’ associated classes’ belongs_to associations, so we can handle deeply nested routes. So, for instance, if we have phone \=> address => lawyer, phone will look for address’ belongs_to associations and merge them in. This allows us to have both lawyer_id and address_id at url generation time.

Override ActiveResource‘s collection_name to support singular names for singleton resources.

This method differs from its parent by adding association_prefix into the generated url. This is needed to support belongs_to associations.

Same as collection_path, except with an extra method_name on the end to support custom methods

Same as collection_path, except it adds the ID to the end of the path (unless it‘s a singleton resource)

Returns the extension based on the format (’.json’, for example), or the empty string if format doesn‘t specify an extension

Active Resource‘s find_one does nothing if you don‘t pass a +:from+ parameter. This doesn‘t make sense if you‘re dealing with a singleton resource, so if we don‘t get anything back from find_one, try hitting the element path directly

Add a has_many relationship to another class. options is a hash of extra parameters:

:class_name
Override the class name of the target of the association. By default, this is based on the attribute name.

Add a has_one relationship to another class. options is a hash of extra parameters:

:class_name
Override the class name of the target of the association. By default, this is based on the attribute name.

Fix up the klass attribute of all of our associations to point to the new child class instead of the parent class, so class lookup works as expected

All the classes that this class references in its belongs_to, along with their parents, and so on.

Returns a list of the belongs_to associations we will use to generate the full path for this resource.

Add all of the belongs_to attributes as prefix parameters. This is necessary to support nested url generation on our polymorphic associations, because we need some way of getting the attributes at the point where we need to generate the url, and only the prefix options are available for both finds and creates.

Call this method to transform a resource into a ‘singleton’ resource. This will fix the paths Active Resource generates for singleton resources. See rails.lighthouseapp.com/projects/8994/tickets/4348-supporting-singleton-resources-in-activeresource for more info.

true if this resource is a singleton resource, false otherwise

Public Instance methods

ActiveResource (as of 3.0) assumes that you have a to_x method on ActiveResource::Base for any format ‘x’ that is assigned to the record. This seems weird — the format should take care of encoding, not the object itself. To keep this as stable as possible, we should use to_x if it‘s defined, otherwise just delegate to the format‘s ‘encode’ function. This is how things worked as of Rails 2.3.

It‘s kind of redundant to have the server return the foreign keys corresponding to the belongs_to associations (since they‘ll be in the URL anyway), so we‘ll try to inject them based on the attributes of the object we just used.

In order to support two-way belongs_to associations in a reasonable way, we duplicate all of the prefix options as real attributes (so license.lawyer_id will set both the lawyer_id attribute and the lawyer_id prefix option). When we‘re ready to save, we should take all the parameters that we‘re already sending as prefix options and remove them from the model‘s attributes so we don‘t send duplicates down the wire. This way, if you have an object that belongs_to :lawyer and belongs_to :phone (in that order), setting lawyer_id and phone_id will treat lawyer_id as a prefix option and phone_id as a normal attribute.

[Validate]