Class: Refine::Conditions::TextCondition

Inherits:
Condition
  • Object
show all
Includes:
HasClauses
Defined in:
app/models/refine/conditions/text_condition.rb

Constant Summary collapse

CLAUSE_EQUALS =
Clauses::EQUALS
CLAUSE_DOESNT_EQUAL =
Clauses::DOESNT_EQUAL
CLAUSE_STARTS_WITH =
Clauses::STARTS_WITH
CLAUSE_ENDS_WITH =
Clauses::ENDS_WITH
CLAUSE_DOESNT_START_WITH =
Clauses::DOESNT_START_WITH
CLAUSE_DOESNT_END_WITH =
Clauses::DOESNT_END_WITH
CLAUSE_CONTAINS =
Clauses::CONTAINS
CLAUSE_DOESNT_CONTAIN =
Clauses::DOESNT_CONTAIN
CLAUSE_SET =
Clauses::SET
CLAUSE_NOT_SET =
Clauses::NOT_SET
I18N_PREFIX =
"refine.refine_blueprints.text_condition."

Constants included from SupportsFlatQueries

SupportsFlatQueries::LEFT_JOIN_CLAUSES

Instance Attribute Summary

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, #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, #set_input_parameters, #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 Method Details

#apply_clause_contains(value, table) ⇒ Object



128
129
130
# File 'app/models/refine/conditions/text_condition.rb', line 128

def apply_clause_contains(value, table)
  table.grouping(arel_attribute(table).matches("%#{value}%"))
end

#apply_clause_doesnt_contain(value, table) ⇒ Object



132
133
134
# File 'app/models/refine/conditions/text_condition.rb', line 132

def apply_clause_doesnt_contain(value, table)
  table.grouping(arel_attribute(table).does_not_match("%#{value}%").or(arel_attribute(table).eq(nil)))
end

#apply_clause_doesnt_end_with(value, table) ⇒ Object



148
149
150
# File 'app/models/refine/conditions/text_condition.rb', line 148

def apply_clause_doesnt_end_with(value, table)
  table.grouping(arel_attribute(table).does_not_match("%#{value}"))
end

#apply_clause_doesnt_equal(value, table) ⇒ Object



116
117
118
# File 'app/models/refine/conditions/text_condition.rb', line 116

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

#apply_clause_doesnt_start_with(value, table) ⇒ Object



144
145
146
# File 'app/models/refine/conditions/text_condition.rb', line 144

def apply_clause_doesnt_start_with(value, table)
  table.grouping(arel_attribute(table).does_not_match("#{value}%"))
end

#apply_clause_ends_with(value, table) ⇒ Object



124
125
126
# File 'app/models/refine/conditions/text_condition.rb', line 124

def apply_clause_ends_with(value, table)
  table.grouping(arel_attribute(table).matches("%#{value}"))
end

#apply_clause_equals(value, table) ⇒ Object



112
113
114
# File 'app/models/refine/conditions/text_condition.rb', line 112

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

#apply_clause_not_set(_, table) ⇒ Object



140
141
142
# File 'app/models/refine/conditions/text_condition.rb', line 140

def apply_clause_not_set(_, table)
  table.grouping(arel_attribute(table).eq_any([nil, ""]))
end

#apply_clause_set(_, table) ⇒ Object



136
137
138
# File 'app/models/refine/conditions/text_condition.rb', line 136

def apply_clause_set(_, table)
  table.grouping(arel_attribute(table).not_eq_all([nil, ""]))
end

#apply_clause_starts_with(value, table) ⇒ Object



120
121
122
# File 'app/models/refine/conditions/text_condition.rb', line 120

def apply_clause_starts_with(value, table)
  table.grouping(arel_attribute(table).matches("#{value}%"))
end

#apply_condition(input, table, _inverse_clause) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/refine/conditions/text_condition.rb', line 76

def apply_condition(input, table, _inverse_clause)
  value = input[:value]

  case clause
  when CLAUSE_EQUALS
    apply_clause_equals(value, table)

  when CLAUSE_DOESNT_EQUAL
    apply_clause_doesnt_equal(value, table)

  when CLAUSE_STARTS_WITH
    apply_clause_starts_with(value, table)

  when CLAUSE_ENDS_WITH
    apply_clause_ends_with(value, table)

  when CLAUSE_DOESNT_START_WITH
    apply_clause_doesnt_start_with(value, table)

  when CLAUSE_DOESNT_END_WITH
    apply_clause_doesnt_end_with(value, table)

  when CLAUSE_CONTAINS
    apply_clause_contains(value, table)

  when CLAUSE_DOESNT_CONTAIN
    apply_clause_doesnt_contain(value, table)

  when CLAUSE_SET
    apply_clause_set(value, table)

  when CLAUSE_NOT_SET
    apply_clause_not_set(value, table)
  end
end

#clauses ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/refine/conditions/text_condition.rb', line 44

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

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

    Clause.new(CLAUSE_STARTS_WITH, I18n.t("#{I18N_PREFIX}starts_with"))
      .requires_inputs(["value"]),

    Clause.new(CLAUSE_ENDS_WITH, I18n.t("#{I18N_PREFIX}ends_with"))
      .requires_inputs(["value"]),

    Clause.new(CLAUSE_DOESNT_START_WITH, I18n.t("#{I18N_PREFIX}does_not_start_with"))
      .requires_inputs(["value"]),

    Clause.new(CLAUSE_DOESNT_END_WITH, I18n.t("#{I18N_PREFIX}does_not_end_with"))
      .requires_inputs(["value"]),

    Clause.new(CLAUSE_CONTAINS, I18n.t("#{I18N_PREFIX}contains"))
      .requires_inputs(["value"]),

    Clause.new(CLAUSE_DOESNT_CONTAIN, I18n.t("#{I18N_PREFIX}does_not_contain"))
      .requires_inputs(["value"]),

    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



22
23
24
# File 'app/models/refine/conditions/text_condition.rb', line 22

def component
  "text-condition"
end

#human_readable(input) ⇒ Object



26
27
28
29
30
31
32
33
# File 'app/models/refine/conditions/text_condition.rb', line 26

def human_readable(input)
  current_clause = get_clause_by_id(input[:clause])
  if input[:clause].in? [CLAUSE_SET, CLAUSE_NOT_SET]
    "#{display} #{current_clause.display}"
  else
    "#{display} #{current_clause.display} #{input[:value]}"
  end
end

#human_readable_value(input) ⇒ Object



35
36
37
38
39
40
41
42
# File 'app/models/refine/conditions/text_condition.rb', line 35

def human_readable_value(input)
  current_clause = get_clause_by_id(input[:clause])
  if input[:clause].in? [CLAUSE_SET, CLAUSE_NOT_SET]
    ""
  else
    input[:value]
  end
end