AddCustomRoles.up()   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
c 2
b 1
f 1
dl 0
loc 75
rs 7.8181

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
      begin
56
        user = User.find(assignment["user_id"])
57
      rescue
58
        next
59
      end
60
61
      new_assignment = { "user_id" => assignment["user_id"] }
62
      if assignment["role_id"] == super_admin_id
63
        new_assignment["new_role_id"] = generate_scoped_role(user, "super_admin")
64
      elsif assignment["role_id"] == user_id
65
        new_assignment["new_role_id"] = generate_scoped_role(user, "user")
66
      elsif assignment["role_id"] == admin_id
67
        new_assignment["new_role_id"] = generate_scoped_role(user, "admin")
68
      elsif assignment["role_id"] == denied_id
69
        new_assignment["new_role_id"] = generate_scoped_role(user, "denied")
70
      elsif assignment["role_id"] == pending_id
71
        new_assignment["new_role_id"] = generate_scoped_role(user, "pending")
72
      end
73
74
      new_assignments << new_assignment
75
    end
76
77
    assign_new_users(new_assignments)
78
  end
79
80
  def generate_scoped_role(user, role_name)
81
    provider = Rails.configuration.loadbalanced_configuration ? user.provider : 'greenlight'
82
    new_role = Role.find_by(name: role_name, provider: provider)
83
84
    if new_role.nil?
85
      Role.create_default_roles(provider)
86
87
      new_role = Role.find_by(name: role_name, provider: provider)
88
    end
89
90
    new_role.id
91
  end
92
93
  def assign_new_users(new_assignments)
94
    # Delete the old assignments
95
    ActiveRecord::Base.connection.execute("DELETE FROM users_roles")
96
    # Add the role assignments to the new roles
97
    new_assignments.each do |assignment|
98
      if assignment['new_role_id']
99
        ActiveRecord::Base.connection.execute("INSERT INTO users_roles (user_id, role_id)" \
100
          " VALUES (#{assignment['user_id']}, #{assignment['new_role_id']})")
101
      end
102
    end
103
  end
104
105
  def down
106
    drop_table :roles
107
108
    create_table(:roles) do |t|
109
      t.string :name
110
      t.references :resource, polymorphic: true
111
112
      t.timestamps
113
    end
114
  end
115
end
116