Class: Restful::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/restful/serializer.rb

Overview

Handle for information about an ActiveRecord association.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, subject_klass, association_name, url_factory, name = nil) ⇒ Association

Returns a new instance of Association.



264
265
266
267
268
269
270
271
# File 'lib/restful/serializer.rb', line 264

def initialize(subject, subject_klass, association_name, url_factory, name = nil)
  self.subject = subject
  self.subject_klass = subject_klass
  self.association_name = association_name
  self.url_factory = url_factory
  self.name = name || association_name
  self.association = subject.class.reflect_on_association(association_name)
end

Instance Attribute Details

#associationObject

Returns the value of attribute association.



262
263
264
# File 'lib/restful/serializer.rb', line 262

def association
  @association
end

#association_nameObject

Returns the value of attribute association_name.



262
263
264
# File 'lib/restful/serializer.rb', line 262

def association_name
  @association_name
end

#nameObject

Returns the value of attribute name.



262
263
264
# File 'lib/restful/serializer.rb', line 262

def name
  @name
end

#subjectObject

Returns the value of attribute subject.



262
263
264
# File 'lib/restful/serializer.rb', line 262

def subject
  @subject
end

#subject_klassObject

Returns the value of attribute subject_klass.



262
263
264
# File 'lib/restful/serializer.rb', line 262

def subject_klass
  @subject_klass
end

#url_factoryObject

Returns the value of attribute url_factory.



262
263
264
# File 'lib/restful/serializer.rb', line 262

def url_factory
  @url_factory
end

Instance Method Details

#collective_hrefObject



286
287
288
289
290
291
292
# File 'lib/restful/serializer.rb', line 286

def collective_href
  # try url_for nested resources first
  unless href = url_factory.create(:resources => [subject_klass, association_name], :args => subject.id)
    href = url_factory.create(:resources => association_name)
  end
  return href 
end

#hrefObject



277
278
279
280
281
282
283
284
# File 'lib/restful/serializer.rb', line 277

def href
  if singular?
    href = url_factory.create(:resources => association_name, :args => subject.send(association.name).id)
  else
    href = collective_href
  end
  return href
end

#singular?Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/restful/serializer.rb', line 273

def singular?
  [:belongs_to, :has_one].include?(association.macro)
end