Module: ZendeskAPI::Associations::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#associated_with(name) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/zendesk_api/association.rb', line 166

def associated_with(name)
  associations.inject([]) do |associated_with, association|
    if association[:include] == name.to_s
      associated_with.push(Association.new(association))
    end

    associated_with
  end
end

#associationsObject



162
163
164
# File 'lib/zendesk_api/association.rb', line 162

def associations
  @associations ||= []
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



299
300
301
302
303
304
305
306
307
308
# File 'lib/zendesk_api/association.rb', line 299

def get_class(resource)
  return false if resource.nil?
  res = ZendeskAPI::Helpers.modulize_string(resource.to_s)

  begin
    const_get(res)
  rescue NameError, ArgumentError # ruby raises NameError, rails raises ArgumentError
    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.



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
224
225
226
227
228
229
# File 'lib/zendesk_api/association.rb', line 179

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),
    :include => (class_level_options.delete(:include) || klass.resource_name).to_s,
    :include_key => (class_level_options.delete(:include_key) || :id).to_s,
    :singular => true
  }

  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.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/zendesk_api/association.rb', line 234

def has_many(resource_name, class_level_opts = {})
  klass = get_class(class_level_opts.delete(:class)) || get_class(Inflection.singular(resource_name.to_s))

  class_level_association = {
    :class => klass,
    :name => resource_name,
    :inline => class_level_opts.delete(:inline),
    :path => class_level_opts.delete(:path),
    :include => (class_level_opts.delete(:include) || klass.resource_name).to_s,
    :include_key => (class_level_opts.delete(:include_key) || :id).to_s,
    :singular => false
  }

  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 = Inflection.singular(resource_name.to_s)

    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