154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/magiq/query.rb', line 154
def self.by(column, opts = {}, &block)
param(column, {
solo: true,
type: :id,
alias: Magiq::Utils.pluralize(column.to_s).to_sym,
array: :always
}.merge(opts))
if block_given?
apply(column, &block)
else
apply(column) do |ids|
if ids.empty?
nil
else
tbl = model.table_name
sql = ids.each_with_index.map { |raw_id, i|
id = raw_id.is_a?(Numeric) ? raw_id : "'#{raw_id}'"
"WHEN #{id} THEN #{i}"
}.join(' ')
scope.where(column => ids).order("CASE #{tbl}.#{column} #{sql} END")
end
end
end
end
|