Module: HasTokenable::FinderMethods

Defined in:
lib/has_tokenable/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



24
25
26
27
28
29
# File 'lib/has_tokenable/finder_methods.rb', line 24

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

#find!(*args) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


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

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

#find_by_case_sensitive_token(token) ⇒ Object

Find by token ensuring case sensitivity



5
6
7
8
# File 'lib/has_tokenable/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



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

def find_by_token(token)
  return if token.nil?
  send(:find_by_case_sensitive_token , token)
end

#find_by_token!(token) ⇒ Object

Find by token and raise error if no record is found

Raises:

  • (ActiveRecord::RecordNotFound)


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

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