Module: Bunster::FindBySqlFile

Defined in:
lib/bunster/find_by_sql_file.rb

Constant Summary collapse

SQL_PATH =

The path to the directory query queries will be found, organized inside directories named like their main tables (or ‘application’ for shared queries)

File.join RAILS_ROOT, 'app', 'queries'

Instance Method Summary collapse

Instance Method Details

#count_by_sql_with_sql_file(query_or_symbol, opts = {}) ⇒ Object

:nodoc:



13
14
15
# File 'lib/bunster/find_by_sql_file.rb', line 13

def count_by_sql_with_sql_file(query_or_symbol, opts = {}) # :nodoc:
  count_by_sql_without_sql_file sql_file(query_or_symbol, opts)
end

#find_by_sql_with_sql_file(query_or_symbol, opts = {}) ⇒ Object

:nodoc:



9
10
11
# File 'lib/bunster/find_by_sql_file.rb', line 9

def find_by_sql_with_sql_file(query_or_symbol, opts = {}) # :nodoc:
  find_by_sql_without_sql_file sql_file(query_or_symbol, opts)
end

#sql_file(query_or_symbol, opts = {}) ⇒ Object

:nodoc:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bunster/find_by_sql_file.rb', line 17

def sql_file(query_or_symbol, opts = {}) # :nodoc:
  if query_or_symbol.is_a? Symbol

    file_name = File.join SQL_PATH,
      (table_name rescue 'application'), (query_or_symbol.to_s + '.sql')

    bound_variables = HashWithIndifferentAccess.new(opts).symbolize_keys!
    injected_locals = bound_variables.delete(:inject!) || []

    query = ERBJacket.wrap File.read(file_name), injected_locals
    query = replace_named_bind_variables(query, bound_variables)

    query.gsub(/--.*/, '').squish!
  else
    raise %' Additional parameters only supported when using a query
      file (pass a symbol, not a string) '.squish! unless opts.blank?

    query_or_symbol
  end
end