Module: ResourceControl
- Extended by:
- ActiveSupport::Concern
- Included in:
- AbstractResourcesController
- Defined in:
- app/controllers/concerns/resource_control.rb
Instance Method Summary collapse
-
#_filter(r, options) ⇒ Object
filter - it at all.
- #_flip(val) ⇒ Object
- #_id ⇒ Object
-
#_paginate(r) ⇒ Object
paginate - it at all.
-
#_search(options) ⇒ Object
search - it at all.
-
#_sort(r, options) ⇒ Object
sort - it at all.
-
#find_all_resources(options) ⇒ Object
find queried resources collection - implement on each controller to customize raise an exception.
-
#find_resources(options) ⇒ Object
find the resources collection.
-
#find_resources_queried(options) ⇒ Object
find queried resources collection - implement on each controller to customize raise an exception.
-
#new_resource ⇒ Object
def resource=val @resource=val end.
- #resource ⇒ Object
- #resource? ⇒ Boolean
- #resource_class ⇒ Object
- #resource_class=(val) ⇒ Object
- #resource_name(options = {}) ⇒ Object
- #resource_options(options = {}) ⇒ Object
- #resource_params ⇒ Object
-
#resource_url(options = {}) ⇒ Object
returns the url for the resource - like /users/1.
-
#resources(options = {}) ⇒ Object
return the resources collection - preferably from the cache.
-
#resources_url(options = {}) ⇒ Object
returns the url for the resources - /employees or /employees/1/events.
- #set_resource ⇒ Object
- #set_resources ⇒ Object
- #sorting_direction(val = nil) ⇒ Object
Instance Method Details
#_filter(r, options) ⇒ Object
filter - it at all
165 166 167 168 169 |
# File 'app/controllers/concerns/resource_control.rb', line 165 def _filter r, return r if params[:f].blank? return resource_class.filter r, params[:f], if resource_class.respond_to? :filter r end |
#_flip(val) ⇒ Object
184 185 186 187 188 189 |
# File 'app/controllers/concerns/resource_control.rb', line 184 def _flip val @sorting_direction = (val == "ASC" ? "DESC" : "ASC") val rescue @sorting_direction = "ASC" end |
#_id ⇒ Object
56 57 58 59 60 61 |
# File 'app/controllers/concerns/resource_control.rb', line 56 def _id return nil if !params[:id] || params[:id]=="0" params[:id] || params["#{resource_class.to_s.downcase}_id".to_sym] rescue Exception => e scoop_from_error e end |
#_paginate(r) ⇒ Object
paginate - it at all
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'app/controllers/concerns/resource_control.rb', line 194 def _paginate r return r if params[:action]=='print' # will it obey the #page call? # is it without format or html or scrolling or with search? if (r.respond_to?( :page)) && (params[:format].nil? or params[:format]=='html' or params[:scrolling] or params[:s]) params[:perpage] ||= 20 params[:page] ||= 1 return r.page(params[:page]).per(params[:perpage]) else return r end end |
#_search(options) ⇒ Object
search - it at all
154 155 156 157 158 159 160 |
# File 'app/controllers/concerns/resource_control.rb', line 154 def _search if params[:q].blank? #or params[:q]=="undefined" parent? ? parent.send(resource_name) : (resource_class.nil? ? nil : find_all_resources()) else find_resources_queried end end |
#_sort(r, options) ⇒ Object
sort - it at all
%th{ role:“sort”, data{ field: “*barcode”, direction: “DESC”} } $(‘th’)
176 177 178 |
# File 'app/controllers/concerns/resource_control.rb', line 176 def _sort r, r.order "%s %s" % [ (params[:s] || "id"), sorting_direction(params[:d]) ] end |
#find_all_resources(options) ⇒ Object
find queried resources collection - implement on each controller to customize raise an exception
210 211 212 |
# File 'app/controllers/concerns/resource_control.rb', line 210 def find_all_resources policy_scope(resource_class) end |
#find_resources(options) ⇒ Object
find the resources collection
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/controllers/concerns/resource_control.rb', line 126 def find_resources # return [] unless resource_class.ancestors.include? ActiveRecord::Base params[:ids] ||= [] params[:ids] << params[:id] unless params[:id].nil? if params[:ids].compact.any? policy_scope(resource_class).where(id: params[:ids].compact.split(",").flatten) else # search r = _search # filter r = _filter r, # sort r = _sort r, # paginate r = _paginate r # (params[:format].nil? or params[:format]=='html') ? r.page(params[:page]).per(params[:perpage]) : r end end |
#find_resources_queried(options) ⇒ Object
find queried resources collection - implement on each controller to customize raise an exception
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'app/controllers/concerns/resource_control.rb', line 217 def find_resources_queried case params[:f] when nil if parent? policy_scope(resource_class).( params[:q].split(" ") ).where( ) else policy_scope(resource_class).( params[:q].split(" ") ) end else policy_scope(resource_class) end end |
#new_resource ⇒ Object
def resource=val
@resource=val
end
45 46 47 48 49 50 51 52 53 54 |
# File 'app/controllers/concerns/resource_control.rb', line 45 def new_resource return nil if resource_class.nil? return resource_class.new if resource_class.ancestors.include?( ActiveRecord::Base ) and !(params.include? resource_class.to_s.underscore) #[ 'create', 'update' ].include? params[:action] p = resource_params p=p.compact.first if p.class==Array return resource_class.new(p.merge(current_user: current_user)) if resource_class.ancestors.include? ActiveRecord::Base nil rescue Exception => e scoop_from_error e end |
#resource ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'app/controllers/concerns/resource_control.rb', line 32 def resource @resource ||= (_id.nil? ? new_resource : resource_class.find(_id) ) return @resource if @resource.nil? @resource.current_user = (current_user || nil) if @resource.respond_to? :current_user @resource rescue Exception => e scoop_from_error e end |
#resource? ⇒ Boolean
26 27 28 29 30 |
# File 'app/controllers/concerns/resource_control.rb', line 26 def resource? !(%w{NilClass TrueClass FalseClass}.include? @resource.class.to_s) rescue Exception => e scoop_from_error e end |
#resource_class ⇒ Object
70 71 72 73 74 75 |
# File 'app/controllers/concerns/resource_control.rb', line 70 def resource_class return @resource_class if resource? @resource_class ||= params[:controller].singularize.classify.constantize rescue Exception => e scoop_from_error e end |
#resource_class=(val) ⇒ Object
77 78 79 |
# File 'app/controllers/concerns/resource_control.rb', line 77 def resource_class= val @resource_class = val end |
#resource_name(options = {}) ⇒ Object
63 64 65 66 67 68 |
# File 'app/controllers/concerns/resource_control.rb', line 63 def resource_name ={} resource_class.table_name rescue Exception => e scoop_from_error e # resource_class.to_s.underscore.pluralize end |
#resource_options(options = {}) ⇒ Object
119 120 121 122 |
# File 'app/controllers/concerns/resource_control.rb', line 119 def ={} .merge! params.except( "id", "controller", "action", "utf8", "_method", "authenticity_token" ) .empty? ? "" : "?" + .collect{ |k,v| "#{k}=#{v}" }.join("&") end |
#resource_params ⇒ Object
13 14 15 |
# File 'app/controllers/concerns/resource_control.rb', line 13 def resource_params raise 'You need to "def resource_params" on the %sController! (see: http://blog.trackets.com/2013/08/17/strong-parameters-by-example.html)' % params[:controller].capitalize end |
#resource_url(options = {}) ⇒ Object
returns the url for the resource - like /users/1
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/controllers/concerns/resource_control.rb', line 91 def resource_url ={} r=request.path id=params[:id] = case params[:action] when 'create','update','delete','destroy'; "" else () end return "%s%s" % [r,] if r.match "#{resource_class.to_s.tableize}\/#{id}$" "%s%s" % [ r.split("/#{params[:action]}")[0], ] rescue Exception => e scoop_from_error e end |
#resources(options = {}) ⇒ Object
return the resources collection - preferably from the cache
84 85 86 |
# File 'app/controllers/concerns/resource_control.rb', line 84 def resources ={} @resources ||= find_resources end |
#resources_url(options = {}) ⇒ Object
returns the url for the resources - /employees or /employees/1/events
107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/controllers/concerns/resource_control.rb', line 107 def resources_url ={} r=request.path = case params[:action] when 'create','update','delete','destroy'; "" else () end return "%s%s" % [r,] if r.match "#{resource_class.to_s.tableize}$" "%s%s%s" % [ r.split( resource_class.to_s.tableize)[0],resource_class.to_s.tableize, ] rescue Exception => e scoop_from_error e end |
#set_resource ⇒ Object
17 18 19 20 |
# File 'app/controllers/concerns/resource_control.rb', line 17 def set_resource parent resource end |
#set_resources ⇒ Object
22 23 24 |
# File 'app/controllers/concerns/resource_control.rb', line 22 def set_resources resources end |
#sorting_direction(val = nil) ⇒ Object
180 181 182 |
# File 'app/controllers/concerns/resource_control.rb', line 180 def sorting_direction val=nil _flip(val || @sorting_direction) end |