Module: Sandboxy::Sandboxed::ClassMethods

Defined in:
lib/sandboxy/sandboxed.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/sandboxy/sandboxed.rb', line 18

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

Instance Method Details

#environment(value) ⇒ Object



32
33
34
# File 'lib/sandboxy/sandboxed.rb', line 32

def environment value
    unscope(:joins, :where).environment_scoped value
end

#environment_scoped(value) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/sandboxy/sandboxed.rb', line 36

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

#respond_to?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/sandboxy/sandboxed.rb', line 28

def respond_to? m, include_private = false
    super || m.to_s[/(.+)_environment/] || m.to_s[/(.+)_environment_scoped/]
end

#sandboxyObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sandboxy/sandboxed.rb', line 10

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

    default_scope { self.environment_scoped(Sandboxy.environment) }
    scope :desandbox, -> { unscope(:joins, :where).all }

    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? 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_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