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

GRN2-xx: Home rooms are created for all users with role if can_create_rooms is enabled #1694

Merged
merged 3 commits into from
May 28, 2020
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
12 changes: 12 additions & 0 deletions app/controllers/concerns/rolify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ def update_permissions(role)
role.update(colour: permission_params[:colour])
role.update_all_role_permissions(permission_params)

# Create home rooms for all users with this role if users with this role are now able to create rooms
create_home_rooms(role.name) if !role.get_permission("can_create_rooms") && permission_params["can_create_rooms"] == "true"

role.save!
end

private

# Create home rooms for users since they are now able to create rooms
def create_home_rooms(role_name)
User.with_role(role_name).each do |user|
user.create_home_room if user.main_room.nil?
end
end
end
10 changes: 5 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ def self.without_role(role)
User.includes(:role).where.not(roles: { name: role })
end

def create_home_room
room = Room.create!(owner: self, name: I18n.t("home_room"))
update_attributes(main_room: room)
end

private

# Destory a users rooms when they are removed.
Expand Down Expand Up @@ -227,11 +232,6 @@ def check_if_email_can_be_blank
end
end

def create_home_room
room = Room.create!(owner: self, name: I18n.t("home_room"))
update_attributes(main_room: room)
end

def role_provider
Rails.configuration.loadbalanced_configuration ? provider : "greenlight"
end
Expand Down
18 changes: 18 additions & 0 deletions spec/controllers/admins_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,24 @@
expect(new_role.get_permission("send_promoted_email")).to eq(false)
expect(response).to redirect_to admin_roles_path(selected_role: new_role.id)
end

it "creates the users home room if can_create_rooms is enabled" do
new_role = Role.create(name: "test2", priority: 2, provider: "provider1")
new_role.update_permission("can_create_rooms", "false")

@request.session[:user_id] = @admin.id

new_user = create(:user, role: new_role)
expect(new_user.role.get_permission("can_create_rooms")).to eq(false)
expect(new_user.main_room).to be_nil

patch :update_role, params: { role_id: new_role.id, role: { name: "test", can_create_rooms: true,
colour: "#45434" } }

new_user.reload
expect(new_user.role.get_permission("can_create_rooms")).to eq(true)
expect(new_user.main_room).not_to be_nil
end
end

context "DELETE delete_role" do
Expand Down