Module: FacetRenderer
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/generators/propel_facets/templates/controllers/concerns/facet_renderer.rb
Overview
Provide means to connect a controller to a JSON facetable model.
Usage:
class MyController < ApiController
connect_facet :short, actions: [:index]
connect_facet :full, actions: [:show, :update, :create]
end
Instance Method Summary collapse
-
#resource_json(resource) ⇒ Object
To be used by controller to return params with visible and included fields.
-
#set_attachments(result, resource) ⇒ Object
set attachments to result json.
Instance Method Details
#resource_json(resource) ⇒ Object
To be used by controller to return params with visible and included fields
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/generators/propel_facets/templates/controllers/concerns/facet_renderer.rb', line 45 def resource_json(resource) facet = self.class.action_facet(action_name.to_sym) result = resource.as_json( facet: [facet, :index, :default], missing: :noaction ) # get attachment url in single resource if !resource.respond_to?('length') && resource.class.respond_to?('attachment_reflections') (result, resource) else result end end |
#set_attachments(result, resource) ⇒ Object
set attachments to result json
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/generators/propel_facets/templates/controllers/concerns/facet_renderer.rb', line 61 def (result, resource) = {} resource.class..keys.each do |name| begin = resource.send(name.to_sym) if .is_a? ActiveStorage::Attached::Many # Note: This is configured to return a maximum of 10 files. # To retrieve all files within a specific parent model, utilize the 'Attachment' class. [name.to_sym] = .take(10).map {|a| url_for(a) if .attached?}.compact else [name.to_sym] = url_for() if .attached? end rescue StandardError => e Rails.logger.error "Failed to generate URL for attachment #{name}: #{e.}" next end end result&.merge() end |