Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4724

Show
Ignore:
Timestamp:
08/08/06 18:41:00 (2 years ago)
Author:
rick
Message:

[Plugins] add toggle for admin status

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • applications/plugins

    • Property svn:ignore set to
      .rake_tasks
  • applications/plugins/app/controllers/users_controller.rb

    r4722 r4724  
    11class UsersController < ApplicationController 
     2  before_filter :find_user,      :except => [:index, :new, :create] 
     3  before_filter :login_required, :except => [:index, :new, :create, :show] 
     4 
    25  def index 
    36    @user_pages, @users = paginate :users, :order => 'login', :per_page => 50 
     
    912   
    1013  def show 
    11     @user    = User.find params[:id] 
    1214    @ratings = @user.rated_plugins.find(:all, :order => 'plugins.created_at desc',  
    1315                 :conditions => ['plugins.score = 1 and plugins.user_id != ?', @user.id], :limit => 15) 
     
    2527    flash[:notice] = "Thanks for signing up!" 
    2628  end 
     29   
     30  def admin 
     31    @user.update_attribute(:admin, !@user.admin?) 
     32  end 
     33   
     34  protected 
     35    def authorized? 
     36      current_user.admin? 
     37    end 
     38 
     39    def find_user 
     40      @user = User.find params[:id] 
     41    end 
    2742end 
  • applications/plugins/app/views/sessions/new.rhtml

    r4723 r4724  
    1 <%= start_form_tag sessions_url %> 
     1<%= start_form_tag sessions_path %> 
    22<p><label for="login">Login</label><br/> 
    33<%= text_field_tag 'login' %></p> 
  • applications/plugins/app/views/users/show.rhtml

    r4722 r4724  
    2525  </div> 
    2626</div> 
     27 
     28<% if admin? -%> 
     29<div id="user-admin"> 
     30  <p> 
     31    <label>Admin? <%= check_box_tag :admin, '1', @user.admin?, :onchange => "User.toggleAdmin('#{@user.id}')" %></label> 
     32    <%= image_tag 'progress.gif', :id => 'user-admin-spinner', :style => 'display:none' %> 
     33  </p> 
     34</div> 
     35<% end -%> 
  • applications/plugins/config

    • Property svn:ignore set to
      database.yml
      deploy.rb
  • applications/plugins/config/routes.rb

    r4722 r4724  
    33    plugin.resources :comments, :ratings 
    44  end 
    5   map.resources :users, :sessions, :releases 
     5  map.resources :users, :member => { :admin => :post } 
     6  map.resources :sessions, :releases 
    67 
    78  map.signup '/signup', :controller => 'users',    :action => 'new' 
  • applications/plugins/db

    • Property svn:ignore set to
      *.sqlite2
  • applications/plugins/lib/authenticated_system.rb

    r4722 r4724  
    3232    def authorized? 
    3333       true 
     34    end 
     35 
     36    def admin? 
     37      logged_in? && current_user.admin? 
    3438    end 
    3539 
     
    9498    # available as ActionView helper methods. 
    9599    def self.included(base) 
    96       base.send :helper_method, :current_user, :logged_in? 
     100      base.send :helper_method, :current_user, :logged_in?, :admin? 
    97101    end 
    98102 
  • applications/plugins/log

    • Property svn:ignore set to
      *
  • applications/plugins/public/javascripts/application.js

    r4722 r4724  
    44  } 
    55} 
     6 
     7var User = { 
     8  toggleAdmin: function(user_id) { 
     9    new Ajax.Request('/users/' + user_id + ';admin'); 
     10    $('user-admin-spinner').show(); 
     11  } 
     12} 
  • applications/plugins/test/functional/users_controller_test.rb

    r4722 r4724  
    4040    assert_equal users(:quentin), assigns(:user) 
    4141    assert_no_tag :tag => 'user' 
     42    assert_no_tag 'div', :attributes => { :id => 'user-admin' } 
     43  end 
     44   
     45  def test_should_show_admin_panel 
     46    login_as :quentin 
     47    get :show, :id => users(:quentin).id 
     48    assert_response :success 
     49    assert_tag 'div', :attributes => { :id => 'user-admin' } 
    4250  end 
    4351 
     
    8997    end 
    9098  end 
     99   
     100  def test_should_require_admin_to_change_admin_status 
     101    [nil, :aaron].each do |u| 
     102      assert_requires_login u do |c| 
     103        c.post :admin, :id => users(:quentin).id 
     104      end 
     105    end 
     106  end 
     107   
     108  def test_should_disable_admin_status 
     109    login_as :quentin 
     110    post :admin, :id => users(:quentin).id 
     111    assert !users(:quentin).reload.admin? 
     112  end 
     113   
     114  def test_should_enable_admin_status 
     115    login_as :quentin 
     116    post :admin, :id => users(:aaron).id 
     117    assert users(:aaron).reload.admin? 
     118  end 
    91119 
    92120  protected 
  • applications/plugins/vendor

    • Property svn:ignore set to
      rails