Module: Cell::Context::ClassMethods
- Defined in:
- lib/cell/context.rb
Class Method Summary collapse
Instance Method Summary collapse
- #after_set(*args, &block) ⇒ Object
- #around_use(*args, &block) ⇒ Object
- #current ⇒ Object
- #set!(tenant, exclusive: false) ⇒ Object
- #use(tenant, exclusive: false) ⇒ Object
Class Method Details
.extended(cls) ⇒ Object
9 10 11 12 |
# File 'lib/cell/context.rb', line 9 def self.extended(cls) cls.include ::ActiveSupport::Callbacks cls.define_callbacks :set_tenant, :use_tenant end |
Instance Method Details
#after_set(*args, &block) ⇒ Object
14 15 16 |
# File 'lib/cell/context.rb', line 14 def after_set(*args, &block) set_callback(:set_tenant, :after, *args, &block) end |
#around_use(*args, &block) ⇒ Object
18 19 20 |
# File 'lib/cell/context.rb', line 18 def around_use(*args, &block) set_callback(:use_tenant, :around, *args, &block) end |
#current ⇒ Object
22 23 24 |
# File 'lib/cell/context.rb', line 22 def current Thread.current[THREAD_KEY] end |
#set!(tenant, exclusive: false) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/cell/context.rb', line 26 def set!(tenant, exclusive: false) Meta.with_schema(tenant.schema_name, exclusive: exclusive) set_current(tenant) tenant.run_callbacks :set_tenant tenant end |
#use(tenant, exclusive: false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cell/context.rb', line 33 def use(tenant, exclusive: false) fail ArgumentError, "block required" unless block_given? saved_tenant = current if tenant Meta.with_schema(tenant.schema_name, exclusive: exclusive) do set_current(tenant) tenant.run_callbacks :use_tenant do yield Model.current end end else Meta.with_global_schema do set_current(nil) yield nil end end tenant ensure set_current(saved_tenant) end |