Skip to content

Commit

Permalink
Merge pull request #16929 from eduardoj/refactoring/group_find_by_title
Browse files Browse the repository at this point in the history
Don't override `find_by_title!` in groups
  • Loading branch information
danidoni authored Oct 9, 2024
2 parents d13b7c4 + 14add27 commit 9556cfb
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/api/app/controllers/group_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class GroupController < ApplicationController
# raise an exception if authorize has not yet been called.
after_action :verify_authorized, except: %i[index show]

rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

rescue_from Pundit::NotAuthorizedError do |exception|
pundit_action = case exception.query.to_s
when 'index?' then 'list'
Expand Down Expand Up @@ -83,4 +85,10 @@ def command

render_ok
end

private

def record_not_found
render_error status: 404, message: "Couldn't find Group '#{params[:title]}'"
end
end
2 changes: 0 additions & 2 deletions src/api/app/controllers/webui/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ def index

def show
@group = Group.includes(:users).find_by_title(params[:title])

# Group.find_by_title! is self implemented and would raise an 500 error
return if @group

flash[:error] = "Group '#{params[:title]}' does not exist"
Expand Down
6 changes: 0 additions & 6 deletions src/api/app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ class Group < ApplicationRecord

alias_attribute :name, :title

def self.find_by_title!(title)
find_by!(title: title)
rescue ActiveRecord::RecordNotFound => e
raise e, "Couldn't find Group '#{title}'", e.backtrace
end

def update_from_xml(xmlhash, user_session_login:)
with_lock do
self.email = xmlhash.value('email')
Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/models/bs_request/find_for/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
context 'with a not existing group' do
subject { klass.new(group: 'not-existent') }

it { expect { subject.all }.to raise_error(ActiveRecord::RecordNotFound, "Couldn't find Group 'not-existent'") }
it { expect { subject.all }.to raise_error(ActiveRecord::RecordNotFound) }
end

context 'with a group maintainer relationship' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
click_button('Accept')
end

expect(page).to have_text("Couldn't find Group 'unknown group'")
expect(page).to have_text("Couldn't find Group")
end

it 'Add an existing group' do
Expand Down

0 comments on commit 9556cfb

Please sign in to comment.