Passed
Push — master ( 02b342...4fc171 )
by Jesus
04:50
created

AddCustomRoles.up()   B

Complexity

Conditions 1

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 70
rs 7.9818

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# frozen_string_literal: true
2
3
class AddCustomRoles < ActiveRecord::Migration[5.2]
4
  def up
5
    super_admin_id = -1
6
    user_id = -1
7
    admin_id = -1
8
    denied_id = -1
9
    pending_id = -1
10
11
    old_roles = ActiveRecord::Base.connection.execute("select * from roles")
12
13
    # Determine what ids corresponded to what roles in the old table
14
    old_roles.each do |role|
15
      if role["name"] == "super_admin"
16
        super_admin_id = role["id"]
17
      elsif role["name"] == "user"
18
        user_id = role["id"]
19
      elsif role["name"] == "admin"
20
        admin_id = role["id"]
21
      elsif role["name"] == "denied"
22
        denied_id = role["id"]
23
      elsif role["name"] == "pending"
24
        pending_id = role["id"]
25
      end
26
    end
27
28
    # Replace Rolify's table with our own
29
    drop_table :roles
30
31
    create_table(:roles) do |t|
32
      t.string :name
33
      t.integer :priority, default: 9999
34
      t.boolean :can_create_rooms, default: false
35
      t.boolean :send_promoted_email, default: false
36
      t.boolean :send_demoted_email, default: false
37
      t.boolean :can_edit_site_settings, default: false
38
      t.boolean :can_edit_roles, default: false
39
      t.boolean :can_manage_users, default: false
40
      t.string  :colour
41
      t.string :provider
42
43
      t.timestamps
44
    end
45
46
    add_index(:roles, :name)
47
    add_index(:roles, [:name, :provider], unique: true)
48
49
    # Look at all the old role assignments and and for each role create a new role
50
    # that is scoped to the provider
51
    old_assignments = ActiveRecord::Base.connection.execute("select * from users_roles")
52
    new_assignments = []
53
54
    old_assignments.each do |assignment|
55
      user = User.find(assignment["user_id"])
56
      new_assignment = { "user_id" => assignment["user_id"] }
57
      if assignment["role_id"] == super_admin_id
58
        new_assignment["new_role_id"] = generate_scoped_role(user, "super_admin")
59
      elsif assignment["role_id"] == user_id
60
        new_assignment["new_role_id"] = generate_scoped_role(user, "user")
61
      elsif assignment["role_id"] == admin_id
62
        new_assignment["new_role_id"] = generate_scoped_role(user, "admin")
63
      elsif assignment["role_id"] == denied_id
64
        new_assignment["new_role_id"] = generate_scoped_role(user, "denied")
65
      elsif assignment["role_id"] == pending_id
66
        new_assignment["new_role_id"] = generate_scoped_role(user, "pending")
67
      end
68
69
      new_assignments << new_assignment
70
    end
71
72
    assign_new_users(new_assignments)
73
  end
74
75
  def generate_scoped_role(user, role_name)
76
    provider = Rails.configuration.loadbalanced_configuration ? user.provider : 'greenlight'
77
    new_role = Role.find_by(name: role_name, provider: provider)
78
79
    if new_role.nil?
80
      Role.create_default_roles(provider)
81
82
      new_role = Role.find_by(name: role_name, provider: provider)
83
    end
84
85
    new_role.id
86
  end
87
88
  def assign_new_users(new_assignments)
89
    # Delete the old assignments
90
    ActiveRecord::Base.connection.execute("DELETE FROM users_roles")
91
    # Add the role assignments to the new roles
92
    new_assignments.each do |assignment|
93
      if assignment['new_role_id']
94
        ActiveRecord::Base.connection.execute("INSERT INTO users_roles (user_id, role_id)" \
95
          " VALUES (#{assignment['user_id']}, #{assignment['new_role_id']})")
96
      end
97
    end
98
  end
99
100
  def down
101
    drop_table :roles
102
103
    create_table(:roles) do |t|
104
      t.string :name
105
      t.references :resource, polymorphic: true
106
107
      t.timestamps
108
    end
109
  end
110
end
111