Class: AbstractResourcesController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- AbstractResourcesController
- Defined in:
- app/controllers/abstract_resources_controller.rb
Constant Summary collapse
- PRINTSUCCESS =
1- NOQUEUE =
-1- NOUSER =
-2- PRINTRECERROR =
-3- PRINTCMDERROR =
-4- PRINTLISTERROR =
-5- PRINTEXCEPTION =
-99
Instance Method Summary collapse
- #_id ⇒ Object
- #activate ⇒ Object
- #attach ⇒ Object
- #create ⇒ Object
- #deactivate ⇒ Object
- #defer ⇒ Object
- #destroy ⇒ Object
- #detach ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
-
#new_resource ⇒ Object
def resource=val @resource=val end.
- #parent ⇒ Object
- #parent? ⇒ Boolean
- #parent_class ⇒ Object
- #parent_class=(val) ⇒ Object
-
#parent_url(options = {}) ⇒ Object
parent_url returns the parent url - /employees/1.
-
#prefer ⇒ Object
default implementation.
-
#print ⇒ Object
print this view - let the Class handle everything returning either the ID to a print_job or false (in which case something went terribly wrong).
- #resource ⇒ Object
- #resource? ⇒ Boolean
- #resource_class ⇒ Object
- #resource_class=(val) ⇒ Object
- #resource_name(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_parents ⇒ Object
build an array of the resource - particular to <SELECT>.
- #set_resource ⇒ Object
- #set_resources ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Methods inherited from ApplicationController
Instance Method Details
#_id ⇒ Object
265 266 267 268 269 270 |
# File 'app/controllers/abstract_resources_controller.rb', line 265 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 |
#activate ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/controllers/abstract_resources_controller.rb', line 47 def activate resource if resource.activate flash[:info] = t(:resource_activated_correct) render :activate, layout: false, status: 200 and return else flash[:error] = t(:resource_activated_incorrect) render :activate, layout: false, status: 401 and return end rescue Exception => e scoop_from_error e end |
#attach ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/controllers/abstract_resources_controller.rb', line 76 def attach resource if resource.attach parent flash[:info] = t(:resource_attached_correct) render :attach, layout: false, status: 200 and return else flash[:error] = t(:resource_attached_incorrect) render :attach, layout: false, status: 401 and return end rescue Exception => e scoop_from_error e end |
#create ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'app/controllers/abstract_resources_controller.rb', line 181 def create resource respond_with(resource, location: redirect_after_create ) do |format| if resource.save && update_parenthood flash[:notice] = t('.success.created', resource: resource_class.to_s ) else format.html { render action: :new, status: :unprocessable_entity } format.js { render action: :new, status: :unprocessable_entity } end end rescue Exception => e scoop_from_error e end |
#deactivate ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/controllers/abstract_resources_controller.rb', line 61 def deactivate resource if resource.deactivate flash[:info] = t(:resource_deactivated_correct) render :deactivate, layout: false, status: 200 and return else flash[:error] = t(:resource_deactivated_incorrect) render :deactivate, layout: false, status: 401 and return end rescue Exception => e scoop_from_error e end |
#defer ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/controllers/abstract_resources_controller.rb', line 33 def defer resource if resource.defer parent flash[:info] = t(:resource_deferred_correct) render :defer, layout: false, status: 200 and return else flash[:error] = t(:resource_deferred_incorrect) render :defer, layout: false, status: 401 and return end rescue Exception => e scoop_from_error e end |
#destroy ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'app/controllers/abstract_resources_controller.rb', line 209 def destroy resource result = true if resource.destroy && update_parenthood result ? (flash.now[:notice] = t('.success', resource: resource_class.to_s)) : (flash.now[:error] = t('.deleted.error',resource: resource_class.to_s)) if result==true render layout:false, status: 200, locals: { result: true } else render layout:false, status: 301, locals: { result: true } end rescue Exception => e scoop_from_error e end |
#detach ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/controllers/abstract_resources_controller.rb', line 90 def detach resource if resource.detach parent flash[:info] = t(:resource_detached_correct) render :detach, layout: false, status: 200 and return else flash[:error] = t(:resource_detached_incorrect) render :detach, layout: false, status: 401 and return end rescue Exception => e scoop_from_error e end |
#edit ⇒ Object
131 132 133 134 135 136 |
# File 'app/controllers/abstract_resources_controller.rb', line 131 def edit resource respond_with resource rescue Exception => e scoop_from_error e end |
#index ⇒ Object
174 175 176 177 178 179 |
# File 'app/controllers/abstract_resources_controller.rb', line 174 def index resource_class, :index? respond_with resources rescue Exception => e scoop_from_error e end |
#new ⇒ Object
123 124 125 126 127 128 129 |
# File 'app/controllers/abstract_resources_controller.rb', line 123 def new resource.parent_id = params[:parent_id] if resource.respond_to? :parent_id resource respond_with resource rescue Exception => e scoop_from_error e end |
#new_resource ⇒ Object
def resource=val
@resource=val
end
254 255 256 257 258 259 260 261 262 263 |
# File 'app/controllers/abstract_resources_controller.rb', line 254 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 |
#parent ⇒ Object
290 291 292 |
# File 'app/controllers/abstract_resources_controller.rb', line 290 def parent @parent ||= find_parent end |
#parent? ⇒ Boolean
303 304 305 |
# File 'app/controllers/abstract_resources_controller.rb', line 303 def parent? !(%w{NilClass TrueClass FalseClass}.include? parent.class.to_s) end |
#parent_class ⇒ Object
295 296 297 |
# File 'app/controllers/abstract_resources_controller.rb', line 295 def parent_class @parent_class ||= @parent.class end |
#parent_class=(val) ⇒ Object
299 300 301 |
# File 'app/controllers/abstract_resources_controller.rb', line 299 def parent_class= val @parent_class = val end |
#parent_url(options = {}) ⇒ Object
parent_url returns the parent url - /employees/1
343 344 345 346 347 |
# File 'app/controllers/abstract_resources_controller.rb', line 343 def parent_url ={} parent? ? ( "/%s/%s" % [ @parent.class.table_name, @parent.id ] ) : "" rescue Exception => e scoop_from_error e end |
#prefer ⇒ Object
default implementation
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/controllers/abstract_resources_controller.rb', line 19 def prefer resource if resource.prefer parent flash[:info] = t(:resource_preferred_correct) render :prefer, layout: false, status: 200 and return else flash[:error] = t(:resource_preferred_incorrect) render :prefer, layout: false, status: 401 and return end rescue Exception => e scoop_from_error e end |
#print ⇒ Object
print this view - let the Class handle everything returning either the ID to a print_job or false (in which case something went terribly wrong)
always an Ajax call - hence will always update the print_jobs link with ‘yellow’-blink POST /printers/print.js params holds records to be printed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/controllers/abstract_resources_controller.rb', line 153 def print resource, :print? if resources.any? if print_resources > 0 flash[:info] = t(:resources_printed_correct) status = 200 else flash[:error] = t(:resources_not_found_printjob_not_created) status = 301 end else flash[:error] = I18n.oxt(:resources_not_found_printjob_not_created) status = 301 end return if params[:download] render :print, layout: false, status: status and return rescue Exception => e scoop_from_error e end |
#resource ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'app/controllers/abstract_resources_controller.rb', line 241 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
235 236 237 238 239 |
# File 'app/controllers/abstract_resources_controller.rb', line 235 def resource? !(%w{NilClass TrueClass FalseClass}.include? @resource.class.to_s) rescue Exception => e scoop_from_error e end |
#resource_class ⇒ Object
279 280 281 282 283 284 |
# File 'app/controllers/abstract_resources_controller.rb', line 279 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
286 287 288 |
# File 'app/controllers/abstract_resources_controller.rb', line 286 def resource_class= val @resource_class = val end |
#resource_name(options = {}) ⇒ Object
272 273 274 275 276 277 |
# File 'app/controllers/abstract_resources_controller.rb', line 272 def resource_name ={} resource_class.table_name rescue Exception => e scoop_from_error e # resource_class.to_s.underscore.pluralize end |
#resource_params ⇒ Object
222 223 224 |
# File 'app/controllers/abstract_resources_controller.rb', line 222 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
317 318 319 320 321 322 323 324 325 326 327 |
# File 'app/controllers/abstract_resources_controller.rb', line 317 def resource_url ={} = () action= case params[:action] when 'show'; '/%s' % resource.id when 'edit'; '/%s/edit' % resource.id else '' end parent? ? ("%s/%s%s%s" % [parent_url,resource_name,action,]) : ("/%s%s%s" % [resource_name,action,]) rescue Exception => e scoop_from_error e end |
#resources(options = {}) ⇒ Object
return the resources collection - preferably from the cache
310 311 312 |
# File 'app/controllers/abstract_resources_controller.rb', line 310 def resources ={} @resources ||= find_resources end |
#resources_url(options = {}) ⇒ Object
returns the url for the resources - /employees or /employees/1/events
332 333 334 335 336 337 338 339 |
# File 'app/controllers/abstract_resources_controller.rb', line 332 def resources_url ={} = () r = "%s_path" % resource_name r = send(r) [ parent_url, r ].join("/").gsub /\/\//,'/' rescue Exception => e scoop_from_error e end |
#set_parents ⇒ Object
build an array of the resource - particular to <SELECT>
106 107 108 109 110 111 112 113 114 |
# File 'app/controllers/abstract_resources_controller.rb', line 106 def set_parents unless (resource_class.respond_to?( 'arraying') && resource? ) @parents = [] else @parents = resource_class.({ order: 'name'}, resource.possible_parents) end rescue Exception => e scoop_from_error e end |
#set_resource ⇒ Object
226 227 228 229 |
# File 'app/controllers/abstract_resources_controller.rb', line 226 def set_resource parent resource end |
#set_resources ⇒ Object
231 232 233 |
# File 'app/controllers/abstract_resources_controller.rb', line 231 def set_resources resources end |
#show ⇒ Object
116 117 118 119 120 121 |
# File 'app/controllers/abstract_resources_controller.rb', line 116 def show resource respond_with resource rescue Exception => e scoop_from_error e end |
#update ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'app/controllers/abstract_resources_controller.rb', line 195 def update resource respond_with(resource, location: redirect_after_update) do |format| if resource.update_attributes(resource_params) && update_parenthood flash[:notice] = t('.success.updated', resource: resource_class.to_s ) else format.html { render action: :edit, status: :unprocessable_entity } format.js { render action: :edit, status: :unprocessable_entity } end end rescue Exception => e scoop_from_error e end |