Class: ActiveModelSerializers::Adapter::JsonApi::ResourceIdentifier
- Inherits:
-
Object
- Object
- ActiveModelSerializers::Adapter::JsonApi::ResourceIdentifier
- Defined in:
- lib/active_model_serializers/adapter/json_api/resource_identifier.rb
Class Method Summary collapse
- .for_type_with_id(type, id, options) ⇒ Object
- .inflect_type(type) ⇒ Object
- .raw_type_from_serializer_object(object) ⇒ Object
- .type_for(serializer, serializer_type = nil, transform_options = {}) ⇒ Object
Instance Method Summary collapse
Constructor Details
#initialize(serializer, options) ⇒ ResourceIdentifier
38 39 40 41 |
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 38 def initialize(serializer, ) @id = id_for(serializer) @type = type_for(serializer, ) end |
Class Method Details
.for_type_with_id(type, id, options) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 12 def self.for_type_with_id(type, id, ) type = inflect_type(type) type = type_for(:no_class_needed, type, ) if id.blank? nil else { id: id.to_s, type: type } end end |
.inflect_type(type) ⇒ Object
31 32 33 34 35 |
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 31 def self.inflect_type(type) singularize = ActiveModelSerializers.config.jsonapi_resource_type == :singular inflection = singularize ? :singularize : :pluralize ActiveSupport::Inflector.public_send(inflection, type) end |
.raw_type_from_serializer_object(object) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 22 def self.raw_type_from_serializer_object(object) class_name = object.class.name # should use model_name raw_type = class_name.underscore raw_type = inflect_type(raw_type) raw_type .gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator) raw_type end |
.type_for(serializer, serializer_type = nil, transform_options = {}) ⇒ Object
7 8 9 10 |
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 7 def self.type_for(serializer, serializer_type = nil, = {}) raw_type = serializer_type ? serializer_type : raw_type_from_serializer_object(serializer.object) JsonApi.send(:transform_key_casing!, raw_type, ) end |
Instance Method Details
#as_json ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/active_model_serializers/adapter/json_api/resource_identifier.rb', line 43 def as_json if id.blank? { type: type } else { id: id.to_s, type: type } end end |