Sha256: b985c11c4f61c62145b13c4d89e907730ae598667a2a2b0b62c0b444cb08a351

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Sandboxy
    module Sandboxed

        def self.included base
            base.extend ClassMethods
        end

        module ClassMethods

            def sandboxy
                has_one :sandbox, as: :sandboxed, dependent: :destroy
                include Sandboxy::Sandboxed::InstanceMethods

                scope :live_scoped, -> { left_outer_joins(:sandbox).where(sandbox: { id: nil }) }
                scope :sandboxed_scoped, -> { left_outer_joins(:sandbox).where.not(sandbox: { id: nil }) }
                default_scope {
                    case $sandbox
                    when true then sandboxed_scoped
                    when false then live_scoped
                    end
                }
                scope :live, -> { unscope(:joins, :where).live_scoped }
                scope :sandboxed, -> { unscope(:joins, :where).sandboxed_scoped }
                scope :desandbox, -> { unscope(:joins, :where).all }

                # before_save :make_sandboxed # -> should be handled automatically through default_scope
            end

        end

        module InstanceMethods

            def make_sandboxed
                self.build_sandbox unless self.sandbox.present?
            end

            def make_live
                self.sandbox.destroy if self.sandbox.present?
            end

            def sandboxed?
                self.sandbox.present?
            end

            def live?
                !self.sandboxed?
            end

        end

    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sandboxy-2.0.0 lib/sandboxy/sandboxed.rb
sandboxy-1.1.1 lib/sandboxy/sandboxed.rb
sandboxy-1.1.0 lib/sandboxy/sandboxed.rb