Sha256: b4c4c8bbdfa8a9e8d3f525da06dea906e4f4a388744cc53dc242c0245efc3ceb

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

module Searchjoy
  class Search < ActiveRecord::Base
    self.table_name = "searchjoy_searches"

    belongs_to :convertable, polymorphic: true, optional: true
    belongs_to :user, optional: true
    has_many :conversions

    before_save :set_normalized_query

    def convert(convertable = nil)
      return unless Searchjoy.multiple_conversions || !converted?

      # use transaction to keep consistent
      self.class.transaction do
        # make time consistent
        now = Time.now

        if Searchjoy.multiple_conversions
          conversion =
            conversions.create!(
              convertable: convertable,
              created_at: now
            )
        end

        unless converted?
          self.converted_at = now
          # check id instead of association
          self.convertable = convertable if respond_to?(:convertable_id=)
          save(validate: false)
        end

        conversion
      end
    end

    def converted?
      converted_at.present?
    end

    protected

    def set_normalized_query
      self.normalized_query = query.downcase if query
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
searchjoy-1.3.2 app/models/searchjoy/search.rb
searchjoy-1.3.1 app/models/searchjoy/search.rb
searchjoy-1.3.0 app/models/searchjoy/search.rb
searchjoy-1.2.0 app/models/searchjoy/search.rb
searchjoy-1.1.0 app/models/searchjoy/search.rb
searchjoy-1.0.0 app/models/searchjoy/search.rb