Class: Searchkick::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/searchkick/relation.rb

Constant Summary collapse

NO_DEFAULT_VALUE =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, term = "*", **options) ⇒ Relation

Returns a new instance of Relation.



14
15
16
17
18
19
20
21
# File 'lib/searchkick/relation.rb', line 14

def initialize(model, term = "*", **options)
  @model = model
  @term = term
  @options = options

  # generate query to validate options
  query
end

Instance Attribute Details

#modelObject (readonly) Also known as: klass

Returns the value of attribute model.



11
12
13
# File 'lib/searchkick/relation.rb', line 11

def model
  @model
end

Instance Method Details

#except(*keys) ⇒ Object

experimental



196
197
198
# File 'lib/searchkick/relation.rb', line 196

def except(*keys)
  Relation.new(@model, @term, **@options.except(*keys))
end

#executeObject



30
31
32
33
# File 'lib/searchkick/relation.rb', line 30

def execute
  Searchkick.warn("The execute method is no longer needed")
  load
end

#includes(*values) ⇒ Object

experimental



179
180
181
# File 'lib/searchkick/relation.rb', line 179

def includes(*values)
  clone.includes!(*values)
end

#includes!(*values) ⇒ Object

experimental



184
185
186
187
188
# File 'lib/searchkick/relation.rb', line 184

def includes!(*values)
  check_loaded
  (@options[:includes] ||= []).concat(values)
  self
end

#inspectObject

same as Active Record



24
25
26
27
28
# File 'lib/searchkick/relation.rb', line 24

def inspect
  entries = results.first(11).map!(&:inspect)
  entries[10] = "..." if entries.size == 11
  "#<#{self.class.name} [#{entries.join(', ')}]>"
end

#limit(value) ⇒ Object

experimental



36
37
38
# File 'lib/searchkick/relation.rb', line 36

def limit(value)
  clone.limit!(value)
end

#limit!(value) ⇒ Object

experimental



41
42
43
44
45
# File 'lib/searchkick/relation.rb', line 41

def limit!(value)
  check_loaded
  @options[:limit] = value
  self
end

#loadObject

experimental



201
202
203
204
# File 'lib/searchkick/relation.rb', line 201

def load
  private_execute
  self
end

#loaded?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/searchkick/relation.rb', line 206

def loaded?
  !@execute.nil?
end

#offset(value = NO_DEFAULT_VALUE) ⇒ Object

experimental



48
49
50
51
52
53
54
55
# File 'lib/searchkick/relation.rb', line 48

def offset(value = NO_DEFAULT_VALUE)
  # TODO remove in Searchkick 6
  if value == NO_DEFAULT_VALUE
    private_execute.offset
  else
    clone.offset!(value)
  end
end

#offset!(value) ⇒ Object

experimental



58
59
60
61
62
# File 'lib/searchkick/relation.rb', line 58

def offset!(value)
  check_loaded
  @options[:offset] = value
  self
end

#only(*keys) ⇒ Object

experimental



191
192
193
# File 'lib/searchkick/relation.rb', line 191

def only(*keys)
  Relation.new(@model, @term, **@options.slice(*keys))
end

#order(*values) ⇒ Object

experimental



126
127
128
# File 'lib/searchkick/relation.rb', line 126

def order(*values)
  clone.order!(*values)
end

#order!(*values) ⇒ Object

experimental



131
132
133
134
135
136
# File 'lib/searchkick/relation.rb', line 131

def order!(*values)
  values = values.first if values.size == 1 && values.first.is_a?(Array)
  check_loaded
  (@options[:order] ||= []).concat(values)
  self
end

#page(value) ⇒ Object

experimental



65
66
67
# File 'lib/searchkick/relation.rb', line 65

def page(value)
  clone.page!(value)
end

#page!(value) ⇒ Object

experimental



70
71
72
73
74
# File 'lib/searchkick/relation.rb', line 70

def page!(value)
  check_loaded
  @options[:page] = value
  self
end

#per_page(value = NO_DEFAULT_VALUE) ⇒ Object

experimental



77
78
79
80
81
82
83
84
# File 'lib/searchkick/relation.rb', line 77

def per_page(value = NO_DEFAULT_VALUE)
  # TODO remove in Searchkick 6
  if value == NO_DEFAULT_VALUE
    private_execute.per_page
  else
    clone.per_page!(value)
  end
end

#per_page!(value) ⇒ Object

experimental



87
88
89
90
91
# File 'lib/searchkick/relation.rb', line 87

def per_page!(value)
  check_loaded
  @options[:per_page] = value
  self
end

#reorder(*values) ⇒ Object

experimental



139
140
141
# File 'lib/searchkick/relation.rb', line 139

def reorder(*values)
  clone.reorder!(*values)
end

#reorder!(*values) ⇒ Object

experimental



144
145
146
147
148
# File 'lib/searchkick/relation.rb', line 144

def reorder!(*values)
  check_loaded
  @options[:order] = values
  self
end

#reselect(*values) ⇒ Object

experimental



167
168
169
# File 'lib/searchkick/relation.rb', line 167

def reselect(*values)
  clone.reselect!(*values)
end

#reselect!(*values) ⇒ Object

experimental



172
173
174
175
176
# File 'lib/searchkick/relation.rb', line 172

def reselect!(*values)
  check_loaded
  @options[:select] = values
  self
end

#respond_to_missing?(method_name, include_all) ⇒ Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/searchkick/relation.rb', line 210

def respond_to_missing?(method_name, include_all)
  Results.new(nil, nil, nil).respond_to?(method_name, include_all) || super
end

#rewhere(value) ⇒ Object

experimental



114
115
116
# File 'lib/searchkick/relation.rb', line 114

def rewhere(value)
  clone.rewhere!(value)
end

#rewhere!(value) ⇒ Object

experimental



119
120
121
122
123
# File 'lib/searchkick/relation.rb', line 119

def rewhere!(value)
  check_loaded
  @options[:where] = ensure_permitted(value)
  self
end

#select(*values, &block) ⇒ Object

experimental



151
152
153
154
155
156
157
# File 'lib/searchkick/relation.rb', line 151

def select(*values, &block)
  if block_given?
    private_execute.select(*values, &block)
  else
    clone.select!(*values)
  end
end

#select!(*values) ⇒ Object

experimental



160
161
162
163
164
# File 'lib/searchkick/relation.rb', line 160

def select!(*values)
  check_loaded
  (@options[:select] ||= []).concat(values)
  self
end

#where(value = NO_DEFAULT_VALUE) ⇒ Object

experimental



94
95
96
97
98
99
100
# File 'lib/searchkick/relation.rb', line 94

def where(value = NO_DEFAULT_VALUE)
  if value == NO_DEFAULT_VALUE
    Where.new(self)
  else
    clone.where!(value)
  end
end

#where!(value) ⇒ Object

experimental



103
104
105
106
107
108
109
110
111
# File 'lib/searchkick/relation.rb', line 103

def where!(value)
  check_loaded
  if @options[:where]
    @options[:where] = {_and: [@options[:where], ensure_permitted(value)]}
  else
    @options[:where] = ensure_permitted(value)
  end
  self
end