Class: Serializer::Collection
- Inherits:
-
Object
- Object
- Serializer::Collection
- Defined in:
- app/lib/serializer/collection.rb
Instance Attribute Summary collapse
-
#compound_opts ⇒ Object
Returns the value of attribute compound_opts.
-
#current_controller ⇒ Object
Returns the value of attribute current_controller.
-
#format ⇒ Object
Returns the value of attribute format.
-
#includes ⇒ Object
Returns the value of attribute includes.
-
#records ⇒ Object
Returns the value of attribute records.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(records, includes: [], compound_opts: {}, **params) ⇒ Collection
constructor
A new instance of Collection.
- #relation_includes ⇒ Object
- #serializer_klass ⇒ Object
Constructor Details
#initialize(records, includes: [], compound_opts: {}, **params) ⇒ Collection
Returns a new instance of Collection.
4 5 6 7 8 9 10 11 12 |
# File 'app/lib/serializer/collection.rb', line 4 def initialize(records, includes: [], compound_opts: {}, **params) @records = ::Serializer::Pagination.run(records: records, page: params[:page], per_page: params[:per_page], **params).result @records = ::Serializer::Sort.run(records: @records, sort: params[:sort], **params).result @includes = includes @compound_opts = compound_opts @format = params[:format] @current_controller = params[:current_controller] end |
Instance Attribute Details
#compound_opts ⇒ Object
Returns the value of attribute compound_opts.
2 3 4 |
# File 'app/lib/serializer/collection.rb', line 2 def compound_opts @compound_opts end |
#current_controller ⇒ Object
Returns the value of attribute current_controller.
2 3 4 |
# File 'app/lib/serializer/collection.rb', line 2 def current_controller @current_controller end |
#format ⇒ Object
Returns the value of attribute format.
2 3 4 |
# File 'app/lib/serializer/collection.rb', line 2 def format @format end |
#includes ⇒ Object
Returns the value of attribute includes.
2 3 4 |
# File 'app/lib/serializer/collection.rb', line 2 def includes @includes end |
#records ⇒ Object
Returns the value of attribute records.
2 3 4 |
# File 'app/lib/serializer/collection.rb', line 2 def records @records end |
Instance Method Details
#as_json ⇒ Object
14 15 16 |
# File 'app/lib/serializer/collection.rb', line 14 def as_json serializer_klass.new(records, include: relation_includes, format: format, current_controller: current_controller, **compound_opts).serializable_hash end |
#relation_includes ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/lib/serializer/collection.rb', line 25 def relation_includes relations = serializer_klass.default_includes @includes = if includes.is_a? String includes.split(",").map(&:underscore).map(&:to_sym) else Array(includes).map(&:to_sym) end relations.concat(includes.compact) end |
#serializer_klass ⇒ Object
18 19 20 21 22 23 |
# File 'app/lib/serializer/collection.rb', line 18 def serializer_klass resource_klass = records.klass if records.is_a? ActiveRecord::Relation resource_klass = records.first.class if records.is_a? Array "#{resource_klass.name}Serializer".constantize end |