Class: Refine::Filters::BlueprintEditor

Inherits:
Object
  • Object
show all
Defined in:
app/models/refine/filters/blueprint_editor.rb

Constant Summary collapse

INPUT_ATTRS =
%i[
  clause
  date1
  date2
  days
  modifier
  selected
  value
  value1
  value2
  count_refinement
  date_refinement
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blueprint) ⇒ BlueprintEditor

Returns a new instance of BlueprintEditor.



21
22
23
# File 'app/models/refine/filters/blueprint_editor.rb', line 21

def initialize(blueprint)
  @blueprint = blueprint
end

Instance Attribute Details

#blueprint ⇒ Object (readonly)

Editor that will transform a given blueprint IN PLACE Designed to support operations for the v2 inline filter builder via ConditionsController



5
6
7
# File 'app/models/refine/filters/blueprint_editor.rb', line 5

def blueprint
  @blueprint
end

Instance Method Details

#add(position: -1,, conjunction: "and", criterion:) ⇒ Object

add a criteria at the specified position (defaults to the end)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/refine/filters/blueprint_editor.rb', line 26

def add(position: -1, conjunction: "and", criterion:)
  # TODO support multiple input conditions, refinements, etc

  conjunction_depth = case conjunction
  in "and" then 1
  in "or" then 0
  end

  # extract data from criterion
  condition_id = criterion[:condition_id]
  input = criterion[:input].slice(*INPUT_ATTRS)

  nodes_to_insert = []
  nodes_to_insert << {
    depth: conjunction_depth,
    type: "conjunction",
    word: conjunction,
  } if blueprint[(position.negative? ? position + 1 : position - 1)]

  nodes_to_insert << {
    depth: 1,
    type: "criterion",
    condition_id: condition_id,
    input: input
  }

  blueprint.insert position, *nodes_to_insert
end

#change_conjunction(index, conjunction_word) ⇒ Object



62
63
64
65
66
67
68
# File 'app/models/refine/filters/blueprint_editor.rb', line 62

def change_conjunction(index, conjunction_word)
  if conjunction_word == "and"
    blueprint[index][:word] = "and"
  elsif conjunction_word == "or"
    blueprint[index][:word] = "or"
  end
end

#delete(index) ⇒ Object



70
71
72
73
74
75
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/filters/blueprint_editor.rb', line 70

def delete(index)
 # To support 'groups' there is some complicated logic for deleting criterion.
 #
 # Imagine this simplified blueprint: [eq, and, sw, or, eq]
 #
 # User clicks to delete the last eq. We also have to delete the preceding or
 # otherwise we're left with a hanging empty group
 #
 # What if the user deletes the sw? We have to clean up the preceding and.
 #
 # Imagine another scenario: [eq or sw and ew]
 # Now we delete the first eq but this time we need to clean up the or.
 #
 # These conditionals cover these cases.

  previous_entry = index.zero? ? nil : blueprint[index - 1]
  next_entry = blueprint[index + 1]

  next_is_or = next_entry && next_entry[:word] == 'or'
  previous_is_or = previous_entry && previous_entry[:word] == 'or'

  next_is_right_paren = next_is_or || !next_entry
  previous_is_left_paren = previous_is_or || !previous_entry

  is_first_in_group = previous_is_left_paren && !next_is_right_paren
  is_last_in_group = previous_is_left_paren && next_is_right_paren
  is_last_criterion = !previous_entry && !next_entry

  if is_last_criterion
    blueprint.slice!(index)
  elsif is_last_in_group && previous_is_or
    blueprint.slice!((index - 1)..index)
  elsif is_last_in_group && !previous_entry
    blueprint.slice!(index..(index + 1))
  elsif is_first_in_group
    blueprint.slice!(index..(index + 1))
  else
    blueprint.slice!((index - 1)..index)
  end

end

#update(index, criterion:) ⇒ Object



55
56
57
58
59
60
# File 'app/models/refine/filters/blueprint_editor.rb', line 55

def update(index, criterion:)
  # extract data from criterion
  input = criterion[:input].slice(*INPUT_ATTRS)

  blueprint[index][:input] = input
end