Class: Restful::Association

Inherits:
Object
  • Object
show all
Includes:
ActionController::UrlWriter, UrlForHelpers
Defined in:
lib/restful/serializer.rb

Overview

Handle for information about an ActiveRecord association.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UrlForHelpers

#get_url_for

Constructor Details

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

Returns a new instance of Association.



140
141
142
143
144
145
146
# File 'lib/restful/serializer.rb', line 140

def initialize(subject, subject_klass, association_name, name = nil)
  self.subject = subject
  self.subject_klass = subject_klass
  self.association_name = association_name
  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.



138
139
140
# File 'lib/restful/serializer.rb', line 138

def association
  @association
end

#association_nameObject

Returns the value of attribute association_name.



138
139
140
# File 'lib/restful/serializer.rb', line 138

def association_name
  @association_name
end

#nameObject

Returns the value of attribute name.



138
139
140
# File 'lib/restful/serializer.rb', line 138

def name
  @name
end

#subjectObject

Returns the value of attribute subject.



138
139
140
# File 'lib/restful/serializer.rb', line 138

def subject
  @subject
end

#subject_klassObject

Returns the value of attribute subject_klass.



138
139
140
# File 'lib/restful/serializer.rb', line 138

def subject_klass
  @subject_klass
end

Instance Method Details

#collective_hrefObject



161
162
163
164
165
166
167
# File 'lib/restful/serializer.rb', line 161

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

#hrefObject



152
153
154
155
156
157
158
159
# File 'lib/restful/serializer.rb', line 152

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

#singular?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/restful/serializer.rb', line 148

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