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

Pass all 4 parameters (message, model_name, query_field and id) to ActiveRecord::RecordNotFound.new when raising the exception. #890

Merged
merged 3 commits into from
Nov 22, 2018
Merged
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
14 changes: 12 additions & 2 deletions lib/friendly_id/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def find(*args)
return super if args.count != 1 || id.unfriendly_id?
first_by_friendly_id(id).tap {|result| return result unless result.nil?}
return super if potential_primary_key?(id)
raise ActiveRecord::RecordNotFound, "can't find record with friendly id: #{id.inspect}"
raise_not_found_exception id

end

# Returns true if a record with the given id exists.
Expand All @@ -34,7 +35,7 @@ def exists?(conditions = :none)
# `find`.
# @raise ActiveRecord::RecordNotFound
def find_by_friendly_id(id)
first_by_friendly_id(id) or raise ActiveRecord::RecordNotFound, "can't find record with friendly id: #{id.inspect}"
first_by_friendly_id(id) or raise raise_not_found_exception(id)
end

def exists_by_friendly_id?(id)
Expand All @@ -61,5 +62,14 @@ def first_by_friendly_id(id)
find_by(friendly_id_config.query_field => id)
end

def raise_not_found_exception(id)
message = "can't find record with friendly id: #{id.inspect}"
if ActiveRecord.version < Gem::Version.create('5.0') then
raise ActiveRecord::RecordNotFound.new(message)
else
raise ActiveRecord::RecordNotFound.new(message, name, friendly_id_config.query_field, id)
end
end

end
end