Class: Refine::Conditions::NumericCondition

Inherits:
Condition
  • Object
show all
Includes:
ActiveModel::Validations, HasClauses
Defined in:
app/models/refine/conditions/numeric_condition.rb

Constant Summary collapse

CLAUSE_EQUALS =
Clauses::EQUALS
CLAUSE_DOESNT_EQUAL =
Clauses::DOESNT_EQUAL
CLAUSE_LESS_THAN_OR_EQUAL =
Clauses::LESS_THAN_OR_EQUAL
CLAUSE_LESS_THAN =
Clauses::LESS_THAN
CLAUSE_GREATER_THAN =
Clauses::GREATER_THAN
CLAUSE_GREATER_THAN_OR_EQUAL =
Clauses::GREATER_THAN_OR_EQUAL
CLAUSE_BETWEEN =
Clauses::BETWEEN
CLAUSE_NOT_BETWEEN =
Clauses::NOT_BETWEEN
CLAUSE_SET =
Clauses::SET
CLAUSE_NOT_SET =
Clauses::NOT_SET
I18N_PREFIX =
"refine.refine_blueprints.numeric_condition."

Constants included from SupportsFlatQueries

SupportsFlatQueries::LEFT_JOIN_CLAUSES

Instance Attribute Summary collapse

Attributes inherited from Condition

#attribute, #before_validations, #clause, #display, #ensurances, #filter, #id, #is_flat_query, #is_refinement

Instance Method Summary collapse

Methods included from HasClauses

#before_clause_validation, #boot_has_clauses, #clause_display_map, #custom_clauses, #ensure_clause, #ensure_clauses, #get_clause_by_id, #get_clauses, included, #only_clauses, #remap_clause_displays, #update_show_clauses, #with_clauses, #without_clauses

Methods inherited from Condition

#add_ensurance, #add_messages, #add_rules, #apply, #approved_clauses, #before_validate, #boot_traits, #call_proc_if_callable, #clause_display, #clause_exists?, #clause_in_approved_list?, #ensure_attribute_configured, #ensure_id, #evaluated_meta, #initialize, #messages, #recursively_evaluate_lazy_enumerable, #run_before_validate_validations, #run_ensurance_validations, #set_filter, #to_array, #update_value, #validate_user_input, #with_display

Methods included from SupportsFlatQueries

#add_pending_join, #add_pending_joins_if_needed, #apply_flat, #apply_flat_relational_condition, #condition_uses_different_database?, #get_foreign_key_from_relation, #get_through_reflection, #handle_flat_cross_database_condition, #handle_flat_relational_condition

Methods included from HasIcon

#category_icon_class, #category_icon_template, #has_icon?, #icon_class, #icon_container_class, #icon_template, #with_category_icon_class, #with_category_icon_template, #with_icon, #with_icon_class, #with_icon_container_class, #with_icon_template

Methods included from WithForcedIndex

#forced_index, #with_forced_index

Methods included from HasThroughIdRelationship

#through_id_relationship, #with_through_id_relationship

Methods included from HasRefinements

#apply_count_refinement, #apply_date_refinement, #apply_refinements, #define_count_condition, #define_date_condition, #get_count_refinement_condition, #get_date_refinement_condition, #has_any_refinements?, #has_count_refinement?, #has_date_refinement?, #refine_by_count, #refine_by_date, #refine_by_filter, #refinements_allowed?, #refinements_to_array

Methods included from UsesAttributes

#apply_relationship_attribute, #can_use_where_in_relationship_subquery?, #create_pending_has_many_through_subquery, #create_pending_wherein_subquery, #get_reflection_object, #group, #is_relationship_attribute?, #key_1, #key_2, #raw_attribute, #should_inverse_clause?, #with_attribute

Methods included from HasMeta

#meta, #with_meta

Constructor Details

This class inherits a constructor from Refine::Conditions::Condition

Instance Attribute Details

#value1 ⇒ Object (readonly)

Returns the value of attribute value1.



16
17
18
# File 'app/models/refine/conditions/numeric_condition.rb', line 16

def value1
  @value1
end

#value2 ⇒ Object (readonly)

Returns the value of attribute value2.



16
17
18
# File 'app/models/refine/conditions/numeric_condition.rb', line 16

def value2
  @value2
end

Instance Method Details

#allow_floats ⇒ Object



101
102
103
104
# File 'app/models/refine/conditions/numeric_condition.rb', line 101

def allow_floats
  @floats = true
  self
end

#apply_clause_between(table, value1, value2) ⇒ Object



208
209
210
# File 'app/models/refine/conditions/numeric_condition.rb', line 208

def apply_clause_between(table, value1, value2)
  table.grouping(arel_attribute(table).between(value1..value2))
end

#apply_clause_doesnt_equal(table, value) ⇒ Object



188
189
190
# File 'app/models/refine/conditions/numeric_condition.rb', line 188

def apply_clause_doesnt_equal(table, value)
  table.grouping(arel_attribute(table).not_eq(value).or(arel_attribute(table).eq(nil)))
end

#apply_clause_equals(table, value) ⇒ Object



184
185
186
# File 'app/models/refine/conditions/numeric_condition.rb', line 184

def apply_clause_equals(table, value)
  table.grouping(arel_attribute(table).eq(value))
end

#apply_clause_greater_than(table, value) ⇒ Object



192
193
194
# File 'app/models/refine/conditions/numeric_condition.rb', line 192

def apply_clause_greater_than(table, value)
  table.grouping(arel_attribute(table).gt(value))
end

#apply_clause_greater_than_or_equal(table, value) ⇒ Object



196
197
198
# File 'app/models/refine/conditions/numeric_condition.rb', line 196

def apply_clause_greater_than_or_equal(table, value)
  table.grouping(arel_attribute(table).gteq(value))
end

#apply_clause_less_than(table, value) ⇒ Object



200
201
202
# File 'app/models/refine/conditions/numeric_condition.rb', line 200

def apply_clause_less_than(table, value)
  table.grouping(arel_attribute(table).lt(value))
end

#apply_clause_less_than_or_equal(table, value) ⇒ Object



204
205
206
# File 'app/models/refine/conditions/numeric_condition.rb', line 204

def apply_clause_less_than_or_equal(table, value)
  table.grouping(arel_attribute(table).lteq(value))
end

#apply_clause_not_between(table, value1, value2) ⇒ Object



212
213
214
# File 'app/models/refine/conditions/numeric_condition.rb', line 212

def apply_clause_not_between(table, value1, value2)
  table.grouping(arel_attribute(table).not_between(value1..value2))
end

#apply_clause_not_set(table) ⇒ Object



220
221
222
# File 'app/models/refine/conditions/numeric_condition.rb', line 220

def apply_clause_not_set(table)
  table.grouping(arel_attribute(table).eq(nil))
end

#apply_clause_set(table) ⇒ Object



216
217
218
# File 'app/models/refine/conditions/numeric_condition.rb', line 216

def apply_clause_set(table)
  table.grouping(arel_attribute(table).not_eq(nil))
end

#apply_condition(input, table, _inverse_clause) ⇒ Object

TODO Refactor to remove input here



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/refine/conditions/numeric_condition.rb', line 111

def apply_condition(input, table, _inverse_clause)
  # TODO check for custom clause

  case clause
  when CLAUSE_EQUALS
    apply_clause_equals(table, value1)

  when CLAUSE_DOESNT_EQUAL
    apply_clause_doesnt_equal(table, value1)

  when CLAUSE_GREATER_THAN
    apply_clause_greater_than(table, value1)

  when CLAUSE_GREATER_THAN_OR_EQUAL
    apply_clause_greater_than_or_equal(table, value1)

  when CLAUSE_LESS_THAN
    apply_clause_less_than(table, value1)

  when CLAUSE_LESS_THAN_OR_EQUAL
    apply_clause_less_than_or_equal(table, value1)

  when CLAUSE_BETWEEN
    apply_clause_between(table, value1, value2)

  when CLAUSE_NOT_BETWEEN
    apply_clause_not_between(table, value1, value2)

  when CLAUSE_SET
    apply_clause_set(table)

  when CLAUSE_NOT_SET
    apply_clause_not_set(table)
  end
end

#boot ⇒ Object



34
35
36
# File 'app/models/refine/conditions/numeric_condition.rb', line 34

def boot
  @floats = false
end

#clauses ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/refine/conditions/numeric_condition.rb', line 77

def clauses
  [
    Clause.new(CLAUSE_EQUALS, I18n.t("#{I18N_PREFIX}is")).requires_inputs(["value1"]),

    Clause.new(CLAUSE_DOESNT_EQUAL, I18n.t("#{I18N_PREFIX}is_not")).requires_inputs(["value1"]),

    Clause.new(CLAUSE_GREATER_THAN, I18n.t("#{I18N_PREFIX}is_gt")).requires_inputs(["value1"]),

    Clause.new(CLAUSE_GREATER_THAN_OR_EQUAL, I18n.t("#{I18N_PREFIX}is_gtteq")).requires_inputs(["value1"]),

    Clause.new(CLAUSE_LESS_THAN, I18n.t("#{I18N_PREFIX}is_lt")).requires_inputs(["value1"]),

    Clause.new(CLAUSE_LESS_THAN_OR_EQUAL, I18n.t("#{I18N_PREFIX}is_lteq")).requires_inputs(["value1"]),

    Clause.new(CLAUSE_BETWEEN, I18n.t("#{I18N_PREFIX}is_between")).requires_inputs(["value1", "value2"]),

    Clause.new(CLAUSE_NOT_BETWEEN, I18n.t("#{I18N_PREFIX}is_not_between")).requires_inputs(["value1", "value2"]),

    Clause.new(CLAUSE_SET, I18n.t("#{I18N_PREFIX}is_set")),

    Clause.new(CLAUSE_NOT_SET, I18n.t("#{I18N_PREFIX}is_not_set")),
  ]
end

#component ⇒ Object



43
44
45
# File 'app/models/refine/conditions/numeric_condition.rb', line 43

def component
  "numeric-condition"
end

#floats_not_allowed? ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'app/models/refine/conditions/numeric_condition.rb', line 106

def floats_not_allowed?
  !@floats
end

#human_readable(input) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/refine/conditions/numeric_condition.rb', line 47

def human_readable(input)
  current_clause = get_clause_by_id(input[:clause])
  case input[:clause]
  when *[CLAUSE_EQUALS, CLAUSE_DOESNT_EQUAL, CLAUSE_GREATER_THAN, CLAUSE_GREATER_THAN_OR_EQUAL, CLAUSE_LESS_THAN, CLAUSE_LESS_THAN_OR_EQUAL]
    "#{display} #{current_clause.display} #{input[:value1]}"
  when *[CLAUSE_BETWEEN, CLAUSE_NOT_BETWEEN]
    "#{display} #{current_clause.display} #{input[:value1]} #{I18n.t("#{I18N_PREFIX}and")} #{input[:value2]}"
  when *[CLAUSE_SET, CLAUSE_NOT_SET]
    "#{display} #{current_clause.display}"
  else
    raise "#{input[:clause]} #{I18n.t("#{I18N_PREFIX}not_supported")}"
  end
end

#human_readable_value(input) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/refine/conditions/numeric_condition.rb', line 61

def human_readable_value(input)
  current_clause = get_clause_by_id(input[:clause])
  case input[:clause]
  when *[CLAUSE_EQUALS, CLAUSE_DOESNT_EQUAL, CLAUSE_GREATER_THAN, CLAUSE_GREATER_THAN_OR_EQUAL, CLAUSE_LESS_THAN, CLAUSE_LESS_THAN_OR_EQUAL]
    input[:value1]
  when *[CLAUSE_BETWEEN, CLAUSE_NOT_BETWEEN]
    "#{input[:value1]} #{I18n.t("#{I18N_PREFIX}and")} #{input[:value2]}"
  when *[CLAUSE_SET, CLAUSE_NOT_SET]
    ""
  else
    raise "#{input[:clause]} #{I18n.t("#{I18N_PREFIX}not_supported")}"
  end
end

#input_could_include_zero?(input) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/refine/conditions/numeric_condition.rb', line 147

def input_could_include_zero?(input)
  clause = input[:clause]
  value1 = input[:value1].to_i
  value2 = input[:value2].to_i
  case clause
  when CLAUSE_EQUALS
    return value1 == 0

  when CLAUSE_DOESNT_EQUAL
    return value1 != 0

  when CLAUSE_LESS_THAN_OR_EQUAL
    return value1 >= 0

  when CLAUSE_LESS_THAN
    return value1 > 0

  when CLAUSE_GREATER_THAN
    return value1 < 0

  when CLAUSE_GREATER_THAN_OR_EQUAL
    return value1 <= 0

  when CLAUSE_BETWEEN
    return value1 <= 0 && value2 >= 0

  when CLAUSE_NOT_BETWEEN
    return (value1 > 0 && value2 > 0) || (value1 < 0 && value2 < 0)

  when CLAUSE_SET
    return false

  when CLAUSE_NOT_SET
    return false
  end
end

#set_input_parameters(input) ⇒ Object



38
39
40
41
# File 'app/models/refine/conditions/numeric_condition.rb', line 38

def set_input_parameters(input)
  @value1 = input[:value1]
  @value2 = input[:value2]
end