Module: OrbitalFields::UrlLookup::ClassMethods

Defined in:
lib/orbital_fields/url_lookup.rb

Instance Method Summary collapse

Instance Method Details

#find_by_url_lookup(input, single_result = true) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/orbital_fields/url_lookup.rb', line 9

def find_by_url_lookup(input,single_result=true)
  input = input.strip
  return nil unless input =~ /^[a-z0-9\-]*$/i
  if single_result
    where(url_lookup: /^#{input}$/i).first
  else
    where(url_lookup: /^#{input}$/i)
  end
end

#url_lookup_field(lookup_field) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
# File 'lib/orbital_fields/url_lookup.rb', line 19

def url_lookup_field(lookup_field)
  raise ArgumentError, "Argument must be a symbol" unless lookup_field.class == Symbol
  raise NoMethodError, "The field '#{lookup_field.to_s}' is not defined" unless fields.include?(lookup_field.to_s)
  self.lookup_field = lookup_field
  field :url_lookup, type: String, default: ""
  validates self.lookup_field, url_lookup_field: true
  set_callback :save, :before, :url_lookup_callback
end