Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update drupal_user.rb to update status of User record #2157

Merged
merged 6 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -142,4 +142,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