Module: DatabaseFunctionHelper

Defined in:
app/models/database_function_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.select_function(*args) ⇒ Object



2
3
4
5
6
7
# File 'app/models/database_function_helper.rb', line 2

def self.select_function(*args)
  obj = Object.new
  obj.extend(DatabaseFunctionHelper)

  obj.select_function(*args)
end

Instance Method Details

#select_function(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/database_function_helper.rb', line 9

def select_function(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}

  sql = [ 'SELECT', function_sql(*args) ].join(' ')
  results = ActiveRecord::Base.connection.execute(sql)

  if type = options[:one]
    return nil unless results.first
    return cast_one(type, results.first.values.first)
  elsif type = options[:values]
    return cast_values(type, results)
  end

  results
end