Class: JSONAPI::ShowRelatedResourcesOperation
- Defined in:
- lib/jsonapi/operation.rb
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#paginator ⇒ Object
readonly
Returns the value of attribute paginator.
-
#relationship_type ⇒ Object
readonly
Returns the value of attribute relationship_type.
-
#sort_criteria ⇒ Object
readonly
Returns the value of attribute sort_criteria.
-
#source_id ⇒ Object
readonly
Returns the value of attribute source_id.
-
#source_klass ⇒ Object
readonly
Returns the value of attribute source_klass.
Attributes inherited from Operation
#resource_klass, #transactional
Instance Method Summary collapse
- #apply ⇒ Object
-
#initialize(resource_klass, options = {}) ⇒ ShowRelatedResourcesOperation
constructor
A new instance of ShowRelatedResourcesOperation.
- #options ⇒ Object
- #pagination_params ⇒ Object
- #record_count ⇒ Object
- #records ⇒ Object
- #source_resource ⇒ Object
Constructor Details
#initialize(resource_klass, options = {}) ⇒ ShowRelatedResourcesOperation
Returns a new instance of ShowRelatedResourcesOperation.
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/jsonapi/operation.rb', line 141 def initialize(resource_klass, = {}) super(resource_klass, ) @source_klass = .fetch(:source_klass) @source_id = .fetch(:source_id) @relationship_type = .fetch(:relationship_type) @filters = [:filters] @sort_criteria = [:sort_criteria] @paginator = [:paginator] @transactional = false end |
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
139 140 141 |
# File 'lib/jsonapi/operation.rb', line 139 def filters @filters end |
#paginator ⇒ Object (readonly)
Returns the value of attribute paginator.
139 140 141 |
# File 'lib/jsonapi/operation.rb', line 139 def paginator @paginator end |
#relationship_type ⇒ Object (readonly)
Returns the value of attribute relationship_type.
139 140 141 |
# File 'lib/jsonapi/operation.rb', line 139 def relationship_type @relationship_type end |
#sort_criteria ⇒ Object (readonly)
Returns the value of attribute sort_criteria.
139 140 141 |
# File 'lib/jsonapi/operation.rb', line 139 def sort_criteria @sort_criteria end |
#source_id ⇒ Object (readonly)
Returns the value of attribute source_id.
139 140 141 |
# File 'lib/jsonapi/operation.rb', line 139 def source_id @source_id end |
#source_klass ⇒ Object (readonly)
Returns the value of attribute source_klass.
139 140 141 |
# File 'lib/jsonapi/operation.rb', line 139 def source_klass @source_klass end |
Instance Method Details
#apply ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/jsonapi/operation.rb', line 182 def apply = source_resource.public_send(@relationship_type, filters: @filters, sort_criteria: @sort_criteria, paginator: @paginator) return JSONAPI::RelatedResourcesOperationResult.new(:ok, source_resource, @relationship_type, , ) rescue JSONAPI::Exceptions::Error => e return JSONAPI::ErrorsOperationResult.new(e.errors[0].code, e.errors) end |
#options ⇒ Object
175 176 177 178 179 180 |
# File 'lib/jsonapi/operation.rb', line 175 def opts = {} opts.merge!(pagination_params: pagination_params) if JSONAPI.configuration.top_level_links_include_pagination opts.merge!(record_count: record_count) if JSONAPI.configuration. opts end |
#pagination_params ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/jsonapi/operation.rb', line 165 def pagination_params if @paginator && JSONAPI.configuration.top_level_links_include_pagination = {} [:record_count] = record_count if @paginator.class.requires_record_count @paginator.links_page_params() else {} end end |
#record_count ⇒ Object
152 153 154 |
# File 'lib/jsonapi/operation.rb', line 152 def record_count @_record_count ||= records.count(:all) end |
#records ⇒ Object
160 161 162 163 |
# File 'lib/jsonapi/operation.rb', line 160 def records = source_resource.public_send("records_for_" + @relationship_type) @resource_klass.filter_records(@filters, @options, ) end |
#source_resource ⇒ Object
156 157 158 |
# File 'lib/jsonapi/operation.rb', line 156 def source_resource @_source_resource ||= @source_klass.find_by_key(@source_id, context: @context) end |