Class: ActiveModel::Serializer::CollectionSerializer
- Inherits:
-
Object
- Object
- ActiveModel::Serializer::CollectionSerializer
- Includes:
- Enumerable
- Defined in:
- lib/active_model/serializer/collection_serializer.rb
Direct Known Subclasses
Constant Summary
- NoSerializerError =
Class.new(StandardError)
Instance Attribute Summary (collapse)
-
- (Object) object
readonly
Returns the value of attribute object.
-
- (Object) root
readonly
Returns the value of attribute root.
Instance Method Summary (collapse)
-
- (CollectionSerializer) initialize(resources, options = {})
constructor
A new instance of CollectionSerializer.
-
- (Object) json_key
TODO: unify naming of root, json_key, and _type.
-
- (Boolean) paginated?
rubocop:enable Metrics/CyclomaticComplexity.
- - (Boolean) success?
Constructor Details
- (CollectionSerializer) initialize(resources, options = {})
Returns a new instance of CollectionSerializer
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/active_model/serializer/collection_serializer.rb', line 10 def initialize(resources, = {}) @object = resources @options = @root = [:root] serializer_context_class = .fetch(:serializer_context_class, ActiveModel::Serializer) @serializers = resources.map do |resource| serializer_class = .fetch(:serializer) { serializer_context_class.serializer_for(resource) } if serializer_class.nil? # rubocop:disable Style/GuardClause fail NoSerializerError, "No serializer found for resource: #{resource.inspect}" else serializer_class.new(resource, .except(:serializer)) end end end |
Instance Attribute Details
- (Object) object (readonly)
Returns the value of attribute object
8 9 10 |
# File 'lib/active_model/serializer/collection_serializer.rb', line 8 def object @object end |
- (Object) root (readonly)
Returns the value of attribute root
8 9 10 |
# File 'lib/active_model/serializer/collection_serializer.rb', line 8 def root @root end |
Instance Method Details
- (Object) json_key
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.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_model/serializer/collection_serializer.rb', line 38 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 end |
- (Boolean) paginated?
rubocop:enable Metrics/CyclomaticComplexity
53 54 55 56 57 |
# File 'lib/active_model/serializer/collection_serializer.rb', line 53 def paginated? object.respond_to?(:current_page) && object.respond_to?(:total_pages) && object.respond_to?(:size) end |
- (Boolean) success?
26 27 28 |
# File 'lib/active_model/serializer/collection_serializer.rb', line 26 def success? true end |