Class: Refine::Filters::Query
- Inherits:
-
Object
- Object
- Refine::Filters::Query
- Includes:
- ActiveModel::Model
- Defined in:
- app/models/refine/filters/query.rb
Instance Attribute Summary collapse
-
#filter ⇒ Object
readonly
View Model for the main section of the filter builder- the dynamic query form.
Instance Method Summary collapse
- #available_conditions ⇒ Object
-
#available_conditions_attributes ⇒ Object
The json representation of conditions that is sent to the front end.
- #blueprint ⇒ Object
- #clear_errors ⇒ Object
- #configuration ⇒ Object
-
#error_messages ⇒ Object
TODO make this a more useful data with pointers to which criteria each error goes with.
- #grouped_criteria ⇒ Object
-
#initialize(filter) ⇒ Query
constructor
A new instance of Query.
- #valid? ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize(filter) ⇒ Query
Returns a new instance of Query.
7 8 9 10 |
# File 'app/models/refine/filters/query.rb', line 7 def initialize(filter) @filter = filter add_criteria! end |
Instance Attribute Details
#filter ⇒ Object (readonly)
View Model for the main section of the filter builder- the dynamic query form
5 6 7 |
# File 'app/models/refine/filters/query.rb', line 5 def filter @filter end |
Instance Method Details
#available_conditions ⇒ Object
44 45 46 |
# File 'app/models/refine/filters/query.rb', line 44 def available_conditions @filter.conditions end |
#available_conditions_attributes ⇒ Object
The json representation of conditions that is sent to the front end.
62 63 64 |
# File 'app/models/refine/filters/query.rb', line 62 def available_conditions_attributes configuration[:conditions] end |
#blueprint ⇒ Object
48 49 50 51 52 53 54 |
# File 'app/models/refine/filters/query.rb', line 48 def blueprint if @filter.blueprint&.any? @filter.blueprint else [] end end |
#clear_errors ⇒ Object
21 22 23 |
# File 'app/models/refine/filters/query.rb', line 21 def clear_errors @criteria.each { |c| c.errors.clear } end |
#configuration ⇒ Object
56 57 58 |
# File 'app/models/refine/filters/query.rb', line 56 def configuration @filter.configuration end |
#error_messages ⇒ Object
TODO make this a more useful data with pointers to which criteria each error goes with
68 69 70 |
# File 'app/models/refine/filters/query.rb', line 68 def @criteria.flat_map {|c| c.errors. } end |
#grouped_criteria ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/refine/filters/query.rb', line 25 def grouped_criteria # Allow for an empty blueprint return [] if @criteria.blank? [].tap do |result| # start with an empty group result.push [] @criteria.each_with_index do |criterion, index| case criterion.word when "or" result.push [] when "and" next else result.last.push criterion end end end end |
#valid? ⇒ Boolean
16 17 18 19 |
# File 'app/models/refine/filters/query.rb', line 16 def valid? validate! @criteria.all? { |c| c.errors.empty? } end |
#validate! ⇒ Object
12 13 14 |
# File 'app/models/refine/filters/query.rb', line 12 def validate! @criteria.each(&:validate!) end |