Module: ZendeskAPI::Associations::ClassMethods

Includes:
Rescue
Defined in:
lib/zendesk_api/association.rb

Instance Method Summary collapse

Instance Method Details

#associationsObject



112
113
114
# File 'lib/zendesk_api/association.rb', line 112

def associations
  @assocations ||= []
end

#get_class(resource) ⇒ Object

Allows using has and has_many without having class defined yet Guesses at Resource, if it’s anything else and the class is later reopened under a different superclass, an error will be thrown



228
229
230
231
232
233
234
235
236
237
# File 'lib/zendesk_api/association.rb', line 228

def get_class(resource)
  return false if resource.nil?
  res = resource.to_s.modulize

  begin
    const_get(res)
  rescue NameError
    ZendeskAPI.get_class(resource)
  end
end

#has(resource_name, class_level_options = {}) ⇒ Object

Represents a parent-to-child association between resources. Options to pass in are: class, path.

Parameters:

  • resource_name (Symbol)

    The underlying resource name

  • opts (Hash)

    The options to pass to the method definition.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/zendesk_api/association.rb', line 119

def has(resource_name, class_level_options = {})
  klass = get_class(class_level_options.delete(:class)) || get_class(resource_name)
  class_level_association = {
    :class => klass,
    :name => resource_name,
    :inline => class_level_options.delete(:inline),
    :path => class_level_options.delete(:path)
  }
  associations << class_level_association
  id_column = "#{resource_name}_id"

  define_method "#{resource_name}_used?" do
    !!instance_variable_get("@#{resource_name}")
  end

  define_method resource_name do |*args|
    instance_options = args.last.is_a?(Hash) ? args.pop : {}

    # return if cached
    cached = instance_variable_get("@#{resource_name}")
    return cached if cached && !instance_options[:reload]

    # find and cache association
    instance_association = Association.new(class_level_association.merge(:parent => self))
    resource = if klass.respond_to?(:find) && resource_id = method_missing(id_column)
      klass.find(@client, :id => resource_id, :association => instance_association)
    elsif found = method_missing(resource_name.to_sym)
      wrap_resource(found, klass, class_level_association)
    elsif klass.ancestors.include?(DataResource)
      rescue_client_error do
        response = @client.connection.get(instance_association.generate_path(:with_parent => true))
        klass.new(@client, response.body[klass.singular_resource_name].merge(:association => instance_association))
      end
    end

    send("#{id_column}=", resource.id) if resource && has_key?(id_column)
    instance_variable_set("@#{resource_name}", resource)
  end

  define_method "#{resource_name}=" do |resource|
    resource = wrap_resource(resource, klass, class_level_association)
    send("#{id_column}=", resource.id) if has_key?(id_column)
    instance_variable_set("@#{resource_name}", resource)
  end
end

#has_many(resource_name, class_level_opts = {}) ⇒ Object

Represents a parent-to-children association between resources. Options to pass in are: class, path.

Parameters:

  • resource (Symbol)

    The underlying resource name

  • opts (Hash)

    The options to pass to the method definition.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/zendesk_api/association.rb', line 168

def has_many(resource_name, class_level_opts = {})
  klass = get_class(class_level_opts.delete(:class)) || get_class(resource_name.to_s.singular)
  class_level_association = {
    :class => klass,
    :name => resource_name,
    :inline => class_level_opts.delete(:inline),
    :path => class_level_opts.delete(:path)
  }
  associations << class_level_association

  id_column = "#{resource_name}_ids"

  define_method "#{resource_name}_used?" do
    !!instance_variable_get("@#{resource_name}")
  end

  define_method resource_name do |*args|
    instance_opts = args.last.is_a?(Hash) ? args.pop : {}

    # return if cached
    cached = instance_variable_get("@#{resource_name}")
    return cached if cached && !instance_opts[:reload]

    # find and cache association
    instance_association = Association.new(class_level_association.merge(:parent => self))
    singular_resource_name = resource_name.to_s.singular

    resources = if (ids = method_missing("#{singular_resource_name}_ids")) && ids.any?
      ids.map do |id|
        klass.find(@client, :id => id, :association => instance_association)
      end.compact
    elsif (resources = method_missing(resource_name.to_sym)) && resources.any?
      resources.map do |res|
        klass.new(@client, res.merge(:association => instance_association))
      end
    else
      ZendeskAPI::Collection.new(@client, klass, instance_opts.merge(:association => instance_association))
    end

    send("#{id_column}=", resources.map(&:id)) if resource && has_key?(id_column)
    instance_variable_set("@#{resource_name}", resources)
  end

  define_method "#{resource_name}=" do |resources|
    if resources.is_a?(Array)
      resources.map! { |attr| wrap_resource(attr, klass, class_level_association) }
      send(resource_name).replace(resources)
    else
      resources.association = instance_association
      instance_variable_set("@#{resource_name}", resources)
    end

    send("#{id_column}=", resources.map(&:id)) if resources && has_key?(id_column)
    resource
  end
end