Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

AO3-5262 AO3-3576: Move query cleaning out of the works controller, fix sa"m/f"rodo #3189

Merged
merged 3 commits into from Dec 1, 2017

Conversation

Projects
None yet
3 participants
Owner

elzj commented Nov 30, 2017

Issue

https://otwarchive.atlassian.net/browse/AO3-5262
https://otwarchive.atlassian.net/browse/AO3-3576

Purpose

Extracts the query cleanup code from the works controller into a separate class. Also ended up fixing the weird category-escaping-overreach bug.

Testing

Behavior should be the same, other than the category tag escaping, which should now be case-insensitive, but should only match when the letters are not part of larger words.

@@ -0,0 +1,100 @@
+# This is currently being tested without loading most of Rails
@houndci-bot

houndci-bot Nov 30, 2017

Missing magic comment # frozen_string_literal: true.

+# This is currently being tested without loading most of Rails
+# so beware of changes that rely on other classes or Rails methods
+class QueryCleaner
+
@houndci-bot

houndci-bot Nov 30, 2017

Extra empty line detected at class body beginning.

+ attr_reader :params
+
+ SORT_OPTIONS = [
+ ['Author', 'authors_to_sort_on'],
@houndci-bot

houndci-bot Nov 30, 2017

Use %w or %W for an array of words.

+
+ SORT_OPTIONS = [
+ ['Author', 'authors_to_sort_on'],
+ ['Title', 'title_to_sort_on'],
@houndci-bot

houndci-bot Nov 30, 2017

Use %w or %W for an array of words.

+ ['Date Posted', 'created_at'],
+ ['Date Updated', 'revised_at'],
+ ['Word Count', 'word_count'],
+ ['Hits', 'hits'],
@houndci-bot

houndci-bot Nov 30, 2017

Use %w or %W for an array of words.

+ ['Date Updated', 'revised_at'],
+ ['Word Count', 'word_count'],
+ ['Hits', 'hits'],
+ ['Kudos', 'kudos_count'],
@houndci-bot

houndci-bot Nov 30, 2017

Use %w or %W for an array of words.

+ ['Word Count', 'word_count'],
+ ['Hits', 'hits'],
+ ['Kudos', 'kudos_count'],
+ ['Comments', 'comments_count'],
@houndci-bot

houndci-bot Nov 30, 2017

Use %w or %W for an array of words.

+ ['Hits', 'hits'],
+ ['Kudos', 'kudos_count'],
+ ['Comments', 'comments_count'],
+ ['Bookmarks', 'bookmarks_count']
@houndci-bot

houndci-bot Nov 30, 2017

Use %w or %W for an array of words.

+ # turn word_count or word count or words into just "word" eg
+ sortby = m[2].gsub(/\s*_?count/, '').singularize
+
+ sort_column = SORT_OPTIONS.find { |opt, _| opt =~ /#{sortby}/i }&.last
@houndci-bot

houndci-bot Nov 30, 2017

Prefer detect over find.

+ end
+
+ def sort_direction(sortdir)
+ if sortdir == '>' || sortdir == 'ascending'
@houndci-bot

houndci-bot Nov 30, 2017

Avoid comparing a variable with multiple items in a conditional, use Array#include? instead.

+ def sort_direction(sortdir)
+ if sortdir == '>' || sortdir == 'ascending'
+ 'asc'
+ elsif sortdir == '<' || sortdir == 'descending'
@houndci-bot

houndci-bot Nov 30, 2017

Avoid comparing a variable with multiple items in a conditional, use Array#include? instead.

+
+ # If we've stripped out everything in the query, null it out
+ def clean_up_query
+ if params[:query] =~ /^\s*$/
@houndci-bot

houndci-bot Nov 30, 2017

Use the return of the conditional for variable assignment and comparison.

@@ -440,7 +428,7 @@ def sort_values
# extract the pretty name
def name_for_sort_column(sort_column)
- Hash[SORT_OPTIONS.collect {|v| [ v[1], v[0] ]}][sort_column]
+ Hash[sort_options.collect {|v| [ v[1], v[0] ]}][sort_column]
@houndci-bot

houndci-bot Nov 30, 2017

Prefer map over collect.
Space between { and | missing.
Space inside square brackets detected.
Space missing inside }.

+require 'active_support/inflector'
+require File.expand_path('../../app/models/search/query_cleaner', File.dirname(__FILE__))
+
+describe QueryCleaner do
@houndci-bot

houndci-bot Nov 30, 2017

Block has too many lines. [56/25]

+require File.expand_path('../../app/models/search/query_cleaner', File.dirname(__FILE__))
+
+describe QueryCleaner do
+ describe "#clean" do
@houndci-bot

houndci-bot Nov 30, 2017

Block has too many lines. [54/25]

+require 'active_support/inflector'
+require File.expand_path('../../app/models/search/query_cleaner', File.dirname(__FILE__))
+
+describe QueryCleaner do
@houndci-bot

houndci-bot Nov 30, 2017

Block has too many lines. [60/25]

+require File.expand_path('../../app/models/search/query_cleaner', File.dirname(__FILE__))
+
+describe QueryCleaner do
+ describe "#clean" do
@houndci-bot

houndci-bot Nov 30, 2017

Block has too many lines. [58/25]

@sarken sarken merged commit 9fabec2 into otwcode:master Dec 1, 2017

4 checks passed

Scrutinizer 13 new issues, 48 updated code elements
Details
codeclimate Approved by Elz.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
hound 18 violations found.

@redsummernight redsummernight changed the title from AO3-5262: Move query cleaning out of the works controller to AO3-5262 AO3-3576: Move query cleaning out of the works controller, fix sa"m/f"rodo Dec 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment