Sha256: 155b92ad71cbefbcc512f5c4c3943bab71992e4970fec41b7ad612d10229827b

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module Workarea
  module Segmentable
    extend ActiveSupport::Concern

    included do
      field :active_segment_ids, type: Array, default: [], localize: true
      after_find :mark_segmented_content
    end

    class_methods do
      def active
        if embedded?
          scoped.select(&:active?)
        else
          Workarea.deprecation.warn(
            <<~eos.squish
              The active scope is being called on a root document. This won't
              respect segments. Rewrite this to use #active?, like: scope.select(&:active?)
            eos
          )
          scoped.where(active: true)
        end
      end

      def inactive
        if embedded?
          scoped.reject(&:active?)
        else
          Workarea.deprecation.warn(
            <<~eos.squish
              The inactive scope is being called on a root document. This won't
              respect segments. Rewrite this to use !#active?, like: scope.reject(&:active?)
            eos
          )
          scoped.where(active: false)
        end
      end
    end

    def active?
      default = super
      return default unless Segment.enabled?
      return false unless default

      if active_segment_ids.blank?
        true
      else
        allowed_ids = active_segment_ids.map(&:to_s)
        Segment.current.any? { |s| allowed_ids.include?(s.id.to_s) }
      end
    end

    def segments
      @segments ||= active_segment_ids.blank? ? [] : Segment.in(id: active_segment_ids)
    end

    private

    def mark_segmented_content
      # If loaded with `.only` this might be missing
      return if attribute_missing?(:active_segment_ids)

      CurrentSegments.has_segmented_content! if active_segment_ids.any?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
workarea-core-3.5.0.beta.1 app/models/workarea/segmentable.rb