Advertisement
Guest User

chris

a guest
Mar 11th, 2009
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.20 KB | None | 0 0
  1. class ApplicationController < ActionController::Base
  2.   #####################   ABOUT THIS CONTROLLER   ####################################
  3.   #                                                                                  #
  4.   # This controller is intended to dry out normal restful actions in                 #
  5.   # subclassed controllers. Some pointers when inheriting from this controller:      #
  6.   #                                                                                  #
  7.   # 1) Any method in this controller can be overwritten in a subclassed controller,  #
  8.   #    in fact, some are intended to be overwritten.                                 #
  9.   #                                                                                  #
  10.   # 2) Before_filters defined in subclassed controllers will be called AFTER the     #
  11.   #    ones defined here, so you'll have access to the instance variables that are   #
  12.   #    set here.                                                                     #
  13.   #                                                                                  #
  14.   # 3) you should not need to define show, edit, new, or index (or any other action  #
  15.   #    that just renders a template.) Rails will automatically render the template   #
  16.   #    that is named the same as the action from the appropriate views directory.    #
  17.   #    for example:                                                                  #
  18.   #    if you want issues/show, you just make a file in app/views/issues called      #
  19.   #    show.html.erb.  You will already have @issue from the before filters.         #
  20.   #                                                                                  #
  21.   ####################################################################################
  22.  
  23.  
  24.   include AuthHelper          # custom helper file, contains methods that have to do with AUTHORIZATION, not AUTHENTICATION.
  25.   include AuthenticatedSystem # from restful_authentication
  26.   include ExceptionNotifiable # from exception_notfication plugin
  27.  
  28.   #this may not need to be global, but it does not seem to affect performance here.
  29.   uses_tiny_mce :options => {
  30.                   :theme => 'advanced',
  31.                   :theme_advanced_resizing => true,
  32.                   :theme_advanced_resize_horizontal => false,
  33.                   :plugins => %w{ table fullscreen style },
  34.                   :theme_advanced_buttons3_add => "styleprops"
  35.                 }
  36.   helper :all # include all helpers, all the time
  37.   helper_method :object, :model, :model_name
  38.   before_filter :login_required
  39.   before_filter :check_permission, :except => :index
  40.   before_filter :cancel_redirect,  :only   => [:update, :create]
  41.   before_filter :set_object
  42.   before_filter :set_title,        :except => [:create, :update, :destroy]
  43.   before_filter :set_objects
  44.   before_filter :set_updater,      :only   => :update
  45.   before_filter :set_creator,      :only   => :create
  46.   before_filter :set_destroyer,    :only   => :destroy
  47.  
  48.  
  49.   #all methods called inside these public ones can be found below with comments under the private macro.
  50.   def create
  51.     object.save ? after_success : after_failure
  52.   end
  53.  
  54.   def update
  55.     object.attributes = params[:object]
  56.     object.save ? after_success : after_failure
  57.   end
  58.  
  59.   def destroy
  60.     object.destroyed_at = Time.now if object.respond_to? :destroyed_at
  61.     object.save!
  62.     object.destroy
  63.     flash[:notice] = "#{model_name.titleize} Destroyed"
  64.     redirect_to :action => :index
  65.   end
  66.  
  67.   private
  68.  
  69.   #this method finds the active record object if there is params[:id] else it instantiates a new model instance based on
  70.   #params for that model.
  71.   #for instance, if you render issues/new, params[:issue] will be nil, and a bare object will be created.
  72.   #when you submit the form rendered by that action, you will commit params[:issue], so those params will
  73.   #be picked up here and assigned to the newly instantiated object through mass assignment.
  74.   #the ||= allows you to call object several times within an action without hitting the database everytime
  75.   def object
  76.     @object ||= params[:id] ? model.find(params[:id]) : model.new(params[model_name.to_sym])
  77.   end
  78.  
  79.   def model
  80.     @model ||= model_name.constantize
  81.   end
  82.  
  83.   #there is probably a better way to derive the model name here, if anyone knows what it is, please feel free to change this.
  84.   #the only requirement is that it returns a string that can be constantized into an ActiveRecord::Base class
  85.   def model_name
  86.     controller_class_name.gsub(/Controller$/, "").singularize
  87.   end
  88.  
  89.   #this is a before filter that sets the collection of objects to be rendered in the index view
  90.   #you may rewrite this method in an subclassed controller to add pagination,
  91.   #scope the collection, or add more conditions to the find.
  92.   def set_objects
  93.     objects = model.find(:all)
  94.     instance_variable_set "@#{model.to_s.underscore.pluralize}", objects
  95.   end
  96.  
  97.   #this method sets an instance variable for the object that's useful in the views
  98.   #for instance, if you're in the Issues Controller, it will set @issue
  99.   def set_object
  100.     instance_variable_set "@#{model.to_s.underscore}", object
  101.   end
  102.  
  103.   #set creator, updater, and destroyer are here because these methods are protected attributes of our models in this application
  104.   def set_creator
  105.     object.created_by   = @session_user if object.respond_to?(:created_by)
  106.   end
  107.  
  108.   def set_updater
  109.     object.updated_by   = @session_user  if object.respond_to?(:updated_by)
  110.   end
  111.  
  112.   #we need to set destroyer because some of our models are never destroyed because of versioning
  113.   def set_destroyer
  114.     object.destroyed_by = @session_user  if object.respond_to?(:destroyed_by)
  115.   end
  116.  
  117.   #auth filtering goes here, including flash message and redirect.
  118.   def check_permission
  119.     true
  120.   end
  121.  
  122.   #this method allows you to put a cancel button next to the submit button in any form.
  123.   #this method can be overwritten in a subclassed controller to customize where to send the user after a cancellation.
  124.   def cancel_redirect
  125.     if params[:commit] == "Cancel"
  126.       flash[:notice] = "Action cancelled by user."
  127.       redirect_to :action => :index
  128.     end
  129.   end
  130.  
  131.   #called if a crud operation succeeds
  132.   def after_success
  133.     respond_to do |format|
  134.       format.html {
  135.         flash[:success] = success_message
  136.         redirect_to action_after_success
  137.       }
  138.       format.js
  139.     end
  140.   end
  141.  
  142.   #called if a crud operation fails
  143.   def after_failure
  144.     respond_to do |format|
  145.       format.html {
  146.         flash[:failure] = failure_message
  147.         render action_after_failure
  148.       }
  149.       format.js
  150.     end
  151.   end
  152.  
  153.   #this determines where to redirect after a successful crud operation,
  154.   #it should return a hash that can be plugged into redirect_to
  155.   def action_after_success
  156.     next_action = params[:action] == "destroy" ? :index : :dots
  157.     id = params[:action] == "destroy" ? nil : object.id
  158.     {:action => next_action, :id =>  id}
  159.   end
  160.  
  161.   #this determines what template to render after a failed crud operation
  162.   #it should be able to be plugged into render
  163.   def action_after_failure
  164.     case params[:action]
  165.     when "destroy"
  166.       next_action = :index
  167.     when "update"
  168.       next_action = :edit
  169.     when "create"
  170.       next_action = :add_child
  171.     else
  172.       next_action = :dots
  173.     end
  174.     #id = params[:action] == "destroy" ? nil : object.id
  175.     {:action => next_action}
  176.   end
  177.  
  178.   #these are the methods that set the flash message after a crud operation
  179.   def success_message
  180.     "#{model_name.titleize} #{params[:action].titleize}d"
  181.   end
  182.  
  183.   def failure_message
  184.     "#{model_name.titleize} Was Not #{params[:action].titleize}d"
  185.   end
  186.  
  187.   #@title is called inside the <title></title> tags in the application layout.
  188.   def set_title
  189.     @title ||= "#{params[:action].to_s.titleize} For #{model_name.titleize}"
  190.   end
  191.  
  192.   # See ActionController::RequestForgeryProtection for details
  193.   # Uncomment the :secret if you're not using the cookie session store
  194.   protect_from_forgery  :secret => '61c027dd20a59a72124a183a36eb6ae1'
  195.   # Return true if a parameter corresponding to the given symbol was posted.
  196.   def param_posted?(symbol)
  197.     request.post? and params[symbol]
  198.   end
  199. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement