Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ApplicationController < ActionController::Base
- ##################### ABOUT THIS CONTROLLER ####################################
- # #
- # This controller is intended to dry out normal restful actions in #
- # subclassed controllers. Some pointers when inheriting from this controller: #
- # #
- # 1) Any method in this controller can be overwritten in a subclassed controller, #
- # in fact, some are intended to be overwritten. #
- # #
- # 2) Before_filters defined in subclassed controllers will be called AFTER the #
- # ones defined here, so you'll have access to the instance variables that are #
- # set here. #
- # #
- # 3) you should not need to define show, edit, new, or index (or any other action #
- # that just renders a template.) Rails will automatically render the template #
- # that is named the same as the action from the appropriate views directory. #
- # for example: #
- # if you want issues/show, you just make a file in app/views/issues called #
- # show.html.erb. You will already have @issue from the before filters. #
- # #
- ####################################################################################
- include AuthHelper # custom helper file, contains methods that have to do with AUTHORIZATION, not AUTHENTICATION.
- include AuthenticatedSystem # from restful_authentication
- include ExceptionNotifiable # from exception_notfication plugin
- #this may not need to be global, but it does not seem to affect performance here.
- uses_tiny_mce :options => {
- :theme => 'advanced',
- :theme_advanced_resizing => true,
- :theme_advanced_resize_horizontal => false,
- :plugins => %w{ table fullscreen style },
- :theme_advanced_buttons3_add => "styleprops"
- }
- helper :all # include all helpers, all the time
- helper_method :object, :model, :model_name
- before_filter :login_required
- before_filter :check_permission, :except => :index
- before_filter :cancel_redirect, :only => [:update, :create]
- before_filter :set_object
- before_filter :set_title, :except => [:create, :update, :destroy]
- before_filter :set_objects
- before_filter :set_updater, :only => :update
- before_filter :set_creator, :only => :create
- before_filter :set_destroyer, :only => :destroy
- #all methods called inside these public ones can be found below with comments under the private macro.
- def create
- object.save ? after_success : after_failure
- end
- def update
- object.attributes = params[:object]
- object.save ? after_success : after_failure
- end
- def destroy
- object.destroyed_at = Time.now if object.respond_to? :destroyed_at
- object.save!
- object.destroy
- flash[:notice] = "#{model_name.titleize} Destroyed"
- redirect_to :action => :index
- end
- private
- #this method finds the active record object if there is params[:id] else it instantiates a new model instance based on
- #params for that model.
- #for instance, if you render issues/new, params[:issue] will be nil, and a bare object will be created.
- #when you submit the form rendered by that action, you will commit params[:issue], so those params will
- #be picked up here and assigned to the newly instantiated object through mass assignment.
- #the ||= allows you to call object several times within an action without hitting the database everytime
- def object
- @object ||= params[:id] ? model.find(params[:id]) : model.new(params[model_name.to_sym])
- end
- def model
- @model ||= model_name.constantize
- end
- #there is probably a better way to derive the model name here, if anyone knows what it is, please feel free to change this.
- #the only requirement is that it returns a string that can be constantized into an ActiveRecord::Base class
- def model_name
- controller_class_name.gsub(/Controller$/, "").singularize
- end
- #this is a before filter that sets the collection of objects to be rendered in the index view
- #you may rewrite this method in an subclassed controller to add pagination,
- #scope the collection, or add more conditions to the find.
- def set_objects
- objects = model.find(:all)
- instance_variable_set "@#{model.to_s.underscore.pluralize}", objects
- end
- #this method sets an instance variable for the object that's useful in the views
- #for instance, if you're in the Issues Controller, it will set @issue
- def set_object
- instance_variable_set "@#{model.to_s.underscore}", object
- end
- #set creator, updater, and destroyer are here because these methods are protected attributes of our models in this application
- def set_creator
- object.created_by = @session_user if object.respond_to?(:created_by)
- end
- def set_updater
- object.updated_by = @session_user if object.respond_to?(:updated_by)
- end
- #we need to set destroyer because some of our models are never destroyed because of versioning
- def set_destroyer
- object.destroyed_by = @session_user if object.respond_to?(:destroyed_by)
- end
- #auth filtering goes here, including flash message and redirect.
- def check_permission
- true
- end
- #this method allows you to put a cancel button next to the submit button in any form.
- #this method can be overwritten in a subclassed controller to customize where to send the user after a cancellation.
- def cancel_redirect
- if params[:commit] == "Cancel"
- flash[:notice] = "Action cancelled by user."
- redirect_to :action => :index
- end
- end
- #called if a crud operation succeeds
- def after_success
- respond_to do |format|
- format.html {
- flash[:success] = success_message
- redirect_to action_after_success
- }
- format.js
- end
- end
- #called if a crud operation fails
- def after_failure
- respond_to do |format|
- format.html {
- flash[:failure] = failure_message
- render action_after_failure
- }
- format.js
- end
- end
- #this determines where to redirect after a successful crud operation,
- #it should return a hash that can be plugged into redirect_to
- def action_after_success
- next_action = params[:action] == "destroy" ? :index : :dots
- id = params[:action] == "destroy" ? nil : object.id
- {:action => next_action, :id => id}
- end
- #this determines what template to render after a failed crud operation
- #it should be able to be plugged into render
- def action_after_failure
- case params[:action]
- when "destroy"
- next_action = :index
- when "update"
- next_action = :edit
- when "create"
- next_action = :add_child
- else
- next_action = :dots
- end
- #id = params[:action] == "destroy" ? nil : object.id
- {:action => next_action}
- end
- #these are the methods that set the flash message after a crud operation
- def success_message
- "#{model_name.titleize} #{params[:action].titleize}d"
- end
- def failure_message
- "#{model_name.titleize} Was Not #{params[:action].titleize}d"
- end
- #@title is called inside the <title></title> tags in the application layout.
- def set_title
- @title ||= "#{params[:action].to_s.titleize} For #{model_name.titleize}"
- end
- # See ActionController::RequestForgeryProtection for details
- # Uncomment the :secret if you're not using the cookie session store
- protect_from_forgery :secret => '61c027dd20a59a72124a183a36eb6ae1'
- # Return true if a parameter corresponding to the given symbol was posted.
- def param_posted?(symbol)
- request.post? and params[symbol]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement