Module: RailsMultitenant::MultitenantModel::ClassMethods
- Defined in:
- lib/rails_multitenant/multitenant_model.rb
Instance Method Summary collapse
- #multitenant_on(context_entity_id_field, required: true) ⇒ Object
- #multitenant_on_model(context_entity, required: true) ⇒ Object
- #validates_multitenant_uniqueness_of(*attr_names) ⇒ Object
Instance Method Details
#multitenant_on(context_entity_id_field, required: true) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rails_multitenant/multitenant_model.rb', line 13 def multitenant_on(context_entity_id_field, required: true) self.context_entity_id_field = context_entity_id_field validates_presence_of(context_entity_id_field) if required context_entity = context_entity_id_field.to_s.gsub(/_id$/, '') scope_sym = "from_current_#{context_entity}".to_sym scope scope_sym, -> do unless GlobalContextRegistry.use_unscoped_queries? where(context_entity_id_field => GlobalContextRegistry[context_entity_id_field]) end end default_scope { send(scope_sym) } scope "strip_#{context_entity}_scope", -> do unscope(where: context_entity_id_field) end end |
#multitenant_on_model(context_entity, required: true) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/rails_multitenant/multitenant_model.rb', line 33 def multitenant_on_model(context_entity, required: true) multitenant_on("#{context_entity}_id".to_sym, required: required) # Rails 5 added required validation to belongs_to associations and # an `optional` setting to disable it. We already do validation on # the foreign key so we always disable the native Rails validation. belongs_to(context_entity, optional: true) end |
#validates_multitenant_uniqueness_of(*attr_names) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/rails_multitenant/multitenant_model.rb', line 42 def validates_multitenant_uniqueness_of(*attr_names) = attr_names..symbolize_keys existing_scope = Array.wrap(.delete(:scope)) scope = existing_scope | [context_entity_id_field] validates_uniqueness_of(*attr_names, .merge(scope: scope)) end |