Module: Leases::Model::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/leases/model/base.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#break!Object

Break a lease. This is usually called when a model is destroyed.

Example

account.break!



127
128
129
# File 'lib/leases/model/base.rb', line 127

def break!
  Apartment::Tenant.drop(leaser_name)
end

#enterObject

Enter the leaser-context.

Example

account.enter



73
74
75
76
# File 'lib/leases/model/base.rb', line 73

def enter
  Apartment::Tenant.switch(leaser_name)
  Leases.current = self
end

#lease!Object

Create a new lease. This is usually called when a model is created.

Example

account.lease!



115
116
117
# File 'lib/leases/model/base.rb', line 115

def lease!
  Apartment::Tenant.create(leaser_name)
end

#leaser_nameObject

Name of the leaser, used for naming the database. This can be set in the leaser_options. Must be unique.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/leases/model/base.rb', line 54

def leaser_name
  name = self.class.leases_options[:name]

  if name.is_a?(Symbol)
    send(name)
  elsif name.is_a?(Proc)
    name.call(self)
  else
    [self.class.name.parameterize, id].join('-')
  end
end

#leaveObject

Leave the leaser-context.

Example

account.leave



85
86
87
88
# File 'lib/leases/model/base.rb', line 85

def leave
  Leases.current = nil
  Apartment::Tenant.reset
end

#visit(&block) ⇒ Object

Visit a leaser by entering and leaving. Very useful for executing code in a leaser-context

Example

account.visit { User.find(1) }



98
99
100
101
102
103
104
105
# File 'lib/leases/model/base.rb', line 98

def visit(&block)
  enter
  begin
    yield
  ensure
    leave
  end
end