lib/active_resource/associations.rb in embark-journey-0.1.8 vs lib/active_resource/associations.rb in embark-journey-0.2.1
- old
+ new
@@ -1,16 +1,24 @@
-module ActiveResource::Associations
+# frozen_string_literal: true
+module ActiveResource::Associations
module Builder
- autoload :Association, 'active_resource/associations/builder/association'
- autoload :HasMany, 'active_resource/associations/builder/has_many'
- autoload :HasOne, 'active_resource/associations/builder/has_one'
- # autoload :BelongsTo, 'active_resource/associations/builder/belongs_to'
+ autoload :Association, "active_resource/associations/builder/association"
+ autoload :HasMany, "active_resource/associations/builder/has_many"
+ autoload :HasOne, "active_resource/associations/builder/has_one"
+ # autoload :BelongsTo, "active_resource/associations/builder/belongs_to"
require_relative 'associations/builder/belongs_to'
end
+ attr_accessor :embeds
+ def defines_belongs_to_embed(reflection)
+ self.embeds ||= []
+ self.embeds << reflection.name.to_s
+ end
+
+
# Specifies a one-to-many association.
#
# === Options
# [:class_name]
# Specify the class name of the association. This class name would
@@ -63,13 +71,13 @@
#
# <tt>has_one :author, :class_name => 'myblog/author'</tt>
# Would resolve this author into the <tt>Myblog::Author</tt> class.
#
# If the response body does not contain an attribute matching the association name
- # a request is sent to a singelton path under the current resource.
+ # a request is sent to a singleton path under the current resource.
# For example, if a Product class <tt>has_one :inventory</tt> calling <tt>Product#inventory</tt>
- # will generate a request on /product/:product_id/inventory.json.
+ # will generate a request on /products/:product_id/inventory.json.
#
def has_one(name, options = {})
Builder::HasOne.build(self, name, options)
end
@@ -87,19 +95,19 @@
# (+association+ is replaced with the symbol passed as the first argument, so
# <tt>belongs_to :post</tt> would add among others <tt>post.nil?</tt>.
#
# === Example
#
- # A Comment class declaress <tt>belongs_to :post</tt>, which will add:
+ # A Comment class declares <tt>belongs_to :post</tt>, which will add:
# * <tt>Comment#post</tt> (similar to <tt>Post.find(post_id)</tt>)
# The declaration can also include an options hash to specialize the behavior of the association.
#
# === Options
# [:class_name]
- # Specify the class name for the association. Use it only if that name canÄt be inferred from association name.
+ # Specify the class name for the association. Use it only if that name can't be inferred from association name.
# So <tt>belongs_to :post</tt> will by default be linked to the Post class, but if the real class name is Article,
- # you'll have to specify it with whis option.
+ # you'll have to specify it with this option.
# [:foreign_key]
# Specify the foreign key used for the association. By default this is guessed to be the name
# of the association with an "_id" suffix. So a class that defines a <tt>belongs_to :post</tt>
# association will use "post_id" as the default <tt>:foreign_key</tt>. Similarly,
# <tt>belongs_to :article, :class_name => "Post"</tt> will use a foreign key
@@ -110,16 +118,17 @@
# Creates a belongs_to association called customer which is represented through the <tt>User</tt> class.
#
# <tt>belongs_to :customer, :foreign_key => 'user_id'</tt>
# Creates a belongs_to association called customer which would be resolved by the foreign_key <tt>user_id</tt> instead of <tt>customer_id</tt>
#
- def belongs_to(name, options={})
+ def belongs_to(name, options = {})
Builder::BelongsTo.build(self, name, options)
end
# Defines the belongs_to association finder method
- def defines_belongs_to_finder_method(method_name, association_model, finder_key)
+ def defines_belongs_to_finder_method(reflection)
+ method_name = reflection.name
ivar_name = :"@#{method_name}"
if method_defined?(method_name)
instance_variable_set(ivar_name, nil)
remove_method(method_name)
@@ -128,53 +137,52 @@
define_method(method_name) do
if instance_variable_defined?(ivar_name)
instance_variable_get(ivar_name)
elsif attributes.include?(method_name)
attributes[method_name]
- elsif association_id = send(finder_key)
+ elsif association_id = send(reflection.foreign_key)
return nil if association_id.blank?
- instance_variable_set(ivar_name, (association_model.find(association_id) rescue nil))
+ instance_variable_set(ivar_name, (reflection.klass.find(association_id) rescue nil))
end
end
define_method("#{method_name}=") do |obj|
instance_variable_set(ivar_name, obj)
attributes["#{method_name}_id"] = obj.try(:id)
end
end
- attr_accessor :embeds
- def defines_belongs_to_embed(method_name, association_model, foreign_key)
- self.embeds ||= []
- self.embeds << method_name.to_s
- end
-
- def defines_has_many_finder_method(method_name, association_model)
+ def defines_has_many_finder_method(reflection)
+ method_name = reflection.name
ivar_name = :"@#{method_name}"
define_method(method_name) do
if instance_variable_defined?(ivar_name)
instance_variable_get(ivar_name)
elsif attributes.include?(method_name)
attributes[method_name]
+ elsif !new_record?
+ instance_variable_set(ivar_name, reflection.klass.find(:all, params: { q: { :"#{self.class.element_name}_id" => self.id } }))
else
- instance_variable_set(ivar_name, association_model.find(:all, :params => { :q => {:"#{self.class.element_name}_id" => self.id} }))
+ instance_variable_set(ivar_name, self.class.collection_parser.new)
end
end
end
# Defines the has_one association
- def defines_has_one_finder_method(method_name, association_model)
+ def defines_has_one_finder_method(reflection)
+ method_name = reflection.name
ivar_name = :"@#{method_name}"
define_method(method_name) do
if instance_variable_defined?(ivar_name)
instance_variable_get(ivar_name)
elsif attributes.include?(method_name)
attributes[method_name]
+ elsif reflection.klass.respond_to?(:singleton_name)
+ instance_variable_set(ivar_name, reflection.klass.find(params: { q: { :"#{self.class.element_name}_id" => self.id } }))
else
- instance_variable_set(ivar_name, association_model.find(:params => { :q => { :"#{self.class.element_name}_id" => self.id} }))
+ instance_variable_set(ivar_name, reflection.klass.find(:one, from: "/#{self.class.collection_name}/#{self.id}/#{method_name}#{self.class.format_extension}"))
end
end
end
-
end