lib/sandboxy/sandboxed.rb in sandboxy-3.0.0 vs lib/sandboxy/sandboxed.rb in sandboxy-3.0.1

- old
+ new

@@ -1,102 +1,114 @@ +# frozen_string_literal: true + module Sandboxy - module Sandboxed + module Sandboxed + def self.included(base) + base.extend(ClassMethods) + end - def self.included base - base.extend ClassMethods - end + module ClassMethods + def sandboxy + has_one :sandbox, as: :sandboxed, dependent: :destroy + before_create :set_environment + include Sandboxy::Sandboxed::SandboxyInstanceMethods - module ClassMethods + default_scope { environment_scoped(Sandboxy.environment) } + scope :desandbox, -> { unscope(:joins, :where).all } - def sandboxy - has_one :sandbox, as: :sandboxed, dependent: :destroy - before_create :set_environment - include Sandboxy::Sandboxed::InstanceMethods + class << self + include Sandboxy::Sandboxed::SandboxyClassMethods + end + end + end - default_scope { self.environment_scoped(Sandboxy.environment) } - scope :desandbox, -> { unscope(:joins, :where).all } + module SandboxyClassMethods + def method_missing(method, *args) + if method.to_s[/(.+)_environment/] + environment($1) + elsif method.to_s[/(.+)_environment_scoped/] + environment_scoped($1) + else + super + end + end - def method_missing m, *args - if m.to_s[/(.+)_environment/] - self.environment $1 - elsif m.to_s[/(.+)_environment_scoped/] - self.environment_scoped $1 - else - super - end - end + def respond_to_missing?(method, include_private = false) + super || + method.to_s[/(.+)_environment/] || + method.to_s[/(.+)_environment_scoped/] + end - def respond_to? m, include_private = false - super || m.to_s[/(.+)_environment/] || m.to_s[/(.+)_environment_scoped/] - end + def environment(value) + unscope(:joins, :where).environment_scoped value + end - def environment value - unscope(:joins, :where).environment_scoped value - end + def environment_scoped(value) + case value + when Sandboxy.configuration.default + left_outer_joins(:sandbox).where(sandboxy: { environment: nil }) + else + left_outer_joins(:sandbox).where(sandboxy: { environment: value }) + end + end + end - def environment_scoped value - case value - when Sandboxy.configuration.default - left_outer_joins(:sandbox).where sandboxy: { environment: nil } - else - left_outer_joins(:sandbox).where sandboxy: { environment: value } - end - end - end - + module SandboxyInstanceMethods + def method_missing(method, *args) + if method.to_s[/move_environment_(.+)/] + move_environment($1) + elsif method.to_s[/(.+)_environment?/] + environment?($1) + else + super end + end - module InstanceMethods + def respond_to_missing?(method, include_private = false) + super || + method.to_s[/move_environment_(.+)/] || + method.to_s[/(.+)_environment?/] + end - def method_missing m, *args - if m.to_s[/move_environment_(.+)/] - self.move_environment $1 - elsif m.to_s[/(.+)_environment?/] - self.environment? $1 - else - super - end - end + def move_environment(environment) + case environment + when Sandboxy.configuration.default + move_to_default_environment + else + move_to_custom_environment(environment) + end + end - def respond_to? m, include_private = false - super || m.to_s[/move_environment_(.+)/] || m.to_s[/(.+)_environment?/] - end + def environment?(value) + environment == value + end - def move_environment value - case value - when Sandboxy.configuration.default - Sandbox.where(sandboxed_id: self.id, sandboxed_type: self.class.name).destroy_all - self.sandbox = nil - self.save! - else - unless self.sandbox.nil? - self.sandbox.update_attributes environment: value - else - sandbox = self.build_sandbox - sandbox.environment = value - sandbox.save! - end - end - end + def environment + return Sandboxy.configuration.default if sandbox.nil? - def environment? value - self.environment == value - end + sandbox.environment + end - def environment - return Sandboxy.configuration.default if self.sandbox.nil? - self.sandbox.environment - end + private - private + def set_environment + return if Sandboxy.environment == Sandboxy.configuration.default - def set_environment - unless Sandboxy.environment == Sandboxy.configuration.default - sandbox = self.build_sandbox - sandbox.environment = Sandboxy.environment - end - end + sandbox = build_sandbox + sandbox.environment = Sandboxy.environment + end - end + def move_to_default_environment + Sandbox.where(sandboxed_id: id, sandboxed_type: self.class.name) + .destroy_all + self.sandbox = nil + save! + end + def move_to_custom_environment(environment) + sandbox ||= build_sandbox + sandbox.environment = environment + sandbox.save! + end end + end end