lib/jsonapi/resource.rb in jsonapi-resources-0.0.14 vs lib/jsonapi/resource.rb in jsonapi-resources-0.0.15
- old
+ new
@@ -396,10 +396,14 @@
def _allowed_filter?(filter)
_allowed_filters.include?(filter)
end
+ def module_path
+ @module_path ||= self.name =~ /::[^:]+\Z/ ? ($`.freeze.gsub('::', '/') + '/').downcase : ''
+ end
+
private
def check_reserved_resource_name(type, name)
if [:ids, :types, :hrefs, :links].include?(type)
warn "[NAME COLLISION] `#{name}` is a reserved resource name."
@@ -439,20 +443,20 @@
@model.method("#{foreign_key}=").call(value)
end unless method_defined?("#{foreign_key}=")
if @_associations[attr].is_a?(JSONAPI::Association::HasOne)
define_method attr do
- type_name = self.class._associations[attr].type
- resource_class = self.class.resource_for(type_name)
+ type_name = self.class._associations[attr].type.to_s
+ resource_class = self.class.resource_for(self.class.module_path + type_name)
if resource_class
associated_model = @model.send attr
return associated_model ? resource_class.new(associated_model, @context) : nil
end
end unless method_defined?(attr)
elsif @_associations[attr].is_a?(JSONAPI::Association::HasMany)
define_method attr do
- type_name = self.class._associations[attr].type
- resource_class = self.class.resource_for(type_name)
+ type_name = self.class._associations[attr].type.to_s
+ resource_class = self.class.resource_for(self.class.module_path + type_name)
resources = []
if resource_class
associated_models = @model.send attr
associated_models.each do |associated_model|
resources.push resource_class.new(associated_model, @context)