Class: ApiPresenter::Base
- Inherits:
-
Object
- Object
- ApiPresenter::Base
- Defined in:
- lib/api_presenter/base.rb
Direct Known Subclasses
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.
28 29 30 31 32 |
# File 'lib/api_presenter/base.rb', line 28 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.
4 5 6 |
# File 'lib/api_presenter/base.rb', line 4 def current_user @current_user end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
4 5 6 |
# File 'lib/api_presenter/base.rb', line 4 def params @params end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
4 5 6 |
# File 'lib/api_presenter/base.rb', line 4 def relation @relation end |
Class Method Details
.call(**kwargs) ⇒ ApiPresenter::Base
17 18 19 |
# File 'lib/api_presenter/base.rb', line 17 def self.call(**kwargs) new(kwargs).call end |
Instance Method Details
#associations_map ⇒ Hash
Hash map that defines the sources for included collection names
128 129 130 |
# File 'lib/api_presenter/base.rb', line 128 def associations_map {} end |
#call ⇒ ApiPresenter::Base
36 37 38 39 40 41 |
# File 'lib/api_presenter/base.rb', line 36 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
47 48 49 |
# File 'lib/api_presenter/base.rb', line 47 def collection count_only? ? [] : relation end |
#included_collection_names ⇒ Array<Symbol>
Class names of included collections
82 83 84 |
# File 'lib/api_presenter/base.rb', line 82 def included_collection_names @included_collection_names ||= Parsers::ParseIncludeParams.call(params[ApiPresenter.configuration.include_param]) end |
#included_collections ⇒ Hash
Map of included collection names and loaded record
97 98 99 |
# File 'lib/api_presenter/base.rb', line 97 def included_collections @included_collections_resolver ? @included_collections_resolver.resolved_collections : {} end |
#policies ⇒ <Array<Hash>]
Policies for the primary collection
71 72 73 |
# File 'lib/api_presenter/base.rb', line 71 def policies @policies_resolver ? @policies_resolver.resolved_policies : {} end |
#policy_associations ⇒ Symbol+
Policy associations to preload to optimize policy resolution
168 169 170 |
# File 'lib/api_presenter/base.rb', line 168 def policy_associations [] end |
#policy_methods ⇒ Symbol+
Policy methods to resolve for the primary relation
148 149 150 |
# File 'lib/api_presenter/base.rb', line 148 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
109 110 111 |
# File 'lib/api_presenter/base.rb', line 109 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
57 58 59 |
# File 'lib/api_presenter/base.rb', line 57 def total_count relation.respond_to?(:total_count) ? relation.total_count : relation.count end |