Changeset 11

Show
Ignore:
Timestamp:
08/29/2006 08:57:16 AM (2 years ago)
Author:
matthew
Message:

Reduce direct mentions of session[:user] in the controller (references #9).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/auto-admin/lib/auto_admin_controller.rb

    r5 r11  
    3333 
    3434    valid_user = false 
    35     if session[:user] 
    36       if permit_user_to_access_admin( session[:user]
     35    if user 
     36      if permit_user_to_access_admin( user
    3737        valid_user = true 
    3838      else 
     
    5151  private :permit_user_to_access_admin 
    5252 
     53  def user_history_includes 
     54    :user 
     55  end 
     56  def user_history_identity 
     57    { :user_id => (user && user.id) } 
     58  end 
     59  def user_history_items(num=10) 
     60    conditions = [] 
     61    condition_values = [] 
     62    user_history_identity.each {|k,v| conditions << "#{k} = ?"; condition_values << v } 
     63    AdminHistory.find( :all,  
     64      :conditions => [conditions.join(' AND '), *condition_values],  
     65      :order => 'created_at DESC', :limit => num ) 
     66  end 
     67  private :user_history_items 
     68 
    5369  def index 
    5470    @no_crumbs = true 
    55     @history_items = AdminHistory.find( :all, :conditions => ['user_id = ?', user.id], :order => 'created_at DESC', :limit => 10 ) if has_history? 
     71    @history_items = user_history_items if has_history? 
    5672  end 
    5773  def login 
     
    152168 
    153169      if has_history? 
    154         history = { :user_id => session[:user].id, :object_label => @object.to_label, :model => params[:model], :obj_id => @object.id } 
     170        history = { :object_label => @object.to_label, :model => params[:model], :obj_id => @object.id } 
     171        history.update user_history_identity 
    155172        if params[:id] 
    156173          history.update :change => 'edit', :description => 'Record modified' 
     
    178195  def history 
    179196    @object = params[:id] ? model.find( params[:id] ) : model.new 
    180     @histories = AdminHistory.find :all, :conditions => ['model = ? AND obj_id = ?', params[:model], params[:id]], :order => 'admin_histories.created_at DESC', :limit => 50, :include => [:user
     197    @histories = AdminHistory.find :all, :conditions => ['model = ? AND obj_id = ?', params[:model], params[:id]], :order => 'admin_histories.created_at DESC', :limit => 50, :include => [*user_history_includes
    181198  end 
    182199 
     
    186203    object = model.find( params[:id] ) 
    187204    label = @object.to_label 
    188     hist = AdminHistory.new( :user_id => session[:user].id, :object_label => @object.to_label, :model => params[:model], :obj_id => params[:id], :change => 'delete', :description => 'Record deleted' ) if has_history? 
     205    if has_history? 
     206      history_hash = { :object_label => @object.to_label, :model => params[:model], :obj_id => params[:id], :change => 'delete', :description => 'Record deleted' } 
     207      history_obj = AdminHistory.new( history_hash ) 
     208    end 
    189209    object.destroy 
    190210    flash[:notice] = "The #{human_model.downcase} \"#{object.to_label}\" was deleted successfully." 
    191     hist.save! if hist 
     211    history_obj.save! if history_obj 
    192212    redirect_to list_page_for_current 
    193213  end