Skip to content

Commit facfe3b

Browse files
committed
goodbye const_get mess
1 parent 6b84cf0 commit facfe3b

File tree

15 files changed

+390
-420
lines changed

15 files changed

+390
-420
lines changed

lib/zendesk_api/association.rb

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def generate_path(*args)
3535
has_parent = namespace.size > 1 || (options[:with_parent] && @options.parent)
3636

3737
if has_parent
38-
parent_class = @options.parent ? @options.parent.class : ZendeskAPI.get_class(namespace[0])
38+
parent_class = @options.parent ? @options.parent.class : ZendeskAPI.const_get(ZendeskAPI::Helpers.modulize_string(namespace[0]))
3939
parent_namespace = build_parent_namespace(parent_class, instance, options, original_options)
4040
namespace[1..1] = parent_namespace if parent_namespace
4141
namespace[0] = parent_class.resource_name
@@ -176,8 +176,13 @@ def associated_with(name)
176176
# Represents a parent-to-child association between resources. Options to pass in are: class, path.
177177
# @param [Symbol] resource_name The underlying resource name
178178
# @param [Hash] opts The options to pass to the method definition.
179-
def has(resource_name, class_level_options = {})
180-
klass = get_class(class_level_options.delete(:class)) || get_class(resource_name)
179+
def has(resource_name_or_class, class_level_options = {})
180+
if klass = class_level_options.delete(:class)
181+
resource_name = resource_name_or_class
182+
else
183+
klass = resource_name_or_class
184+
resource_name = klass.singular_resource_name
185+
end
181186

182187
class_level_association = {
183188
:class => klass,
@@ -231,8 +236,13 @@ def has(resource_name, class_level_options = {})
231236
# Represents a parent-to-children association between resources. Options to pass in are: class, path.
232237
# @param [Symbol] resource The underlying resource name
233238
# @param [Hash] opts The options to pass to the method definition.
234-
def has_many(resource_name, class_level_opts = {})
235-
klass = get_class(class_level_opts.delete(:class)) || get_class(Inflection.singular(resource_name.to_s))
239+
def has_many(resource_name_or_class, class_level_opts = {})
240+
if klass = class_level_opts.delete(:class)
241+
resource_name = resource_name_or_class
242+
else
243+
klass = resource_name_or_class
244+
resource_name = klass.resource_name
245+
end
236246

237247
class_level_association = {
238248
:class => klass,
@@ -292,47 +302,6 @@ def has_many(resource_name, class_level_opts = {})
292302
resource
293303
end
294304
end
295-
296-
# Allows using has and has_many without having class defined yet
297-
# Guesses at Resource, if it's anything else and the class is later
298-
# reopened under a different superclass, an error will be thrown
299-
def get_class(resource)
300-
return false if resource.nil?
301-
res = ZendeskAPI::Helpers.modulize_string(resource.to_s)
302-
303-
begin
304-
const_get(res)
305-
rescue NameError, ArgumentError # ruby raises NameError, rails raises ArgumentError
306-
ZendeskAPI.get_class(resource)
307-
end
308-
end
309-
end
310-
end
311-
312-
class << self
313-
# Make sure Rails' overwriting of const_missing doesn't cause trouble
314-
def const_missing(*args)
315-
Object.const_missing(*args)
316-
end
317-
318-
# Allows using has and has_many without having class defined yet
319-
# Guesses at Resource, if it's anything else and the class is later
320-
# reopened under a different superclass, an error will be thrown
321-
def get_class(resource)
322-
return false if resource.nil?
323-
res = ZendeskAPI::Helpers.modulize_string(resource.to_s).split("::")
324-
325-
begin
326-
res[1..-1].inject(ZendeskAPI.const_get(res[0])) do |iter, k|
327-
begin
328-
iter.const_get(k)
329-
rescue
330-
iter.const_set(k, Class.new(Resource))
331-
end
332-
end
333-
rescue NameError
334-
ZendeskAPI.const_set(res[0], Class.new(Resource))
335-
end
336305
end
337306
end
338307
end

lib/zendesk_api/client.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def method_missing(method, *args, &block)
3232
method = method.to_s
3333
options = args.last.is_a?(Hash) ? args.pop : {}
3434
return instance_variable_get("@#{method}") if !options.delete(:reload) && instance_variable_defined?("@#{method}")
35-
instance_variable_set("@#{method}", ZendeskAPI::Collection.new(self, ZendeskAPI.get_class(Inflection.singular(method)), options))
35+
instance_variable_set("@#{method}", ZendeskAPI::Collection.new(self, ZendeskAPI.const_get(ZendeskAPI::Helpers.modulize_string(Inflection.singular(method))), options))
3636
end
3737

3838
# Returns the current user (aka me)
@@ -111,7 +111,9 @@ def insert_callback(&block)
111111

112112
# show a nice warning for people using the old style api
113113
def self.check_deprecated_namespace_usage(attributes, name)
114-
raise "un-nest '#{name}' from the attributes" if attributes[name].is_a?(Hash)
114+
if attributes[name].is_a?(Hash)
115+
raise "un-nest '#{name}' from the attributes"
116+
end
115117
end
116118

117119
protected

lib/zendesk_api/collection.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
require 'zendesk_api/resource'
2-
require 'zendesk_api/resources/misc'
3-
require 'zendesk_api/resources/ticket'
4-
require 'zendesk_api/resources/user'
2+
require 'zendesk_api/resources'
53

64
module ZendeskAPI
75
# Represents a collection of resources. Lazily loaded, resources aren't

lib/zendesk_api/resource.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ def initialize(client, attributes = {})
5050
@association = attributes.delete(:association) || Association.new(:class => self.class)
5151
@client = client
5252
@attributes = ZendeskAPI::Trackie.new(attributes)
53-
ZendeskAPI::Client.check_deprecated_namespace_usage @attributes, self.class.singular_resource_name
53+
54+
if self.class.associations.none? {|a| a[:name] == self.class.singular_resource_name}
55+
ZendeskAPI::Client.check_deprecated_namespace_usage @attributes, self.class.singular_resource_name
56+
end
5457

5558
@attributes.clear_changes unless new_record?
5659
end

0 commit comments

Comments
 (0)