-
Notifications
You must be signed in to change notification settings - Fork 9
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
Refactor search query with quotes #2491
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is still in draft, and looking good, but just a few stylistic change suggestions:
- For methods whose main job is to return a thing, it often reads better to just have the method named the thing, and avoid the
get_
prefix. Also, parentheses are usually left off when there aren't any params defined., e.g. usedef stopwords
instead ofdef get_stopwords()
. - No need for parentheses unless you have params defined.
- Blocks within
do...end
are indented 2 spaces. - No need to assign a local variable
stopwords
if it's not getting used anywhere else. - Put all these methods into a PORO somewhere, like ./lib/query_to_terms_array.rb.
- Use constructor to take in the parameter, assign it to an (ideally immutable)
attribute. - For immutable attributes, use
attr_reader
, which is just a shortcut for:This is not required, but i like it because it ensures you won't accidentallydef query @query end
change the @query attribute, and it provides a clean separation between the
object's interface (public methods meant to be used by consumer) and data
attributes which, by default are all mutable. - Any method that does not need to be called by a consumer of the object
can be private. In Ruby, any method defined beneathprivate
is private- I like to indent below
private
, but some people don't, because there is no
end
after. - Using
private
does not affect functionality or performance, but it's
good OOP practice because it:- Forces the programmer to think about the object interface, what it
consumers of the object need, and what they don't. - You are only obliged to test the public interface of an object, so
keeping as much private as possible saves you some work.
- Forces the programmer to think about the object interface, what it
- I like to indent below
Putting it all together i imagine would shape up to be something like this:
# ./lib/query_to_terms_array.rb
class QueryToTermsArray
attr_reader :query
def initialize(query)
@query = query
end
def terms_array
# logic!
end
private
def stopwords
Rails.cache('stopwords') do
# get the stopwords from the file
end
end
def quoted_phrases
query.scan(/"([^"]*)"/)
end
def query_minus_quoted_phrases
# logic!
end
end
# And can be used in controllers like this...
terms_array = QueryToTermsArray.new(query).term_array
Adds specs and does a little refactoring of constructor and other methods to (hopefully) make the logic a little more clear and maintainable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Quotes
Refactors
ApplicationHelper::query_to_terms_array
tolib/query_to_terms_array.rb
Better handles quoted searches and edge cases.
terms_array
stopwords
stopwords.txt
quoted_phrases
Array<String>
quoted_terms_arrays
Array<Array<String>>
unquoted_query
String
of original query, minus quoted phrasesunquoted_terms
Array<Array<String>>
of all unquoted terms from query, minus stopwordsstrip_special_chars
"
app/views/catalog/index.html.erb
` `
(backtick) escaping"
and'
charactersraw()
formattingspecs/lib/query_to_terms_array_spec.rb
[]
Closes #2453