Module: HasTokenId::FinderMethods
- Defined in:
- lib/has_token_id/finder_methods.rb
Instance Method Summary collapse
-
#find(*args) ⇒ Object
Find by token if the first param looks like a token, otherwise use super.
-
#find_by_case_insensitive_token(token) ⇒ Object
Find by token regardless of case.
-
#find_by_case_sensitive_token(token) ⇒ Object
Find by token ensuring case sensitivity.
-
#find_by_token(token) ⇒ Object
Find by token.
-
#find_by_token!(token) ⇒ Object
Find by token and raise error if no record is found.
Instance Method Details
#find(*args) ⇒ Object
Find by token if the first param looks like a token, otherwise use super
27 28 29 30 31 32 |
# File 'lib/has_token_id/finder_methods.rb', line 27 def find(*args) if args[0].is_a?(String) && args[0].length == [:length] record = find_by_token(args[0]) end record || super(*args) end |
#find_by_case_insensitive_token(token) ⇒ Object
Find by token regardless of case
10 11 12 |
# File 'lib/has_token_id/finder_methods.rb', line 10 def find_by_case_insensitive_token(token) 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 |
# File 'lib/has_token_id/finder_methods.rb', line 5 def find_by_case_sensitive_token(token) where("#{token_with_table_name} = ?", token).first end |
#find_by_token(token) ⇒ Object
Find by token
15 16 17 |
# File 'lib/has_token_id/finder_methods.rb', line 15 def find_by_token(token) send([: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
20 21 22 23 24 |
# File 'lib/has_token_id/finder_methods.rb', line 20 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 |