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

raise a more informative NoMethodError for missing custom queries #873

Merged
merged 2 commits into from
Sep 20, 2021
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: 11 additions & 1 deletion lib/valkyrie/persistence/custom_query_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ def register_query_handler(query_handler)
end

def method_missing(meth_name, *args, &block)
query_handler = find_query_handler(meth_name).new(query_service: query_service)
handler_class =
find_query_handler(meth_name) ||
raise(NoMethodError, "Custom query #{meth_name} is not registered. The registered queries are: #{queries}")

query_handler = handler_class.new(query_service: query_service)
return super unless query_handler
query_handler.__send__(meth_name, *args, &block)
end
Expand All @@ -59,5 +63,11 @@ def find_query_handler(method)
def respond_to_missing?(meth_name, _args)
find_query_handler(meth_name).present?
end

private

def queries
query_handlers.map(&:queries).flatten
end
end
end
7 changes: 7 additions & 0 deletions lib/valkyrie/specs/shared_specs/queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,13 @@ class Valkyrie::Specs::ChildWithOrderedParents < Valkyrie::Resource
end
end

describe ".custom_queries" do
it "raises NoMethodError when the custom query does not exist" do
expect(query_service.custom_queries).not_to respond_to :very_fake_query
expect { query_service.custom_queries.very_fake_query }.to raise_error(NoMethodError)
end
end

describe ".register_query_handler" do
it "can register a query handler" do
class QueryHandler
Expand Down