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



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

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


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

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



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

def destroy
  model.destroy
  response_for :destroy
end

#indexObject



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

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)


102
103
104
# File 'app/controllers/admin/resource_controller.rb', line 102

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



110
111
112
113
114
115
116
117
# File 'app/controllers/admin/resource_controller.rb', line 110

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)



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

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