Module: HasTokenId::FinderMethods

Defined in:
lib/has_token_id/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object

Find by token if the first param looks like a token, otherwise use super



30
31
32
33
34
35
# File 'lib/has_token_id/finder_methods.rb', line 30

def find(*args)
  if args[0].is_a?(String) && args[0].length == has_token_id_options[:length]
    record = find_by_token(args[0])
  end
  record || super(*args)
end

#find!(*args) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


37
38
39
40
41
# File 'lib/has_token_id/finder_methods.rb', line 37

def find!(*args)
  record = find(*args)
  raise ActiveRecord::RecordNotFound if record.nil?
  record
end

#find_by_case_insensitive_token(token) ⇒ Object

Find by token regardless of case



11
12
13
14
# File 'lib/has_token_id/finder_methods.rb', line 11

def find_by_case_insensitive_token(token)
  return if token.nil?
  where("lower(#{token_with_table_name}) = ?", token.downcase).first
end

#find_by_case_sensitive_token(token) ⇒ Object

Find by token ensuring case sensitivity



5
6
7
8
# File 'lib/has_token_id/finder_methods.rb', line 5

def find_by_case_sensitive_token(token)
  return if token.nil?
  where("#{token_with_table_name} = ?", token).first
end

#find_by_token(token) ⇒ Object

Find by token



17
18
19
20
# File 'lib/has_token_id/finder_methods.rb', line 17

def find_by_token(token)
  return if token.nil?
  send(has_token_id_options[:case_sensitive] ? :find_by_case_sensitive_token : :find_by_case_insensitive_token, token)
end

#find_by_token!(token) ⇒ Object

Find by token and raise error if no record is found

Raises:

  • (ActiveRecord::RecordNotFound)


23
24
25
26
27
# File 'lib/has_token_id/finder_methods.rb', line 23

def find_by_token!(token)
  record = find_by_token(token)
  raise ActiveRecord::RecordNotFound, "Could not find #{self.name} with token #{token.inspect}" if record.nil?
  record
end