Module: Leases::Model::Base
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/leases/model/base.rb
Instance Method Summary collapse
-
#break! ⇒ Object
Break a lease.
-
#enter ⇒ Object
Enter the leaser-context.
-
#lease! ⇒ Object
Create a new lease.
-
#leaser_name ⇒ Object
Name of the leaser, used for naming the database.
-
#leave ⇒ Object
Leave the leaser-context.
-
#visit(&block) ⇒ Object
Visit a leaser by entering and leaving.
Instance Method Details
#break! ⇒ Object
Break a lease. This is usually called when a model is destroyed.
Example
account.break!
91 92 93 |
# File 'lib/leases/model/base.rb', line 91 def break! Apartment::Tenant.drop(leaser_name) end |
#enter ⇒ Object
Enter the leaser-context.
Example
account.enter
37 38 39 40 |
# File 'lib/leases/model/base.rb', line 37 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!
79 80 81 |
# File 'lib/leases/model/base.rb', line 79 def lease! Apartment::Tenant.create(leaser_name) end |
#leaser_name ⇒ Object
Name of the leaser, used for naming the database. This can be set in the leaser_options. Must be unique.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/leases/model/base.rb', line 18 def leaser_name name = self.class.[:name] if name.nil? [self.class.name.parameterize, id].join('-') elsif name.is_a?(Symbol) send(name) elsif name.is_a?(Proc) name.call(self) end end |
#leave ⇒ Object
Leave the leaser-context.
Example
account.leave
49 50 51 52 |
# File 'lib/leases/model/base.rb', line 49 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) }
62 63 64 65 66 67 68 69 |
# File 'lib/leases/model/base.rb', line 62 def visit(&block) enter begin yield ensure leave end end |