Class: Admin::ResourceController

Inherits:
ApplicationController
  • Object
show all
Extended by:
TrustyCms::ResourceResponses
Defined in:
app/controllers/admin/resource_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TrustyCms::ResourceResponses

create_responses, extended

Class Method Details

.model_class(model_class = nil) ⇒ Object



74
75
76
# File 'app/controllers/admin/resource_controller.rb', line 74

def self.model_class(model_class = nil)
  @model_class ||= (model_class || controller_name).to_s.singularize.camelize.constantize
end

.paginate_models(options = {}) ⇒ Object

call paginate_models to declare that will_paginate should be used in the index view options specified here are accessible in the view by calling will_paginate_options eg.

Class MyController < Admin::ResourceController

paginate_models :per_page => 100


85
86
87
88
89
# File 'app/controllers/admin/resource_controller.rb', line 85

def self.paginate_models(options = {})
  @@paginated = true
  @@will_paginate_options = options.slice(:class, :previous_label, :next_label, :inner_window, :outer_window, :separator, :container).merge(param_name: :p)
  @@default_per_page = options[:per_page]
end

Instance Method Details

#destroyObject



69
70
71
72
# File 'app/controllers/admin/resource_controller.rb', line 69

def destroy
  model.destroy
  response_for :destroy
end

#indexObject



48
49
50
# File 'app/controllers/admin/resource_controller.rb', line 48

def index
  response_for :plural
end

#paginated?Boolean

a convenience method that returns true if paginate_models has been called on this controller class and can be used to make display decisions in controller and view

Returns:

  • (Boolean)


104
105
106
# File 'app/controllers/admin/resource_controller.rb', line 104

def paginated?
  self.class.paginated == true && params[:pp] != 'all'
end

#pagination_parametersObject

return a hash of page and per_page that can be used to build a will_paginate collection the per_page figure can be set in several ways: request parameter > declared by paginate_models > default set in config entry @admin.pagination.per_page@ > overall default of 50



113
114
115
116
117
118
119
120
# File 'app/controllers/admin/resource_controller.rb', line 113

def pagination_parameters
  pp = params[:pp] || TrustyCms.config['admin.pagination.per_page']
  pp = (self.class.default_per_page || 50) if pp.blank?
  {
    page: (params[:p] || 1).to_i,
    per_page: pp.to_i,
  }
end

#will_paginate_optionsObject

returns a hash of options that can be passed to will_paginate the @pagination_for@ helper method calls @will_paginate_options@ unless other options are supplied.

pagination_for(@events)



96
97
98
# File 'app/controllers/admin/resource_controller.rb', line 96

def will_paginate_options
  self.class.will_paginate_options || {}
end