Class: Refine::Inline::Criterion
- Inherits:
-
Object
- Object
- Refine::Inline::Criterion
- Includes:
- ActiveModel::Model
- Defined in:
- app/models/refine/inline/criterion.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#condition_id ⇒ Object
Returns the value of attribute condition_id.
-
#conjunction ⇒ Object
Returns the value of attribute conjunction.
-
#input ⇒ Object
Returns the value of attribute input.
-
#position ⇒ Object
Returns the value of attribute position.
-
#refine_filter ⇒ Object
Returns the value of attribute refine_filter.
-
#stable_id ⇒ Object
Returns the value of attribute stable_id.
Class Method Summary collapse
-
.from_blueprint_node(node, **additional_attrs) ⇒ Object
initialize a Crtierion object from a blueprint node.
-
.groups_from_filter(refine_filter, **attrs) ⇒ Object
Returns a nested array of Criterion objects reflecting the grouping of the OR groups in a filter's blueprint.
Instance Method Summary collapse
- #attributes ⇒ Object
- #clause_display ⇒ Object
- #condition ⇒ Object
- #condition_display ⇒ Object
- #human_readable_value ⇒ Object
- #input_attributes ⇒ Object
- #input_attributes=(attrs = {}) ⇒ Object
- #input_partial ⇒ Object
- #multiple? ⇒ Boolean
- #options ⇒ Object
- #to_blueprint_node ⇒ Object
- #to_key ⇒ Object
- #to_params ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
4 5 6 |
# File 'app/models/refine/inline/criterion.rb', line 4 def client_id @client_id end |
#condition_id ⇒ Object
Returns the value of attribute condition_id.
4 5 6 |
# File 'app/models/refine/inline/criterion.rb', line 4 def condition_id @condition_id end |
#conjunction ⇒ Object
Returns the value of attribute conjunction.
4 5 6 |
# File 'app/models/refine/inline/criterion.rb', line 4 def conjunction @conjunction end |
#input ⇒ Object
Returns the value of attribute input.
4 5 6 |
# File 'app/models/refine/inline/criterion.rb', line 4 def input @input end |
#position ⇒ Object
Returns the value of attribute position.
4 5 6 |
# File 'app/models/refine/inline/criterion.rb', line 4 def position @position end |
#refine_filter ⇒ Object
Returns the value of attribute refine_filter.
4 5 6 |
# File 'app/models/refine/inline/criterion.rb', line 4 def refine_filter @refine_filter end |
#stable_id ⇒ Object
Returns the value of attribute stable_id.
4 5 6 |
# File 'app/models/refine/inline/criterion.rb', line 4 def stable_id @stable_id end |
Class Method Details
.from_blueprint_node(node, **additional_attrs) ⇒ Object
initialize a Crtierion object from a blueprint node
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/refine/inline/criterion.rb', line 13 def self.from_blueprint_node(node, **additional_attrs) attrs = node.deep_dup.merge(additional_attrs) # delete some attributes we don't need attrs.delete(:depth) attrs.delete(:type) # suffix nested hash keys with '_attributes' to initialize objects attrs[:input_attributes] = attrs.delete(:input) if input_attrs = attrs[:input_attributes] input_attrs[:count_refinement_attributes] = input_attrs.delete(:count_refinement) input_attrs[:date_refinement_attributes] = input_attrs.delete(:date_refinement) end new(attrs) end |
.groups_from_filter(refine_filter, **attrs) ⇒ Object
Returns a nested array of Criterion objects reflecting the grouping of the OR groups in a filter's blueprint
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/refine/inline/criterion.rb', line 31 def self.groups_from_filter(refine_filter, **attrs) return [] unless refine_filter&.blueprint.present? [].tap do |result| result.push([]) refine_filter.blueprint.each_with_index do |node, i| case node[:word] when "or" result.push [] when "and" next else criterion = from_blueprint_node(node, **attrs.merge(refine_filter: refine_filter, position: i)) result.last.push criterion end end end end |
Instance Method Details
#attributes ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'app/models/refine/inline/criterion.rb', line 49 def attributes { stable_id: stable_id, client_id: client_id, condition_id: condition_id, position: position, conjunction: conjunction, input_attributes: input_attributes }.compact end |
#clause_display ⇒ Object
109 110 111 |
# File 'app/models/refine/inline/criterion.rb', line 109 def clause_display condition&.clause_display(input&.clause) end |
#condition ⇒ Object
80 81 82 83 84 85 86 |
# File 'app/models/refine/inline/criterion.rb', line 80 def condition @condition ||= begin @refine_filter .instantiated_conditions .find { |c| c.id == condition_id } end end |
#condition_display ⇒ Object
105 106 107 |
# File 'app/models/refine/inline/criterion.rb', line 105 def condition_display condition&.display end |
#human_readable_value ⇒ Object
101 102 103 |
# File 'app/models/refine/inline/criterion.rb', line 101 def human_readable_value condition.human_readable_value(to_blueprint_node[:input]) end |
#input_attributes ⇒ Object
68 69 70 |
# File 'app/models/refine/inline/criterion.rb', line 68 def input_attributes input.attributes end |
#input_attributes=(attrs = {}) ⇒ Object
72 73 74 |
# File 'app/models/refine/inline/criterion.rb', line 72 def input_attributes=(attrs = {}) input.attributes = attrs.to_h end |
#input_partial ⇒ Object
88 89 90 |
# File 'app/models/refine/inline/criterion.rb', line 88 def input_partial "refine/inline/inputs/#{condition.component}".underscore end |
#multiple? ⇒ Boolean
119 120 121 122 |
# File 'app/models/refine/inline/criterion.rb', line 119 def multiple? selected_clause = condition.clauses.detect {|c| c.id == input.clause } || condition.clauses.first selected_clause.[:multiple].present? end |
#options ⇒ Object
113 114 115 116 117 |
# File 'app/models/refine/inline/criterion.rb', line 113 def if condition.respond_to? : condition..call.map {|option_hash| Refine::Inline::Criteria::Option.new(**option_hash)} end end |
#to_blueprint_node ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'app/models/refine/inline/criterion.rb', line 92 def to_blueprint_node result = attributes.slice(:condition_id, :input_attributes) result[:input] = result.delete(:input_attributes) if input_attrs = result[:input] result[:input] = input_attrs end result end |
#to_key ⇒ Object
76 77 78 |
# File 'app/models/refine/inline/criterion.rb', line 76 def to_key [client_id, position, conjunction].map(&:presence).compact end |
#to_params ⇒ Object
60 61 62 |
# File 'app/models/refine/inline/criterion.rb', line 60 def to_params {refine_inline_criterion: attributes} end |
#valid? ⇒ Boolean
136 137 138 139 |
# File 'app/models/refine/inline/criterion.rb', line 136 def valid? validate! errors.empty? end |
#validate! ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/models/refine/inline/criterion.rb', line 124 def validate! errors.clear begin query_for_validate = refine_filter.initial_query || refine_filter.model.all condition&.apply(input_attributes, refine_filter.table, query_for_validate) rescue Refine::Conditions::Errors::ConditionClauseError => e e.errors.each do |error| errors.add(:base, error.) end end end |