Sha256: abdddca7742e9d341e9fc041cedc11a194e3473d2d88c6710dd97c34eb7cf159

Contents?: true

Size: 1.39 KB

Versions: 20

Compression:

Stored size: 1.39 KB

Contents

module Snaptime
  module Versioned
    module Scopes
      extend ActiveSupport::Concern

      included do
        default_scope do
          current_version
        end
      end

      module ClassMethods
        def current_version
          snaptime = Snaptime.snaptime
          _at_explicit_snaptime(snaptime)
        end

        def _at_explicit_snaptime(snaptime = nil)
          if snaptime.nil?
            where(valid_to: nil, deleted: false)
          else
            where(
              arel_table[:valid_from].lteq(snaptime).and(
                arel_table[:valid_to].eq(nil).or(
                  arel_table[:valid_to].gteq(snaptime)
                )
              ).and(
                arel_table[:deleted].eq(false)
              )
            )
          end
        end

        def at_snaptime
          _at_explicit_snaptime Snaptime.snaptime
        end
      end

      def snaptimes
        Harvester.harvest_for(self)
      end

      def with_snaptime(snaptime = nil)
        Snaptime.with_snaptime(snaptime) do
          yield at_snaptime
        end
      end

      def at_snaptime
        _at_explicit_snaptime Snaptime.snaptime
      end

      def all_versions
        self.class.unscoped.where('natural_id = ?', natural_id)
      end

      private

      def _at_explicit_snaptime(snaptime = nil)
        all_versions._at_explicit_snaptime(snaptime).first
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
snaptime-22.0.0 lib/snaptime/versioned/scopes.rb
snaptime-21.0.0 lib/snaptime/versioned/scopes.rb
snaptime-0.3.0 lib/snaptime/versioned/scopes.rb
snaptime-0.2.0 lib/snaptime/versioned/scopes.rb
snaptime-0.0.20 lib/snaptime/versioned/scopes.rb
snaptime-0.0.19 lib/snaptime/versioned/scopes.rb
snaptime-0.0.18 lib/snaptime/versioned/scopes.rb
snaptime-0.0.17 lib/snaptime/versioned/scopes.rb
snaptime-0.0.16 lib/snaptime/versioned/scopes.rb
snaptime-0.0.15 lib/snaptime/versioned/scopes.rb
snaptime-0.0.14 lib/snaptime/versioned/scopes.rb
snaptime-0.0.13 lib/snaptime/versioned/scopes.rb
snaptime-0.0.12 lib/snaptime/versioned/scopes.rb
snaptime-0.0.11 lib/snaptime/versioned/scopes.rb
snaptime-0.0.10 lib/snaptime/versioned/scopes.rb
snaptime-0.0.9 lib/snaptime/versioned/scopes.rb
snaptime-0.0.8 lib/snaptime/versioned/scopes.rb
snaptime-0.0.7 lib/snaptime/versioned/scopes.rb
snaptime-0.0.6 lib/snaptime/versioned/scopes.rb
snaptime-0.0.1 lib/snaptime/versioned/scopes.rb