Module: Neuron::Resources::Controller::ClassMethods

Defined in:
lib/neuron/resources.rb

Instance Method Summary collapse

Instance Method Details

#order_scopes(options = {}) ⇒ Object

Adds default ordering scopes for #index action

Parameters:

  • options (Hash) (defaults to: {})

    options hash

Options Hash (options):

  • :ascending (Array, Symbol, String)

    default ascending scopes

  • :descending (Array, Symbol, String)

    default descending scopes



51
52
53
54
55
56
# File 'lib/neuron/resources.rb', line 51

def order_scopes(options = {})
  with_options 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(options = {})
  define_method(:collection_with_pagination) do
    get_collection_ivar || begin
      results = end_of_association_chain
      if (params[:paginate] != "false") && applicable?(options[:if], true) && applicable?(options[: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(options = {})
  options = Neuron::Resources.default_options.merge(options)
  options[:order] = {}    if options[:order] == true
  options[:paginate] = {} if options[:paginate] == true

  unless respond_to?(:resource_options)
    class_attribute :resource_options
  end
  self.resource_options = options
  prepend_before_filter :set_default_collection_scopes, only: [:index]

  inherit_resources

  if options[:order]
    order_scopes(options[:order])
  end

  if options[:paginate]
    paginate_scope(options[:paginate])
  end

  if options[:authorize]
    authorize_resources(options[:authorize])
  end
end