Skip to content
Draft
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: 6 additions & 6 deletions lib/search_cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ def search_scope(name, &block)
search_scopes[name] = SearchScope.new(name, self)
search_scopes[name].instance_exec(&block)

send(:define_singleton_method, name) { |query, query_options = {}| search_cop(query, name, query_options) }
send(:define_singleton_method, "unsafe_#{name}") { |query, query_options = {}| unsafe_search_cop(query, name, query_options) }
send(:define_singleton_method, name) { |query, query_options = {}, scope_options = {}| search_cop(query, name, query_options, scope_options) }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The syntax would be cleaner with keyword arguments. Is that possible to do without a breaking change?

send(:define_singleton_method, "unsafe_#{name}") { |query, query_options = {}, scope_options = {}| unsafe_search_cop(query, name, query_options, scope_options) }
end

def search_reflection(scope_name)
search_scopes[scope_name].reflection
end

def search_cop(query, scope_name, query_options)
unsafe_search_cop(query, scope_name, query_options)
def search_cop(query, scope_name, query_options, scope_options)
unsafe_search_cop(query, scope_name, query_options, scope_options)
rescue SearchCop::RuntimeError
respond_to?(:none) ? none : where("1 = 0")
end

def unsafe_search_cop(query, scope_name, query_options)
def unsafe_search_cop(query, scope_name, query_options, scope_options)
return respond_to?(:scoped) ? scoped : all if query.blank?

query_builder = QueryBuilder.new(self, query, search_scopes[scope_name], query_options)

scope = instance_exec(&search_scopes[scope_name].reflection.scope) if search_scopes[scope_name].reflection.scope
scope = instance_exec(scope_options, &search_scopes[scope_name].reflection.scope) if search_scopes[scope_name].reflection.scope
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the whole point here. I also considered doing **scope_options, which might be nicer?

scope ||= eager_load(query_builder.associations) if query_builder.associations.any?

(scope || self).where(query_builder.sql)
Expand Down