User deletion

This commit is contained in:
Gopesh Tulsyan 2014-08-10 11:41:35 +05:30
parent a2b8806e35
commit 6890802a78
8 changed files with 60 additions and 9 deletions

View file

@ -30,6 +30,7 @@ module Admin
def edit; end def edit; end
def destroy def destroy
sign_out @user
@user.destroy @user.destroy
redirect_to admin_users_path, notice: 'User got deleted' redirect_to admin_users_path, notice: 'User got deleted'
end end

View file

@ -54,6 +54,12 @@ class RegistrationsController < Devise::RegistrationsController
end end
end end
def destroy
sign_out current_user
current_user.destroy
redirect_to root_path
end
protected protected
def after_update_path_for(resource) def after_update_path_for(resource)

View file

@ -205,7 +205,6 @@ class Event < ActiveRecord::Base
end end
alert alert
end end
private private
def abstract_limit def abstract_limit

View file

@ -5,4 +5,10 @@ class EventUser < ActiveRecord::Base
belongs_to :event belongs_to :event
belongs_to :user belongs_to :user
def self.create_deleted_eventuser(user,event)
event_role = event.event_users.where(user: user).first.event_role
event.event_users.where(user: user).destroy_all
EventUser.create(user: User.find_by(email: 'deleted@localhost.osem'), event: event, event_role: event_role)
end
end end

View file

@ -122,6 +122,21 @@ class User < ActiveRecord::Base
end end
end end
def destroy
if self.events.blank?
super
else
self.events.each do |event|
if event.start_time.nil? || event.start_time > DateTime.now
event.destroy
else
EventUser.create_deleted_eventuser(self,event)
end
end
super
end
end
private private
def biography_limit def biography_limit

View file

@ -53,4 +53,14 @@
disabled: true, class: 'btn btn-primary disabled btn-danger', role: 'button' disabled: true, class: 'btn btn-primary disabled btn-danger', role: 'button'
- else - else
= link_to 'Delete',admin_user_path(user), method: :delete, = link_to 'Delete',admin_user_path(user), method: :delete,
data: { confirm: 'Are you sure ?' }, class: 'btn btn-primary btn-danger' data: { confirm: 'Are you sure ?' }, class: 'btn btn-primary btn-danger', id: "user-delete-#{user.id}"
:javascript
$(document).ready(function() {
$('#users').dataTable( {
"bPaginate": false,
"bLengthChange": false
} );
} );
>>>>>>> User deletion

View file

@ -33,3 +33,4 @@
= f.input :current_password, input_html: {autocomplete: 'off'}, = f.input :current_password, input_html: {autocomplete: 'off'},
hint: '(we need your current password to confirm password or email changes)' hint: '(we need your current password to confirm password or email changes)'
= f.action :submit, as: :button, label: 'Update', button_html: {class: 'btn btn-primary'} = f.action :submit, as: :button, label: 'Update', button_html: {class: 'btn btn-primary'}
= link_to 'Delete', registration_path(resource_name), method: :delete , data: { confirm: 'Are you sure ?'},class: "btn btn-primary btn-danger"

View file

@ -5,15 +5,28 @@ feature User do
shared_examples 'admin ability' do shared_examples 'admin ability' do
scenario 'deletes a user', feature: true, js: true do scenario 'deletes a user', feature: true, js: true do
sign_in(create(:admin)) sign_in(create(:admin))
@user = create(:user)
visit admin_users_path visit admin_users_path
expected_count = User.count - 1 expected_count = User.count - 1
page.all('btn btn-primary btn-danger') do find("#user-delete-#{@user.id}").click
click_link 'Delete' page.evaluate_script('window.confirm = function() { return true; }')
page.evaluate_script('window.confirm = function() { return true; }') expect(flash).to eq('User got deleted')
page.click('OK') expect(User.count).to eq(expected_count)
expect(flash).to eq('User got deleted') sign_out
expect(User.count).to eq(expected_count) end
end scenario 'deletes a user with scheduled events', feature: true, js: true do
sign_in(create(:admin))
@user = create(:user)
deleted_user = create(:user, email: 'deleted@localhost.osem', name: 'User deleted', biography: 'Data is no longer available for deleted user.')
@user.events << create(:event, start_time: DateTime.now)
event = @user.events.first
visit admin_users_path
expected_count = User.count - 1
find("#user-delete-#{@user.id}").click
page.evaluate_script('window.confirm = function() { return true; }')
expect(flash).to eq('User got deleted')
expect(User.count).to eq(expected_count)
expect(event.event_users.map{|x| User.find(x.user_id)}).to include(deleted_user)
sign_out sign_out
end end
end end