Module: EasyModelSelects

Defined in:
lib/easy_model_selects.rb,
lib/easy_model_selects/version.rb

Constant Summary collapse

VERSION =
"0.9.0"

Class Method Summary collapse

Class Method Details

.delete_last_and(wrong_and_at_the_end) ⇒ Object



75
76
77
# File 'lib/easy_model_selects.rb', line 75

def self.delete_last_and(wrong_and_at_the_end)
  no_and_at_the_end = wrong_and_at_the_end.from(0).to(wrong_and_at_the_end.length - 5)
end

.get_where_condition(param_name, param_value, where_condition, modifier, operator) ⇒ Object



71
72
73
74
# File 'lib/easy_model_selects.rb', line 71

def self.get_where_condition(param_name, param_value, where_condition, modifier, operator)
  where_condition[0] = "#{where_condition[0]}#{param_name} #{modifier} (?) #{operator} "#question
  where_condition.push "#{param_value}" #values
end

.get_where_statement(param_name, param_value, where_condition) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/easy_model_selects.rb', line 15

def self.get_where_statement(param_name, param_value, where_condition)
  #some stuff to prepare everything
  modifiers = {">>"=> ">","<<"=> "<","=="=> "=","<="=> "<=",">="=> ">=", "LK"=> "LIKE", "IN"=> "IN"}
  operators = {"!and!"=>"and","!xor!"=>"or"}

  #care for easy writing
  param_name = "updated_at" if param_name == "modified_since"

  #care for operator staff
  if param_value.include? operators.keys.first#each element recommendet, but not necessary at the moment
    operatorkey = operators.keys.first
  else
    if param_value.include? operators.keys.last
      operatorkey = operators.keys.last
    else
      operatorkey = "!and!"
    end
  end

  param_value.split(operatorkey).each do |param_value_sep|
    if param_value_sep == param_value.split(operatorkey).last
      #operator to next statement needs to be and not or, or can just be between a condition for one attribute
      operatorkey = "!and!"
    end
  
    #care for modifiers
    if modifiers.key? param_value_sep[0..1]
      modifier = modifiers[param_value_sep[0..1]]
      param_value_sep = param_value_sep[2..param_value_sep.length]
    else
      modifier = "="
    end
  
    where_condition = get_where_condition(param_name, param_value_sep, where_condition, modifier, operators[operatorkey])
  end

  #care for array staff
  #if array param_value, the staff should be selected in more optimized way: (selecting every given possibility instead of just one)
  if param_value.include? "{" and param_value.include? "}"
    array_to_be_found = param_value.clone
    array_to_be_found.remove!("{","}")
  
    if !where_condition[0].nil?
      where_condition[0] = delete_last_and(where_condition[0])
      where_condition[0] = "#{where_condition[0]} or "
    end
  
    array_to_be_found.split(",").each do |array_value|
      where_condition[0] = "#{where_condition[0]}(?) = ANY (#{param_name}) or "#question
      where_condition.push "#{array_value}" #values
    end
  end

  #return
  where_condition
end

.get_where_statement_from_param(params) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/easy_model_selects.rb', line 5

def self.get_where_statement_from_param(params)
  #prepare where statemant out of names and values in an array
    where_condition = []
    params.each do |param_name, param_value|
        where_condition = get_where_statement(param_name, param_value, where_condition)
    end
    where_condition[0] = delete_last_and(where_condition[0])
    where_condition
end