Sha256: e3bd184e12755b30039b3108815b8f9de04f726bcf94ef30315f9289c5dbb5c3

Contents?: true

Size: 1.24 KB

Versions: 28

Compression:

Stored size: 1.24 KB

Contents

module Workarea
  class Segment
    include ApplicationDocument
    include Commentable
    include Mongoid::Document::Taggable

    field :name, type: String
    embeds_many :rules, class_name: 'Workarea::Segment::Rules::Base', inverse_of: :segment

    validates :name, presence: true
    validate do |segment| # Don't decorate this
      if Segment.count >= 15
        segment.errors.add(:base, I18n.t('workarea.errors.messages.max_allowed_segments'))
      end
    end

    def self.find_qualifying(visit)
      all.select { |s| s.qualifies?(visit) }
    end

    def self.current
      Thread.current[:current_segments] || []
    end

    def self.current=(*segments)
      Thread.current[:current_segments] = Array.wrap(segments).flatten
    end

    def self.enabled?
      !!Thread.current[:enable_segmentation]
    end

    def self.enabled
      Thread.current[:enable_segmentation] = true
      yield
    ensure
      Thread.current[:enable_segmentation] = nil
    end

    def self.with_current(*segments)
      previous = current

      self.current = segments
      enabled { yield }
    ensure
      self.current = previous
    end

    def qualifies?(visit)
      return false if visit.blank?
      rules.all? { |r| r.qualifies?(visit) }
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.6 app/models/workarea/segment.rb
workarea-core-3.5.5 app/models/workarea/segment.rb
workarea-core-3.5.4 app/models/workarea/segment.rb
workarea-core-3.5.3 app/models/workarea/segment.rb
workarea-core-3.5.2 app/models/workarea/segment.rb
workarea-core-3.5.1 app/models/workarea/segment.rb
workarea-core-3.5.0 app/models/workarea/segment.rb
workarea-core-3.5.0.beta.1 app/models/workarea/segment.rb