Module: Searchable

Included in:
ActiveLeopard::Base
Defined in:
lib/activeleopard/modules/searchable.rb

Instance Method Summary collapse

Instance Method Details

#find(id) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/activeleopard/modules/searchable.rb', line 23

def find(id)
  search_datum = DBConnection.get_first_row(<<-SQL)
    SELECT
      *
    FROM
      #{self.table_name}
    WHERE
      id = #{id}
  SQL
  raise RecordNotFound, "This record does not exist" if search_datum.nil?
  self.new(search_datum)
end

#find_by(params) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/activeleopard/modules/searchable.rb', line 2

def find_by(params)
  where_clause = WhereClause.new([params])

  search_datum = DBConnection.get_first_row(<<-SQL, where_clause.values)
    SELECT
      *
    FROM
      #{self.table_name}
    #{where_clause.as_sql}
  SQL

  search_datum.nil? ? nil : self.new(search_datum)
end

#group(group_attr) ⇒ Object



52
53
54
55
56
57
# File 'lib/activeleopard/modules/searchable.rb', line 52

def group(group_attr)
  Relation.new(
    {group: GroupClause.new(group_attr)},
    self
  )
end

#joins(association, _ = nil) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/activeleopard/modules/searchable.rb', line 36

def joins(association, _ = nil)
  options = self.assoc_options[association]

  Relation.new(
    {join: JoinOptions.new(options, self.table_name)},
    self
  )
end

#order(ordering_attr) ⇒ Object



59
60
61
62
63
64
# File 'lib/activeleopard/modules/searchable.rb', line 59

def order(ordering_attr)
  Relation.new(
    {order: OrderClause.new(ordering_attr)},
    self
  )
end

#select(*params) ⇒ Object



45
46
47
48
49
50
# File 'lib/activeleopard/modules/searchable.rb', line 45

def select(*params)
  Relation.new(
    {select: SelectClause.new(params)},
    self
  )
end

#where(*params) ⇒ Object



16
17
18
19
20
21
# File 'lib/activeleopard/modules/searchable.rb', line 16

def where(*params)
  Relation.new(
    {where: WhereClause.new(params)},
    self
  )
end