Sha256: 3a2e52e8715121e2505b012e843835b1a799f88d5f4e33ac907e689e3b7a2d5a
Contents?: true
Size: 1.46 KB
Versions: 1
Compression:
Stored size: 1.46 KB
Contents
class TbRedirect < ActiveRecord::Base scope :ordered, ->{ order('source desc') } scope :search, ->(term){ where('source LIKE ?', "%#{term}%") } belongs_to :owner, :polymorphic => true, :inverse_of => :tb_redirects validates :source, :destination, :presence => true validates :source, :uniqueness => true validate :source_not_equals_destination after_save :destroy_opposite_redirect after_save :schedule_loop_detection def self.find_by_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 def self.create_smart(params) redirect = find_or_initialize_by(:source => params.delete(:source)) redirect.assign_attributes(params) redirect.save() return redirect end private def source_not_equals_destination if self.source == self.destination errors.add(:destination, 'cannot be the same as the source') end return true end def destroy_opposite_redirect opposite = TbRedirect.find_by({ :source => self.destination, :destination => self.source }) opposite.destroy if opposite.present? return true end def schedule_loop_detection if TbRedirect.where(:destination => source).count > 0 TbRedirects::DetectRedirectLoopJob.perform_later(self) end return true end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
tb_redirects-0.0.1 | app/models/tb_redirect.rb |