Class: ActiveModelSerializersPg::CollectionSerializer
- Inherits:
-
Object
- Object
- ActiveModelSerializersPg::CollectionSerializer
- Includes:
- Enumerable
- Defined in:
- lib/active_model_serializers_pg/collection_serializer.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#element_serializer ⇒ Object
PATCH: Give ourselves access to the serializer for the individual elements:.
-
#initialize(resources, options = {}) ⇒ CollectionSerializer
constructor
A new instance of CollectionSerializer.
-
#json_key ⇒ Object
TODO: unify naming of root, json_key, and _type.
-
#paginated? ⇒ Boolean
rubocop:enable Metrics/CyclomaticComplexity.
- #serializable_hash(adapter_options, options, adapter_instance) ⇒ Object private
- #success? ⇒ Boolean
Constructor Details
#initialize(resources, options = {}) ⇒ CollectionSerializer
Returns a new instance of CollectionSerializer.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/active_model_serializers_pg/collection_serializer.rb', line 22 def initialize(resources, = {}) @object = resources = @root = [:root] # PATCHED: We don't want to materialize a Relation by iterating unless we have to. # On the other hand, if we don't have a serializer we *do* want to `throw :no_serializer` # right away. That should only happen for basic types (like a String or Hash), # so we act lazy when we have a Relation, and eager otherwise: unless resources.is_a? ActiveRecord::Relation @serializers = serializers_from_resources end end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
20 21 22 |
# File 'lib/active_model_serializers_pg/collection_serializer.rb', line 20 def object @object end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
20 21 22 |
# File 'lib/active_model_serializers_pg/collection_serializer.rb', line 20 def root @root end |
Instance Method Details
#element_serializer ⇒ Object
PATCH: Give ourselves access to the serializer for the individual elements:
36 37 38 |
# File 'lib/active_model_serializers_pg/collection_serializer.rb', line 36 def element_serializer [:serializer] end |
#json_key ⇒ Object
TODO: unify naming of root, json_key, and _type. Right now, a serializer’s json_key comes from the root option or the object’s model name, by default. But, if a dev defines a custom ‘json_key` method with an explicit value, we have no simple way to know that it is safe to call that instance method. (which is really a class property at this point, anyhow). rubocop:disable Metrics/CyclomaticComplexity Disabling cop since it’s good to highlight the complexity of this method by including all the logic right here.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_model_serializers_pg/collection_serializer.rb', line 61 def json_key return root if root # 1. get from options[:serializer] for empty resource collection key = object.empty? && (explicit_serializer_class = [:serializer]) && explicit_serializer_class._type # 2. get from first serializer instance in collection key ||= (serializer = serializers.first) && serializer.json_key # 3. get from collection name, if a named collection key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil # 4. key may be nil for empty collection and no serializer option key &&= key.pluralize # 5. fail if the key cannot be determined key || fail(ArgumentError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String') end |
#paginated? ⇒ Boolean
rubocop:enable Metrics/CyclomaticComplexity
78 79 80 81 82 83 |
# File 'lib/active_model_serializers_pg/collection_serializer.rb', line 78 def paginated? ActiveModelSerializers.config.jsonapi_pagination_links_enabled && object.respond_to?(:current_page) && object.respond_to?(:total_pages) && object.respond_to?(:size) end |
#serializable_hash(adapter_options, options, adapter_instance) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
45 46 47 48 49 50 51 |
# File 'lib/active_model_serializers_pg/collection_serializer.rb', line 45 def serializable_hash(, , adapter_instance) [:include_directive] ||= ActiveModel::Serializer.() [:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, [:include_directive]) serializers.map do |serializer| serializer.serializable_hash(, , adapter_instance) end end |
#success? ⇒ Boolean
40 41 42 |
# File 'lib/active_model_serializers_pg/collection_serializer.rb', line 40 def success? true end |