Conditions | 1 |
Total Lines | 75 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
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:
If many parameters/temporary variables are present:
1 | # frozen_string_literal: true |
||
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 | |||
116 |