Class: ExampleQuery

Inherits:
Object
  • Object
show all
Includes:
Squirrell
Defined in:
lib/generators/sqrl/install/templates/example_query.rb

Constant Summary

Constants included from Squirrell

Squirrell::VERSION

Instance Method Summary collapse

Methods included from Squirrell

configure, included

Instance Method Details

#arelObject

#arel is where you might put Arel code. As long as the return value of the method responds to to_sql, you’re good. The code will be converted to SQL, executed, and passed into process.



25
26
27
28
29
# File 'lib/generators/sqrl/install/templates/example_query.rb', line 25

def arel
  users = User.arel_table
  users.where(users[:name].eq(@name))
    .project(users[:email])
end

#finderObject

#finder execute the code in the method and pass directly to #process. Great for ActiveRecord find_by, where, etc.



18
19
20
# File 'lib/generators/sqrl/install/templates/example_query.rb', line 18

def finder
  User.find_by(id: @id, name: @name, email: @email)
end

#process(result) ⇒ Object

process handles the result of the query before passing it to the caller. It’s defined by default like this.



12
13
14
# File 'lib/generators/sqrl/install/templates/example_query.rb', line 12

def process(result)
  result
end

#raw_sqlObject

#raw_sql is for raw sql code. It gets executed and passed to process.



32
33
34
# File 'lib/generators/sqrl/install/templates/example_query.rb', line 32

def raw_sql
  "SELECT * FROM users WHERE name = '#{@name}'"
end