Module: Searchable

Included in:
LarisrecordBase
Defined in:
lib/laris/larisrecord/searchable.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/laris/larisrecord/searchable.rb', line 27

def method_missing(method_name, *args)
  if method_name.to_s.start_with?("find_by_")
    columns = method_name[8..-1].split('_and_')

    conditions = {}
    columns.size.times { |i| conditions[columns[i]] = args[i] }

    all.where(conditions).limit(1).first
  else
    all.send(method_name, *args)
  end
end

Instance Method Details

#allObject



2
3
4
# File 'lib/laris/larisrecord/searchable.rb', line 2

def all
  Relation.new(self)
end

#find(id) ⇒ Object



6
7
8
# File 'lib/laris/larisrecord/searchable.rb', line 6

def find(id)
  all.where(id: id).limit(1).first
end

#find_by(conditions) ⇒ Object



10
11
12
# File 'lib/laris/larisrecord/searchable.rb', line 10

def find_by(conditions)
  all.where(conditions).limit(1).first
end

#find_by_sql(sql, values = []) ⇒ Object



14
15
16
17
# File 'lib/laris/larisrecord/searchable.rb', line 14

def find_by_sql(sql, values = [])
  results = DBConnection.execute(sql, values)
  parse_all(results)
end

#firstObject



19
20
21
# File 'lib/laris/larisrecord/searchable.rb', line 19

def first
  all.order(:id).limit(1).first
end

#lastObject



23
24
25
# File 'lib/laris/larisrecord/searchable.rb', line 23

def last
  all.order(:id, :DESC).limit(1).first
end

#parse_all(results) ⇒ Object



40
41
42
# File 'lib/laris/larisrecord/searchable.rb', line 40

def parse_all(results)
  results.map { |params| new(params) }
end