Class: Refine::Conditions::FilterCondition

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

Constant Summary collapse

CLAUSE_IN =
Clauses::IN
CLAUSE_NOT_IN =
Clauses::NOT_IN
I18N_PREFIX =
"refine.refine_blueprints.filter_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 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 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, #human_readable, #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 HasMeta

#meta, #with_meta

Constructor Details

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

Instance Attribute Details

#options ⇒ Object (readonly)

Returns the value of attribute options.



7
8
9
# File 'app/models/refine/conditions/filter_condition.rb', line 7

def options
  @options
end

Instance Method Details

#apply_condition(input, table, _inverse_clause) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'app/models/refine/conditions/filter_condition.rb', line 76

def apply_condition(input, table, _inverse_clause)
  filter_id = input[:selected].first.to_i
  filter = Refine::Rails.configuration.stabilizer_classes[:db].new.from_stable_id(id: filter_id)
  # TODO handle this more elegantly
  raise I18n.t("#{I18N_PREFIX}not_found") if filter.blank?
  # TODO - Filter initial query is currently handled on the filter class. ProductFilter.where....
  # Is this the right way to handle it?
  filter.make_sub_query(filter.blueprint)
end

#boot ⇒ Object



18
19
20
21
22
# File 'app/models/refine/conditions/filter_condition.rb', line 18

def boot
  @options = nil
  with_meta({options: get_options})
  add_ensurance(ensure_options)
end

#clauses ⇒ Object



86
87
88
89
90
91
# File 'app/models/refine/conditions/filter_condition.rb', line 86

def clauses
  [
    Clause.new(CLAUSE_IN, I18n.t("#{I18N_PREFIX}in")),
    Clause.new(CLAUSE_NOT_IN, I18n.t("#{I18N_PREFIX}not_in"))
  ]
end

#component ⇒ Object



14
15
16
# File 'app/models/refine/conditions/filter_condition.rb', line 14

def component
  "filter-condition"
end

#ensure_no_duplicates(developer_options) ⇒ Object



49
50
51
52
53
54
55
# File 'app/models/refine/conditions/filter_condition.rb', line 49

def ensure_no_duplicates(developer_options)
  id_array = developer_options.map { |option| option[:id] }
  duplicates = id_array.select { |id| id_array.count(id) > 1 }.uniq
  if duplicates.present?
    raise Refine::Conditions::Errors::OptionError.new(I18n.t("#{I18N_PREFIX}must_be_unique", duplicates: duplicates))
  end
end

#ensure_options ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/refine/conditions/filter_condition.rb', line 32

def ensure_options
  proc do
    developer_options = get_options.call
    # Options must evaluate to an array
    if !developer_options.is_a? Array
      raise I18n.t("#{I18N_PREFIX}options_not_determined")
    end
    # Each option must be a hash of values that includes :id and :display
    developer_options.each do |option|
      if (!option.is_a? Hash) || option.keys.exclude?(:id) || option.keys.exclude?(:display)
        raise Refine::Conditions::Errors::OptionError.new(I18n.t("#{I18N_PREFIX}must_have_id_and_display"))
      end
    end
    ensure_no_duplicates(developer_options)
  end
end

#get_options ⇒ Object



70
71
72
73
74
# File 'app/models/refine/conditions/filter_condition.rb', line 70

def get_options
  proc do
    @options = call_proc_if_callable(options)
  end
end

#select_is_array ⇒ Object



28
29
30
# File 'app/models/refine/conditions/filter_condition.rb', line 28

def select_is_array
  errors.add(:base, I18n.t("#{I18N_PREFIX}must_be_array")) unless selected.is_a?(Array)
end

#set_input_parameters(input) ⇒ Object



24
25
26
# File 'app/models/refine/conditions/filter_condition.rb', line 24

def set_input_parameters(input)
  @selected = input[:selected]
end

#stored_only ⇒ Object



66
67
68
# File 'app/models/refine/conditions/filter_condition.rb', line 66

def stored_only
  self
end

#with_scope(scope) ⇒ Object

TODO improve this developer interface...



58
59
60
61
62
63
64
# File 'app/models/refine/conditions/filter_condition.rb', line 58

def with_scope(scope)
  @options = []
  scope.all.each do |filter|
    @options << {id: filter.id, display: filter.name}
  end
  self
end