lib/jsonapi/resource.rb in jsonapi-resources-0.10.0.beta1 vs lib/jsonapi/resource.rb in jsonapi-resources-0.10.0.beta2
- old
+ new
@@ -398,10 +398,11 @@
subclass.abstract(false)
subclass.immutable(false)
subclass.caching(_caching)
subclass.paginator(_paginator)
subclass._attributes = (_attributes || {}).dup
+ subclass.polymorphic(false)
subclass._model_hints = (_model_hints || {}).dup
unless _model_name.empty? || _immutable
subclass.model_name(_model_name, add_model_hint: (_model_hints && !_model_hints[_model_name].nil?) == true)
@@ -437,57 +438,57 @@
# <relationship_name>: <ResourceIdentity of a source resource in find_included_fragments>
# }
# }
#
# begin ResourceFinder Abstract methods
- def find(_filters, _options = {})
- # :nocov:
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
- # :nocov:
- end
+ def find(_filters, _options = {})
+ # :nocov:
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
+ # :nocov:
+ end
- def count(_filters, _options = {})
- # :nocov:
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
- # :nocov:
- end
+ def count(_filters, _options = {})
+ # :nocov:
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
+ # :nocov:
+ end
- def find_by_keys(_keys, _options = {})
- # :nocov:
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
- # :nocov:
- end
+ def find_by_keys(_keys, _options = {})
+ # :nocov:
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
+ # :nocov:
+ end
- def find_by_key(_key, _options = {})
- # :nocov:
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
- # :nocov:
- end
+ def find_by_key(_key, _options = {})
+ # :nocov:
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
+ # :nocov:
+ end
- def find_fragments(_filters, _options = {})
- # :nocov:
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
- # :nocov:
- end
+ def find_fragments(_filters, _options = {})
+ # :nocov:
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
+ # :nocov:
+ end
- def find_included_fragments(_source_rids, _relationship_name, _options = {})
- # :nocov:
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
- # :nocov:
- end
+ def find_included_fragments(_source_rids, _relationship_name, _options = {})
+ # :nocov:
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
+ # :nocov:
+ end
- def find_related_fragments(_source_rids, _relationship_name, _options = {})
- # :nocov:
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
- # :nocov:
- end
+ def find_related_fragments(_source_rids, _relationship_name, _options = {})
+ # :nocov:
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
+ # :nocov:
+ end
- def count_related(_source_rid, _relationship_name, _options = {})
- # :nocov:
- raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
- # :nocov:
- end
+ def count_related(_source_rid, _relationship_name, _options = {})
+ # :nocov:
+ raise 'Abstract ResourceFinder method called. Ensure that a ResourceFinder has been set.'
+ # :nocov:
+ end
#end ResourceFinder Abstract methods
def rebuild_relationships(relationships)
original_relationships = relationships.deep_dup
@@ -708,18 +709,10 @@
def fields
_relationships.keys | _attributes.keys
end
- def records(options = {})
- _model_class.all
- end
-
- def retrieve_records(ids, options = {})
- _model_class.where(_primary_key => ids)
- end
-
def resources_for(records, context)
records.collect do |record|
resource_for(record, context)
end
end
@@ -848,26 +841,35 @@
def _updatable_relationships
@_relationships.map { |key, relationship| key unless relationship.readonly? }.compact
end
def _relationship(type)
+ return nil unless type
type = type.to_sym
@_relationships[type]
end
def _model_name
if _abstract
- return ''
+ ''
else
return @_model_name.to_s if defined?(@_model_name)
class_name = self.name
return '' if class_name.nil?
@_model_name = class_name.demodulize.sub(/Resource$/, '')
- return @_model_name.to_s
+ @_model_name.to_s
end
end
+ def _polymorphic_name
+ if !_polymorphic
+ ''
+ else
+ @_polymorphic_name ||= _model_name.to_s.downcase
+ end
+ end
+
def _primary_key
@_primary_key ||= _default_primary_key
end
def _default_primary_key
@@ -898,9 +900,37 @@
@_paginator ||= JSONAPI.configuration.default_paginator
end
def paginator(paginator)
@_paginator = paginator
+ end
+
+ def _polymorphic
+ @_polymorphic
+ end
+
+ def polymorphic(polymorphic = true)
+ @_polymorphic = polymorphic
+ end
+
+ def _polymorphic_types
+ @poly_hash ||= {}.tap do |hash|
+ ObjectSpace.each_object do |klass|
+ next unless Module === klass
+ if klass < ActiveRecord::Base
+ klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
+ (hash[reflection.options[:as]] ||= []) << klass.name.downcase
+ end
+ end
+ end
+ end
+ @poly_hash[_polymorphic_name.to_sym]
+ end
+
+ def _polymorphic_resource_klasses
+ @_polymorphic_resource_klasses ||= _polymorphic_types.collect do |type|
+ resource_klass_for(type)
+ end
end
def abstract(val = true)
@abstract = val
end