• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MushroomObserver / mushroom-observer / 14145714091

29 Mar 2025 12:39PM UTC coverage: 84.27% (-9.7%) from 93.985%
14145714091

Pull #2808

github

web-flow
Merge 0843c72fa into 6655216a6
Pull Request #2808: Convert Query to AR scopes

472 of 2300 new or added lines in 78 files covered. (20.52%)

1381 existing lines in 27 files now uncovered.

26557 of 31514 relevant lines covered (84.27%)

537.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/app/classes/query/modules/initialization.rb
1
# frozen_string_literal: true
2

3
# Helper methods for turning Query parameters into SQL conditions.
UNCOV
4
module Query::Modules::Initialization
×
UNCOV
5
  attr_accessor :join, :tables, :where, :group, :order, :selects, :executor
×
6

UNCOV
7
  def initialized?
×
UNCOV
8
    @initialized ? true : false
×
UNCOV
9
  end
×
10

UNCOV
11
  def initialize_query
×
UNCOV
12
    @initialized = true
×
UNCOV
13
    @join        = []
×
UNCOV
14
    @tables      = []
×
UNCOV
15
    @where       = []
×
UNCOV
16
    @group       = ""
×
UNCOV
17
    @order       = ""
×
UNCOV
18
    @selects     = ""
×
UNCOV
19
    @executor    = nil
×
UNCOV
20
    initialize_flavor
×
UNCOV
21
    initialize_group
×
UNCOV
22
    initialize_order
×
UNCOV
23
    initialize_selects
×
UNCOV
24
  end
×
25

26
  # Make a value safe for SQL.
UNCOV
27
  def escape(val)
×
UNCOV
28
    model.connection.quote(val)
×
UNCOV
29
  end
×
30

31
  # Put together a list of ids for use in a "id IN (1,2,...)" condition.
32
  #
33
  #   set = clean_id_set(name.children)
34
  #   @where << "names.id IN (#{set})"
35
  #
UNCOV
36
  def clean_id_set(ids)
×
UNCOV
37
    set = limited_id_set(ids).map(&:to_s).join(",")
×
UNCOV
38
    set.presence || "-1"
×
UNCOV
39
  end
×
40

41
  # array of max of MO.query_max_array unique ids for use with Arel "in"
42
  #    where(<x>.in(limited_id_set(ids)))
UNCOV
43
  def limited_id_set(ids)
×
UNCOV
44
    ids.map(&:to_i).uniq[0, MO.query_max_array]
×
UNCOV
45
  end
×
46

47
  # Clean a pattern for use in LIKE condition.  Takes and returns a String.
UNCOV
48
  def clean_pattern(pattern)
×
UNCOV
49
    pattern.gsub(/[%'"\\]/) { |x| "\\#{x}" }.tr("*", "%")
×
UNCOV
50
  end
×
51

52
  # Combine args into one parenthesized condition by ANDing them.
UNCOV
53
  def and_clause(*args)
×
UNCOV
54
    if args.length > 1
×
UNCOV
55
      "(#{args.join(" AND ")})"
×
UNCOV
56
    else
×
UNCOV
57
      args.first
×
UNCOV
58
    end
×
UNCOV
59
  end
×
60

61
  # Combine args into one parenthesized condition by ORing them.
UNCOV
62
  def or_clause(*args)
×
UNCOV
63
    if args.length > 1
×
UNCOV
64
      "(#{args.join(" OR ")})"
×
UNCOV
65
    else
×
UNCOV
66
      args.first
×
UNCOV
67
    end
×
UNCOV
68
  end
×
69

70
  # Add a join condition if it doesn't already exist.  There are two forms:
71
  #
72
  #   # Add join from root table to the given table:
73
  #   add_join(:observations)
74
  #     => join << :observations
75
  #
76
  #   # Add join from one table to another: (will create join from root to
77
  #   # first table if it doesn't already exist)
78
  #   add_join(:observations, :names)
79
  #     => join << {:observations => :names}
80
  #   add_join(:names, :descriptions)
81
  #     => join << {:observations => {:names => :descriptions}}
82
  #
UNCOV
83
  def add_join(*)
×
UNCOV
84
    @join.add_leaf(*)
×
UNCOV
85
  end
×
86

87
  # Same as add_join but can provide chain of more than two tables.
UNCOV
88
  def add_joins(*args)
×
UNCOV
89
    if args.length == 1
×
UNCOV
90
      @join.add_leaf(args[0])
×
UNCOV
91
    elsif args.length > 1
×
UNCOV
92
      while args.length > 1
×
UNCOV
93
        @join.add_leaf(args[0], args[1])
×
UNCOV
94
        args.shift
×
UNCOV
95
      end
×
UNCOV
96
    end
×
UNCOV
97
  end
×
98

99
  # Safely add to :where in +args+. Dups <tt>args[:where]</tt>,
100
  # casts it into an Array, and returns the new Array.
UNCOV
101
  def extend_where(args)
×
UNCOV
102
    extend_arg(args, :where)
×
UNCOV
103
  end
×
104

105
  # Safely add to :join in +args+.  Dups <tt>args[:join]</tt>, casts it into
106
  # an Array, and returns the new Array.
UNCOV
107
  def extend_join(args)
×
UNCOV
108
    extend_arg(args, :join)
×
UNCOV
109
  end
×
110

111
  # Safely add to +arg+ in +args+.  Dups <tt>args[arg]</tt>, casts it into
112
  # an Array, and returns the new Array.
UNCOV
113
  def extend_arg(args, arg)
×
UNCOV
114
    args[arg] = case old_arg = args[arg]
×
UNCOV
115
                when Symbol, String
×
116
                  [old_arg]
×
UNCOV
117
                when Array
×
118
                  old_arg.dup
×
UNCOV
119
                else
×
UNCOV
120
                  []
×
UNCOV
121
                end
×
UNCOV
122
  end
×
123

124
  # params[:group] is not sanitized or validated.
UNCOV
125
  def initialize_group
×
UNCOV
126
    return if params[:group].blank?
×
127

UNCOV
128
    self.group = params[:group]
×
UNCOV
129
  end
×
130

131
  # params[:selects] is not sanitized or validated.
UNCOV
132
  def initialize_selects
×
UNCOV
133
    if params[:selects].blank?
×
UNCOV
134
      @selects = "DISTINCT #{model.table_name}.id"
×
UNCOV
135
      return
×
UNCOV
136
    end
×
137

UNCOV
138
    self.selects = params[:selects]
×
UNCOV
139
  end
×
UNCOV
140
end
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc