diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb new file mode 100644 index 0000000..d6ac2c0 --- /dev/null +++ b/app/controllers/admin/users_controller.rb @@ -0,0 +1,32 @@ +module Admin + class UsersController < ApplicationController + before_action :authenticate_user! + before_action :admin_only! + + # GET /users/orphans + def orphans + @users = User.orphans + end + + def update_groups + additions = 0 + + params.each_pair { |k, group_id| + next if !k.start_with?('orphan-') + + _, id = k.split('-', 2) + user = User.orphans.find_by_id(id) + next if !user + + group = Group.find_by_id(group_id) + next if !group + + user.groups << group + additions += 1 + } + + redirect_to :back, notice: "#{additions} users were assigned to groups" + end + + end +end diff --git a/app/models/user.rb b/app/models/user.rb index f2bc444..0fc6268 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,7 +1,9 @@ class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :memberships has_many :groups, through: :memberships + + scope :orphans, -> { includes(:memberships).where(:memberships => { user_id: nil }) } end diff --git a/app/views/admin/users/orphans.html.erb b/app/views/admin/users/orphans.html.erb new file mode 100644 index 0000000..c08cd7d --- /dev/null +++ b/app/views/admin/users/orphans.html.erb @@ -0,0 +1,10 @@ +
+ <%= bootstrap_form_tag(url: update_groups_admin_users_path, method: 'PUT', layout: :horizontal, label_col: 'col-sm-2', control_col: 'col-sm-4') do |f| %> + <% @users.each do |u| %> + <%= f.collection_select "orphan-#{u.id}", Group.all, :id, :name, label: u.email, prompt: 'Assign to group' %> + <% end %> + <%= f.submit 'Add', class: 'btn btn-primary' %> + <% end %> +
+