Skip to content

Commit

Permalink
update drupal_user.rb to update status of User record (#2157)
Browse files Browse the repository at this point in the history
* update drupal_user.rb to update user.status

fixes #2156

* minor changes

* minor changes

* minor changes

* changes

* added tests
  • Loading branch information
ViditChitkara authored and jywarren committed Feb 2, 2018
1 parent 9b5354b commit 5ed7c12
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def new

def create
@user = User.new(params[:user])
@user.status = 1
using_recaptcha = !params[:spamaway] && Rails.env == "production"
recaptcha = verify_recaptcha(model: @user) if using_recaptcha
@spamaway = Spamaway.new(params[:spamaway]) unless using_recaptcha
Expand Down
10 changes: 10 additions & 0 deletions app/models/drupal_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,23 @@ def role
def moderate
self.status = 5
save
update_user_status(5)
# user is logged out next time they access current_user in a controller; see application controller
self
end

def unmoderate
self.status = 1
save
update_user_status(1)
self
end

def ban
self.status = 0
decrease_likes_banned
save
update_user_status(0)
# user is logged out next time they access current_user in a controller; see application controller
self
end
Expand All @@ -69,13 +72,20 @@ def unban
self.status = 1
increase_likes_unbanned
save
update_user_status(1)
self
end

def email
mail
end

def update_user_status(status)
u = self.user
u.status = status
u.save!
end

def first_time_poster
user.first_time_poster
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def validate(record)

class User < ActiveRecord::Base
self.table_name = 'rusers'
attr_accessible :username, :email, :password, :password_confirmation, :openid_identifier, :key, :photo, :photo_file_name, :bio
attr_accessible :username, :email, :password, :password_confirmation, :openid_identifier, :key, :photo, :photo_file_name, :bio, :status
alias_attribute :name, :username

acts_as_authentic do |c|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class SyncUserStatusWithDrupalUserStatus < ActiveRecord::Migration
def up
DrupalUser.all.each do |du|
user = du.user
user.status = du.status
user.save({})
end
end

def down
end
end
2 changes: 1 addition & 1 deletion db/schema.rb.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180119204200) do
ActiveRecord::Schema.define(version: 20180128162224) do

create_table "answer_selections", force: true do |t|
t.integer "user_id"
Expand Down
18 changes: 18 additions & 0 deletions test/unit/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,22 @@ class UserTest < ActiveSupport::TestCase
assert_equal 1, user.errors[:email].count
end

test 'user status changes when drupal user is banned or unbanned' do
drupal_user = drupal_users(:bob)
assert_equal 1, drupal_user.user.status
drupal_user.ban
assert_equal 0, drupal_user.user.status
drupal_user.unban
assert_equal 1, drupal_user.user.status
end

test 'user status changes when drupal user is moderated or unmoderated' do
drupal_user = drupal_users(:bob)
assert_equal 1, drupal_user.user.status
drupal_user.moderate
assert_equal 5, drupal_user.user.status
drupal_user.unmoderate
assert_equal 1, drupal_user.user.status
end

end

0 comments on commit 5ed7c12

Please sign in to comment.