Module: SearchQualifierHelper
- Included in:
- SearchHelper
- Defined in:
- /var/apps/qpixel/app/helpers/search_qualifier_helper.rb
Instance Method Summary collapse
-
#date_value_sql(value) ⇒ Array(String, String, String)
private
Parses a qualifier value string, including operator, as a date value.
- #matches_date?(value) ⇒ Boolean
- #matches_id?(value) ⇒ Boolean
- #matches_int?(value) ⇒ Boolean
- #matches_non_negative_int?(value) ⇒ Boolean
- #matches_numeric?(value) ⇒ Boolean
- #matches_source?(value) ⇒ Boolean
- #matches_status?(value) ⇒ Boolean
-
#numeric_value_sql(value) ⇒ Array(String, String)
private
Parses a qualifier value string, including operator, as a numeric value.
- #parse_answers_qualifier(value) ⇒ Object
- #parse_category_qualifier(value) ⇒ Object
- #parse_created_qualifier(value) ⇒ Object
- #parse_downvotes_qualifier(value) ⇒ Object
- #parse_exclude_tag_qualifier(value) ⇒ Object
- #parse_include_tag_qualifier(value) ⇒ Object
- #parse_post_type_qualifier(value) ⇒ Object
- #parse_score_qualifier(value) ⇒ Object
- #parse_source_qualifier(value) ⇒ Object
- #parse_status_qualifier(value) ⇒ Object
- #parse_upvotes_qualifier(value) ⇒ Object
- #parse_user_qualifier(value) ⇒ Object
- #parse_votes_qualifier(value) ⇒ Object
Instance Method Details
#date_value_sql(value) ⇒ Array(String, String, String)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses a qualifier value string, including operator, as a date value.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 146 def date_value_sql(value) operator = '' while ['<', '>', '='].include? value[0] operator += value[0] value = value[1..-1] end # working with dates: <1y ('less than one year ago') is SQL: > 1y ago operator = { '<' => '>', '>' => '<', '<=' => '>=', '>=' => '<=' }[operator] || '' val = '' while value[0] =~ /[[:digit:]]/ val += value[0] value = value[1..-1] end timeframe = { s: 'SECOND', m: 'MINUTE', h: 'HOUR', d: 'DAY', w: 'WEEK', mo: 'MONTH', y: 'YEAR' }[value.to_sym] [operator, val, timeframe || 'MONTH'] end |
#matches_date?(value) ⇒ Boolean
2 3 4 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 2 def matches_date?(value) value.match?(/^[<>=]{0,2}[\d.]+(?:s|m|h|d|w|mo|y)?$/) end |
#matches_id?(value) ⇒ Boolean
6 7 8 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 6 def matches_id?(value) value.match?(/^[<>=]{0,2}\d+$/) end |
#matches_int?(value) ⇒ Boolean
10 11 12 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 10 def matches_int?(value) value.match?(/^[<>=]{0,2}-?\d+$/) end |
#matches_non_negative_int?(value) ⇒ Boolean
18 19 20 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 18 def matches_non_negative_int?(value) value.match?(/^[<>=]{0,2}\d+$/) end |
#matches_numeric?(value) ⇒ Boolean
14 15 16 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 14 def matches_numeric?(value) value.match?(/^[<>=]{0,2}[\d.]+$/) end |
#matches_source?(value) ⇒ Boolean
26 27 28 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 26 def matches_source?(value) value.match?(/any|imported|native/) end |
#matches_status?(value) ⇒ Boolean
22 23 24 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 22 def matches_status?(value) value.match?(/any|open|closed/) end |
#numeric_value_sql(value) ⇒ Array(String, String)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses a qualifier value string, including operator, as a numeric value.
130 131 132 133 134 135 136 137 138 139 140 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 130 def numeric_value_sql(value) operator = '' while ['<', '>', '='].include? value[0] operator += value[0] value = value[1..-1] end # whatever's left after stripping operator is the number # validated by regex in qualifiers_to_sql [operator, value] end |
#parse_answers_qualifier(value) ⇒ Object
30 31 32 33 34 35 36 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 30 def parse_answers_qualifier(value) return unless matches_non_negative_int?(value) operator, val = numeric_value_sql(value) { param: :answers, operator: operator.presence || '=', value: val.to_i } end |
#parse_category_qualifier(value) ⇒ Object
38 39 40 41 42 43 44 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 38 def parse_category_qualifier(value) return unless matches_id?(value) operator, val = numeric_value_sql(value) { param: :category, operator: operator.presence || '=', category_id: val.to_i } end |
#parse_created_qualifier(value) ⇒ Object
46 47 48 49 50 51 52 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 46 def parse_created_qualifier(value) return unless matches_date?(value) operator, val, timeframe = date_value_sql(value) { param: :created, operator: operator.presence || '=', timeframe: timeframe, value: val.to_i } end |
#parse_downvotes_qualifier(value) ⇒ Object
54 55 56 57 58 59 60 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 54 def parse_downvotes_qualifier(value) return unless matches_non_negative_int?(value) operator, val = numeric_value_sql(value) { param: :downvotes, operator: operator.presence || '=', value: val.to_i } end |
#parse_exclude_tag_qualifier(value) ⇒ Object
62 63 64 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 62 def parse_exclude_tag_qualifier(value) { param: :exclude_tag, tag_id: Tag.where(name: value).select(:id) } end |
#parse_include_tag_qualifier(value) ⇒ Object
94 95 96 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 94 def parse_include_tag_qualifier(value) { param: :include_tag, tag_id: Tag.where(name: value).select(:id) } end |
#parse_post_type_qualifier(value) ⇒ Object
66 67 68 69 70 71 72 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 66 def parse_post_type_qualifier(value) return unless matches_id?(value) operator, val = numeric_value_sql(value) { param: :post_type, operator: operator.presence || '=', post_type_id: val.to_i } end |
#parse_score_qualifier(value) ⇒ Object
74 75 76 77 78 79 80 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 74 def parse_score_qualifier(value) return unless matches_numeric?(value) operator, val = numeric_value_sql(value) { param: :score, operator: operator.presence || '=', value: val.to_f } end |
#parse_source_qualifier(value) ⇒ Object
88 89 90 91 92 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 88 def parse_source_qualifier(value) return unless matches_source?(value) { param: :source, value: value } end |
#parse_status_qualifier(value) ⇒ Object
82 83 84 85 86 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 82 def parse_status_qualifier(value) return unless matches_status?(value) { param: :status, value: value } end |
#parse_upvotes_qualifier(value) ⇒ Object
98 99 100 101 102 103 104 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 98 def parse_upvotes_qualifier(value) return unless matches_non_negative_int?(value) operator, val = numeric_value_sql(value) { param: :upvotes, operator: operator.presence || '=', value: val.to_i } end |
#parse_user_qualifier(value) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 106 def parse_user_qualifier(value) return unless matches_int?(value) || value == 'me' operator, val = if value == 'me' ['=', current_user&.id] else numeric_value_sql(value) end { param: :user, operator: operator.presence || '=', user_id: val.to_i } end |
#parse_votes_qualifier(value) ⇒ Object
118 119 120 121 122 123 124 |
# File '/var/apps/qpixel/app/helpers/search_qualifier_helper.rb', line 118 def parse_votes_qualifier(value) return unless matches_int?(value) operator, val = numeric_value_sql(value) { param: :net_votes, operator: operator.presence || '=', value: val.to_i } end |