From 3977fd27618d606d64c1c237567d19429997a51b Mon Sep 17 00:00:00 2001 From: Jared Pace Date: Fri, 13 Aug 2010 09:15:37 -0400 Subject: [PATCH] Add full management controls for users. --- README.md | 10 +++++++++- app/controllers/application_controller.rb | 2 +- app/controllers/users_controller.rb | 31 ++++++++++++++++++++++++++++++- app/models/user.rb | 1 - app/views/users/_fields.html.haml | 19 +++++++++++++++++++ app/views/users/edit.html.haml | 7 +++++++ app/views/users/new.html.haml | 7 +++++++ spec/controllers/deploys_controller_spec.rb | 2 +- spec/controllers/users_controller_spec.rb | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 4 ++++ 10 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 app/views/users/_fields.html.haml create mode 100644 app/views/users/edit.html.haml create mode 100644 app/views/users/new.html.haml diff --git a/README.md b/README.md index 2779028..ba270d8 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,12 @@ Installation 1. Install MongoDB 2. Install & Run Bundler -3. Seed DB - rake db:seed \ No newline at end of file +3. Seed DB - rake db:seed + +TODO +---- + +Add capistrano +Add form.error_messages +Add a deployment view +Add ability for watchers to be configured for types of notifications they should receive \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e097175..92fffdb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base protected - def authenticate_admin! + def require_admin! redirect_to(root_path) and return(false) unless user_signed_in? && current_user.admin? end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5438852..b386f5a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,7 +1,7 @@ class UsersController < ApplicationController respond_to :html - before_filter :authenticate_admin! + before_filter :require_admin! def index @users = User.paginate(:page => params[:page]) @@ -12,18 +12,47 @@ class UsersController < ApplicationController end def new + @user = User.new end def edit + @user = User.find(params[:id]) end def create + @user = User.new(params[:user]) + + if @user.save + flash[:success] = "#{@user.name} is now part of the team. Be sure to add them as a project watcher." + redirect_to user_path(@user) + else + render :new + end end def update + # Devise Hack + if params[:user][:password].blank? && params[:user][:password_confirmation].blank? + params[:user].delete(:password) + params[:user].delete(:password_confirmation) + end + + @user = User.find(params[:id]) + + if @user.update_attributes(params[:user]) + flash[:success] = "#{@user.name}'s information was successfully updated" + redirect_to user_path(@user) + else + render :edit + end end def destroy + @user = User.find(params[:id]) + @user.destroy + + flash[:notice] = "That's sad. #{@user.name} is no longer part of your team." + redirect_to users_path end end diff --git a/app/models/user.rb b/app/models/user.rb index f1351d6..b469252 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,6 @@ class User field :name field :admin, :type => Boolean, :default => false - key :name validates_presence_of :name diff --git a/app/views/users/_fields.html.haml b/app/views/users/_fields.html.haml new file mode 100644 index 0000000..9de8c06 --- /dev/null +++ b/app/views/users/_fields.html.haml @@ -0,0 +1,19 @@ +.required + = f.label :name + = f.text_field :name + +.required + = f.label :email + = f.text_field :email + +.required + = f.label :password + = f.password_field :password + +.required + = f.label :password_confirmation + = f.password_field :password_confirmation + +.checkbox + = f.check_box :admin + = f.label :admin, 'Admin?' \ No newline at end of file diff --git a/app/views/users/edit.html.haml b/app/views/users/edit.html.haml new file mode 100644 index 0000000..089ff7f --- /dev/null +++ b/app/views/users/edit.html.haml @@ -0,0 +1,7 @@ +- content_for :title, "Edit #{@user.name}" + += form_for @user do |f| + = @user.errors.full_messages.to_sentence + = render 'fields', :f => f + + %div= f.submit 'Update' \ No newline at end of file diff --git a/app/views/users/new.html.haml b/app/views/users/new.html.haml new file mode 100644 index 0000000..d5aa2e9 --- /dev/null +++ b/app/views/users/new.html.haml @@ -0,0 +1,7 @@ +- content_for :title, 'New User' + += form_for @user do |f| + + = render 'fields', :f => f + + %div= f.submit 'Add' \ No newline at end of file diff --git a/spec/controllers/deploys_controller_spec.rb b/spec/controllers/deploys_controller_spec.rb index 7ab18ca..fe77f6f 100644 --- a/spec/controllers/deploys_controller_spec.rb +++ b/spec/controllers/deploys_controller_spec.rb @@ -30,7 +30,7 @@ describe DeploysController do post :create, :deploy => @params, :api_key => 'APIKEY' end - it 'sends an email notification', :focused => true do + it 'sends an email notification' do post :create, :deploy => @params, :api_key => 'APIKEY' email = ActionMailer::Base.deliveries.last email.to.should include(@app.watchers.first.email) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index dc23f86..23d17ec 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -26,5 +26,105 @@ describe UsersController do assigns(:user).should == user end end + + context "GET /users/new" do + it 'assigns a new user' do + get :new + assigns(:user).should be_a(User) + assigns(:user).should be_new_record + end + end + + context "GET /users/:id/edit" do + it 'finds the user' do + user = Factory(:user) + get :edit, :id => user.id + assigns(:user).should == user + end + end + + context "POST /users" do + context "when the create is successful" do + before do + @user = Factory(:user) + User.should_receive(:new).and_return(@user) + @user.should_receive(:save).and_return(true) + end + + it "sets a message to display" do + post :create + request.flash[:success].should include('part of the team') + end + + it "redirects to the user's page" do + post :create + response.should redirect_to(user_path(@user)) + end + end + + context "when the create is unsuccessful" do + before do + @user = Factory(:user) + User.should_receive(:new).and_return(@user) + @user.should_receive(:save).and_return(false) + end + + it "renders the new page" do + post :create + response.should render_template(:new) + end + end + end + + context "PUT /users/:id" do + context "when the update is successful" do + before do + @user = Factory(:user) + end + + it "sets a message to display" do + put :update, :id => @user.to_param, :user => {:name => 'Kermit'} + request.flash[:success].should include('updated') + end + + it "redirects to the user's page" do + put :update, :id => @user.to_param, :user => {:name => 'Kermit'} + response.should redirect_to(user_path(@user)) + end + end + + context "when the update is unsuccessful" do + before do + @user = Factory(:user) + end + + it "renders the edit page" do + put :update, :id => @user.to_param, :user => {:name => nil} + response.should render_template(:edit) + end + end + end + + context "DELETE /users/:id" do + before do + @user = Factory(:user) + end + + it "destroys the user" do + delete :destroy, :id => @user.id + User.where(:id => @user.id).first.should be_nil + end + + it "redirects to the users index page" do + delete :destroy, :id => @user.id + response.should redirect_to(users_path) + end + + it "sets a message to display" do + delete :destroy, :id => @user.id + request.flash[:notice].should include('no longer part of your team') + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9960b1d..8793dde 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,10 @@ RSpec.configure do |config| config.mock_with :rspec config.include Devise::TestHelpers, :type => :controller + config.filter_run :focused => true + config.run_all_when_everything_filtered = true + config.alias_example_to :fit, :focused => true + config.before(:each) do DatabaseCleaner.orm = "mongoid" DatabaseCleaner.strategy = :truncation -- libgit2 0.21.2