Class: ApiPresenter::Base
- Inherits:
-
Object
- Object
- ApiPresenter::Base
- Defined in:
- lib/api_presenter.rb
Instance Attribute Summary collapse
-
#current_user ⇒ Object
readonly
Returns the value of attribute current_user.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Class Method Summary collapse
Instance Method Summary collapse
-
#associations_map ⇒ Hash
abstract
Hash map that defines the sources for included collection names.
- #call ⇒ ApiPresenter::Base
-
#collection ⇒ ActiveRecord::Relation, Array<ActiveRecord::Base>
Primary collection, empty if count requested.
-
#included_collection_names ⇒ Array<Symbol>
Class names of included collections.
-
#included_collections ⇒ Hash
Map of included collection names and loaded record.
-
#initialize(current_user: nil, relation:, params: {}) ⇒ Base
constructor
A new instance of Base.
-
#policies ⇒ <Array<Hash>]
Policies for the primary collection.
-
#policy_associations ⇒ Symbol+
abstract
Policy associations to preload to optimize policy resolution.
-
#policy_methods ⇒ Symbol+
abstract
Policy methods to resolve for the primary relation.
-
#preload(associations) ⇒ Object
Preload additional records with the relation.
-
#total_count ⇒ Integer
Count of primary collection.
Constructor Details
#initialize(current_user: nil, relation:, params: {}) ⇒ Base
Returns a new instance of Base.
36 37 38 39 40 |
# File 'lib/api_presenter.rb', line 36 def initialize(current_user: nil, relation:, params: {}) @current_user = current_user @relation = relation @params = params end |
Instance Attribute Details
#current_user ⇒ Object (readonly)
Returns the value of attribute current_user.
12 13 14 |
# File 'lib/api_presenter.rb', line 12 def current_user @current_user end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
12 13 14 |
# File 'lib/api_presenter.rb', line 12 def params @params end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
12 13 14 |
# File 'lib/api_presenter.rb', line 12 def relation @relation end |
Class Method Details
.call(**kwargs) ⇒ ApiPresenter::Base
25 26 27 |
# File 'lib/api_presenter.rb', line 25 def self.call(**kwargs) new(kwargs).call end |
Instance Method Details
#associations_map ⇒ Hash
Hash map that defines the sources for included collection names
136 137 138 |
# File 'lib/api_presenter.rb', line 136 def associations_map {} end |
#call ⇒ ApiPresenter::Base
44 45 46 47 48 49 |
# File 'lib/api_presenter.rb', line 44 def call return self if count_only? initialize_resolvers call_resolvers self end |
#collection ⇒ ActiveRecord::Relation, Array<ActiveRecord::Base>
Primary collection, empty if count requested
55 56 57 |
# File 'lib/api_presenter.rb', line 55 def collection count_only? ? [] : relation end |
#included_collection_names ⇒ Array<Symbol>
Class names of included collections
90 91 92 |
# File 'lib/api_presenter.rb', line 90 def included_collection_names @included_collection_names ||= ParseIncludeParams.call(params[:include]) end |
#included_collections ⇒ Hash
Map of included collection names and loaded record
105 106 107 |
# File 'lib/api_presenter.rb', line 105 def included_collections @included_collections_resolver ? @included_collections_resolver.resolved_collections : {} end |
#policies ⇒ <Array<Hash>]
Policies for the primary collection
79 80 81 |
# File 'lib/api_presenter.rb', line 79 def policies @policies_resolver ? @policies_resolver.resolved_policies : {} end |
#policy_associations ⇒ Symbol+
Policy associations to preload to optimize policy resolution
176 177 178 |
# File 'lib/api_presenter.rb', line 176 def policy_associations [] end |
#policy_methods ⇒ Symbol+
Policy methods to resolve for the primary relation
156 157 158 |
# File 'lib/api_presenter.rb', line 156 def policy_methods [] end |
#preload(associations) ⇒ Object
Called by resolvers, but can also be called if additional data is required that does not need to be loaded as an included collection, and for some reason cannot be chained onto the original relation.
Preload additional records with the relation
117 118 119 |
# File 'lib/api_presenter.rb', line 117 def preload(associations) @relation = @relation.preload(associations) end |
#total_count ⇒ Integer
Delegate to Kaminari’s ‘total_count` property, or regular count if not a paginated relation
Count of primary collection
65 66 67 |
# File 'lib/api_presenter.rb', line 65 def total_count relation.respond_to?(:total_count) ? relation.total_count : relation.count end |