Class: TbRedirect

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/tb_redirect.rb

Class Method Summary collapse

Class Method Details

.create_smart(params) ⇒ Object



26
27
28
29
30
31
# File 'app/models/tb_redirect.rb', line 26

def self.create_smart(params)
  redirect = find_or_initialize_by(source: params.delete(:source))
  redirect.assign_attributes(params)
  redirect.save()
  return redirect
end

.find_with_uri(uri) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/tb_redirect.rb', line 14

def self.find_with_uri(uri)
  path = URI(uri).path
  uri_without_params = uri.split('?').first
  find_by(
    arel_table[:source].eq(path)
    .or(arel_table[:source].eq(uri_without_params))
  )
rescue URI::InvalidURIError => e
  logger.debug e.message
  return nil
end