Skip to content

Commit

Permalink
Merge pull request #172 from openstax/make-user-admin
Browse files Browse the repository at this point in the history
Add "make them admin" link next to each user in the admin console
  • Loading branch information
jpslav committed Apr 14, 2015
2 parents 4ec4294 + 6144b4d commit 583a276
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 10 additions & 2 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Admin
class UsersController < BaseController

before_filter :get_user, only: [:show, :edit, :update, :destroy, :become]
before_filter :get_user, only: [:show, :edit, :update, :destroy, :become, :make_admin]

def index
handle_with(UsersSearch,
Expand All @@ -28,11 +28,19 @@ def become
redirect_to request.referrer
end

def make_admin
if @user.update_attribute(:is_administrator, true)
redirect_to request.referrer, notice: 'User successfully updated.'
else
redirect_to request.referrer, alert: 'Unable to update user.'
end
end

protected

def get_user
@user = User.find(params[:id])
end

end
end
end
23 changes: 18 additions & 5 deletions app/views/admin/users/search.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<%
users = @handler_result.outputs[:items]

contents = osu.action_list(
contents = osu.action_list(
records: users,
list: {
headings: ['Username', 'First Name', 'Last Name', ''],
widths: ['25%', '25%', '25%', '25%'],
headings: ['Username', 'First Name', 'Last Name', '', 'Is Admin?'],
widths: ['20%', '20%', '20%', '20%', '20%'],
data_procs:
[
Proc.new { |user| user.username },
Expand All @@ -18,12 +18,25 @@
link_to 'Sign in as',
become_admin_user_path(user),
method: :post
}
},
Proc.new { |user|
if user.is_administrator
'Yes'
else
link_to 'Make them admin',
make_admin_admin_user_path(user),
method: :post,
remote: true,
class: 'make-admin'
end
},
]
}
) %>


$("#search-results-list").html("<%= j(contents) %>");
$('#search-results-list').prepend("<%= j(render 'admin/users/search_pagination') %>");
<% end %>
// reload the users table after clicking on the make admin link
$('.make-admin').bind('ajax:complete', function() { $('.tab-pane.active form').submit(); });
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@

resources :users, only: [:index, :show, :update, :edit] do
post 'become', on: :member
post 'make_admin', on: :member
end
end

Expand Down

0 comments on commit 583a276

Please sign in to comment.