Module: Neuron::Resources::Controller::ClassMethods
- Defined in:
- lib/neuron/resources.rb
Instance Method Summary collapse
-
#order_scopes(options = {}) ⇒ Object
Adds default ordering scopes for #index action.
- #paginate_scope(options = {}) ⇒ Object
- #resources(options = {}) ⇒ Object
Instance Method Details
#order_scopes(options = {}) ⇒ Object
Adds default ordering scopes for #index action
51 52 53 54 55 56 |
# File 'lib/neuron/resources.rb', line 51 def order_scopes( = {}) only: :index, type: :array do |index| index.has_scope(:ascending) { |controller, scope, values| values.any? ? scope.asc(*values) : scope } index.has_scope(:descending) { |controller, scope, values| values.any? ? scope.desc(*values) : scope } end end |
#paginate_scope(options = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/neuron/resources.rb', line 58 def paginate_scope( = {}) define_method(:collection_with_pagination) do get_collection_ivar || begin results = end_of_association_chain if (params[:paginate] != "false") && applicable?([:if], true) && applicable?([:unless], false) results = results.page(params[:page]).per(params[:per_page]) headers['X-Pagination-TotalEntries'] = results.total_entries.to_s headers['X-Pagination-TotalPages'] = results.total_pages.to_s headers['X-Pagination-CurrentPage'] = results.current_page.to_s headers['X-Pagination-PerPage'] = results.limit_value.to_s end set_collection_ivar(results) end end alias_method_chain :collection, :pagination end |
#resources(options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/neuron/resources.rb', line 21 def resources( = {}) = Neuron::Resources..merge() [:order] = {} if [:order] == true [:paginate] = {} if [:paginate] == true unless respond_to?(:resource_options) class_attribute :resource_options end self. = prepend_before_filter :set_default_collection_scopes, only: [:index] inherit_resources if [:order] order_scopes([:order]) end if [:paginate] paginate_scope([:paginate]) end if [:authorize] ([:authorize]) end end |