Class: Brainstem::PresenterCollection
- Inherits:
-
Object
- Object
- Brainstem::PresenterCollection
- Defined in:
- lib/brainstem/presenter_collection.rb
Instance Attribute Summary collapse
-
#default_max_filter_and_search_page ⇒ Object
Returns the value of attribute default_max_filter_and_search_page.
-
#default_max_per_page ⇒ Integer
The maximum number of objects that can be requested in a single presented hash.
-
#default_per_page ⇒ Integer
The default number of objects that will be returned in the presented hash.
Instance Method Summary collapse
- #add_presenter_class(presenter_class, *klasses) ⇒ Object
- #brainstem_key_for!(klass) ⇒ Object
-
#for(klass) ⇒ Brainstem::Presenter?
A new instance of the Presenter that knows how to present the class
klass
, ornil
if there isn’t one. -
#for!(klass) ⇒ Brainstem::Presenter
A new instance of the Presenter that knows how to present the class
klass
. -
#presenters ⇒ Hash
The presenters this collection knows about, keyed on the names of the classes that can be presented.
-
#presenting(name, options = {}) { ... } ⇒ Hash
The main presentation method, converting a model name and an optional scope into a hash structure, ready to be converted into JSON.
- #structure_response(presented_class, primary_models, strategy, count, options) ⇒ Object
- #validate! ⇒ Object
Instance Attribute Details
#default_max_filter_and_search_page ⇒ Object
Returns the value of attribute default_max_filter_and_search_page.
15 16 17 |
# File 'lib/brainstem/presenter_collection.rb', line 15 def default_max_filter_and_search_page @default_max_filter_and_search_page end |
#default_max_per_page ⇒ Integer
Returns The maximum number of objects that can be requested in a single presented hash.
9 10 11 |
# File 'lib/brainstem/presenter_collection.rb', line 9 def default_max_per_page @default_max_per_page end |
#default_per_page ⇒ Integer
Returns The default number of objects that will be returned in the presented hash.
13 14 15 |
# File 'lib/brainstem/presenter_collection.rb', line 13 def default_per_page @default_per_page end |
Instance Method Details
#add_presenter_class(presenter_class, *klasses) ⇒ Object
123 124 125 126 127 |
# File 'lib/brainstem/presenter_collection.rb', line 123 def add_presenter_class(presenter_class, *klasses) klasses.each do |klass| presenters[klass.to_s] = presenter_class end end |
#brainstem_key_for!(klass) ⇒ Object
140 141 142 143 144 |
# File 'lib/brainstem/presenter_collection.rb', line 140 def brainstem_key_for!(klass) presenter = presenters[klass.to_s] raise(ArgumentError, "Unable to find a presenter for class #{klass}") unless presenter presenter.configuration[:brainstem_key] || klass.table_name end |
#for(klass) ⇒ Brainstem::Presenter?
Returns A new instance of the Presenter that knows how to present the class klass
, or nil
if there isn’t one.
130 131 132 |
# File 'lib/brainstem/presenter_collection.rb', line 130 def for(klass) presenters[klass.to_s].try(:new) end |
#for!(klass) ⇒ Brainstem::Presenter
Returns A new instance of the Presenter that knows how to present the class klass
.
136 137 138 |
# File 'lib/brainstem/presenter_collection.rb', line 136 def for!(klass) self.for(klass) || raise(ArgumentError, "Unable to find a presenter for class #{klass}") end |
#presenters ⇒ Hash
Returns The presenters this collection knows about, keyed on the names of the classes that can be presented.
117 118 119 |
# File 'lib/brainstem/presenter_collection.rb', line 117 def presenters @presenters ||= {} end |
#presenting(name, options = {}) { ... } ⇒ Hash
The main presentation method, converting a model name and an optional scope into a hash structure, ready to be converted into JSON. If searching, Brainstem filtering, only, pagination, and ordering are skipped and should be implemented with your search solution. All request options are passed to the search block for your convenience.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/brainstem/presenter_collection.rb', line 37 def presenting(name, = {}, &block) [:params] = HashWithIndifferentAccess.new([:params] || {}) () set_default_filters_option!() presented_class = ([:model] || name) presented_class = presented_class.classify.constantize if presented_class.is_a?(String) scope = presented_class.instance_eval(&block) count = 0 # grab the presenter that knows about filters and sorting etc. [:primary_presenter] ||= for!(presented_class) # table name will be used to query the database for the filtered data [:table_name] = presented_class.table_name [:default_per_page] = default_per_page [:default_max_per_page] = default_max_per_page [:default_max_filter_and_search_page] = default_max_filter_and_search_page strategy = get_strategy(, scope) primary_models, count = strategy.execute(scope) # Determine if an exception should be raised on an empty result set. if [:raise_on_empty] && primary_models.empty? raise [:empty_error_class] || ActiveRecord::RecordNotFound end structure_response(presented_class, primary_models, strategy, count, ) end |
#structure_response(presented_class, primary_models, strategy, count, options) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/brainstem/presenter_collection.rb', line 67 def structure_response(presented_class, primary_models, strategy, count, ) # key these models will use in the struct that is output brainstem_key = brainstem_key_for!(presented_class) # filter the incoming :includes list by those available from this Presenter in the current context selected_associations = filter_includes() optional_fields = filter_optional_fields() page_size = strategy.calculate_per_page struct = { 'count' => count, 'results' => [], brainstem_key => {}, 'meta' => { 'count' => count, 'page_count' => count > 0 ? (count.to_f / page_size).ceil : 0, 'page_number' => count > 0 ? [:params].fetch(:page, 1).to_i : 0, 'page_size' => page_size, } } # Build top-level keys for all requested associations. selected_associations.each do |association| struct[brainstem_key_for!(association.target_class)] ||= {} unless association.polymorphic? end if primary_models.length > 0 associated_models = {} presented_primary_models = [:primary_presenter].group_present(primary_models, selected_associations.map(&:name), optional_fields: optional_fields, load_associations_into: associated_models) struct[brainstem_key] = presented_primary_models.each.with_object({}) { |model, obj| obj[model['id']] = model } struct['results'] = presented_primary_models.map { |model| { 'key' => brainstem_key, 'id' => model['id'] } } associated_models.each do |association_brainstem_key, associated_models_hash| presenter = for!(associated_models_hash.values.first.class) struct[association_brainstem_key] ||= {} presenter.group_present(associated_models_hash.values).each do |model| struct[association_brainstem_key][model['id']] ||= model end end end struct end |
#validate! ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/brainstem/presenter_collection.rb', line 147 def validate! errors = [] presenters.each do |name, klass| validator = Brainstem::PresenterValidator.new(klass) unless validator.valid? errors += validator.errors..map { |error| "#{name}: #{error}" } end end raise "PresenterCollection invalid:\n - #{errors.join("\n - ")}" if errors.length > 0 end |