activerecord-multi-tenant 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/active-record-multi-tenant-tests.yml +80 -0
  3. data/.gitignore +6 -0
  4. data/.readthedocs.yaml +15 -0
  5. data/.rspec +0 -0
  6. data/.rubocop.yml +51 -0
  7. data/Appraisals +0 -0
  8. data/CHANGELOG.md +6 -0
  9. data/Gemfile +3 -1
  10. data/LICENSE +0 -0
  11. data/README.md +2 -1
  12. data/Rakefile +1 -1
  13. data/activerecord-multi-tenant.gemspec +28 -22
  14. data/docker-compose.yml +24 -18
  15. data/docs/.gitignore +3 -0
  16. data/docs/Makefile +28 -0
  17. data/docs/api-reference.sh +10 -0
  18. data/docs/requirements.in +4 -0
  19. data/docs/requirements.txt +62 -0
  20. data/docs/source/_static/api-reference/ActiveRecord/Associations/Association.html +285 -0
  21. data/docs/source/_static/api-reference/ActiveRecord/Associations/ClassMethods.html +255 -0
  22. data/docs/source/_static/api-reference/ActiveRecord/Associations.html +117 -0
  23. data/docs/source/_static/api-reference/ActiveRecord/ConnectionAdapters/SchemaStatements.html +232 -0
  24. data/docs/source/_static/api-reference/ActiveRecord/ConnectionAdapters.html +126 -0
  25. data/docs/source/_static/api-reference/ActiveRecord/QueryMethods.html +336 -0
  26. data/docs/source/_static/api-reference/ActiveRecord/SchemaDumper.html +121 -0
  27. data/docs/source/_static/api-reference/ActiveRecord.html +130 -0
  28. data/docs/source/_static/api-reference/MultiTenant/ArelTenantVisitor.html +755 -0
  29. data/docs/source/_static/api-reference/MultiTenant/ArelVisitorsDepthFirst.html +208 -0
  30. data/docs/source/_static/api-reference/MultiTenant/BaseTenantEnforcementClause.html +462 -0
  31. data/docs/source/_static/api-reference/MultiTenant/Context.html +659 -0
  32. data/docs/source/_static/api-reference/MultiTenant/ControllerExtensions.html +202 -0
  33. data/docs/source/_static/api-reference/MultiTenant/CopyFromClient.html +186 -0
  34. data/docs/source/_static/api-reference/MultiTenant/CopyFromClientHelper.html +362 -0
  35. data/docs/source/_static/api-reference/MultiTenant/Current.html +124 -0
  36. data/docs/source/_static/api-reference/MultiTenant/DatabaseStatements.html +366 -0
  37. data/docs/source/_static/api-reference/MultiTenant/FastTruncate.html +226 -0
  38. data/docs/source/_static/api-reference/MultiTenant/MigrationExtensions.html +554 -0
  39. data/docs/source/_static/api-reference/MultiTenant/MissingTenantError.html +124 -0
  40. data/docs/source/_static/api-reference/MultiTenant/ModelExtensionsClassMethods.html +492 -0
  41. data/docs/source/_static/api-reference/MultiTenant/QueryMonitor.html +257 -0
  42. data/docs/source/_static/api-reference/MultiTenant/Table.html +419 -0
  43. data/docs/source/_static/api-reference/MultiTenant/TenantEnforcementClause.html +148 -0
  44. data/docs/source/_static/api-reference/MultiTenant/TenantIsImmutable.html +135 -0
  45. data/docs/source/_static/api-reference/MultiTenant/TenantJoinEnforcementClause.html +310 -0
  46. data/docs/source/_static/api-reference/MultiTenant/TenantValueVisitor.html +239 -0
  47. data/docs/source/_static/api-reference/MultiTenant.html +1454 -0
  48. data/docs/source/_static/api-reference/MultiTenantFindBy.html +180 -0
  49. data/docs/source/_static/api-reference/Sidekiq/Client.html +302 -0
  50. data/docs/source/_static/api-reference/Sidekiq/Middleware/MultiTenant/Client.html +217 -0
  51. data/docs/source/_static/api-reference/Sidekiq/Middleware/MultiTenant/Server.html +219 -0
  52. data/docs/source/_static/api-reference/Sidekiq/Middleware/MultiTenant.html +126 -0
  53. data/docs/source/_static/api-reference/Sidekiq.html +126 -0
  54. data/docs/source/_static/api-reference/_index.html +399 -0
  55. data/docs/source/_static/api-reference/class_list.html +51 -0
  56. data/docs/source/_static/api-reference/css/common.css +1 -0
  57. data/docs/source/_static/api-reference/css/full_list.css +58 -0
  58. data/docs/source/_static/api-reference/css/style.css +497 -0
  59. data/docs/source/_static/api-reference/file.README.html +167 -0
  60. data/docs/source/_static/api-reference/file_list.html +56 -0
  61. data/docs/source/_static/api-reference/frames.html +17 -0
  62. data/docs/source/_static/api-reference/index.html +167 -0
  63. data/docs/source/_static/api-reference/js/app.js +314 -0
  64. data/docs/source/_static/api-reference/js/full_list.js +216 -0
  65. data/docs/source/_static/api-reference/js/jquery.js +4 -0
  66. data/docs/source/_static/api-reference/method_list.html +715 -0
  67. data/docs/source/_static/api-reference/top-level-namespace.html +126 -0
  68. data/docs/source/_templates/.gitignore +4 -0
  69. data/docs/source/api-reference.rst +8 -0
  70. data/docs/source/appendix.rst +26 -0
  71. data/docs/source/changelog.rst +8 -0
  72. data/docs/source/community-and-support.rst +26 -0
  73. data/docs/source/conf.py +30 -0
  74. data/docs/source/contributing.rst +70 -0
  75. data/docs/source/getting-started.rst +37 -0
  76. data/docs/source/guides-and-tutorials.rst +129 -0
  77. data/docs/source/index.rst +54 -0
  78. data/docs/source/introduction.rst +33 -0
  79. data/docs/source/license.rst +22 -0
  80. data/docs/source/troubleshooting.rst +41 -0
  81. data/docs/source/usage-guide.rst +59 -0
  82. data/lib/activerecord-multi-tenant/arel_visitors_depth_first.rb +183 -174
  83. data/lib/activerecord-multi-tenant/controller_extensions.rb +15 -4
  84. data/lib/activerecord-multi-tenant/copy_from_client.rb +4 -0
  85. data/lib/activerecord-multi-tenant/fast_truncate.rb +4 -2
  86. data/lib/activerecord-multi-tenant/habtm.rb +50 -0
  87. data/lib/activerecord-multi-tenant/migrations.rb +18 -8
  88. data/lib/activerecord-multi-tenant/model_extensions.rb +78 -37
  89. data/lib/activerecord-multi-tenant/multi_tenant.rb +40 -21
  90. data/lib/activerecord-multi-tenant/query_monitor.rb +21 -5
  91. data/lib/activerecord-multi-tenant/query_rewriter.rb +111 -80
  92. data/lib/activerecord-multi-tenant/sidekiq.rb +31 -20
  93. data/lib/activerecord-multi-tenant/version.rb +1 -1
  94. data/lib/activerecord-multi-tenant.rb +3 -12
  95. data/lib/activerecord_multi_tenant.rb +12 -0
  96. data/spec/activerecord-multi-tenant/associations_spec.rb +21 -0
  97. data/spec/activerecord-multi-tenant/controller_extensions_spec.rb +3 -2
  98. data/spec/activerecord-multi-tenant/fast_truncate_spec.rb +8 -6
  99. data/spec/activerecord-multi-tenant/model_extensions_spec.rb +233 -153
  100. data/spec/activerecord-multi-tenant/multi_tenant_spec.rb +15 -13
  101. data/spec/activerecord-multi-tenant/query_rewriter_spec.rb +60 -59
  102. data/spec/activerecord-multi-tenant/record_callback_spec.rb +0 -0
  103. data/spec/activerecord-multi-tenant/record_finding_spec.rb +11 -11
  104. data/spec/activerecord-multi-tenant/record_modifications_spec.rb +4 -4
  105. data/spec/activerecord-multi-tenant/sidekiq_spec.rb +10 -10
  106. data/spec/database.yml +0 -0
  107. data/spec/schema.rb +20 -2
  108. data/spec/spec_helper.rb +46 -17
  109. data/spec/support/format_sql.rb +20 -0
  110. metadata +130 -25
  111. data/.github/workflows/CI.yml +0 -47
  112. data/gemfiles/.bundle/config +0 -2
  113. data/gemfiles/active_record_6.0.gemfile +0 -8
  114. data/gemfiles/active_record_6.1.gemfile +0 -8
  115. data/gemfiles/active_record_7.0.gemfile +0 -8
  116. data/gemfiles/rails_6.0.gemfile +0 -8
  117. data/gemfiles/rails_6.1.gemfile +0 -8
  118. data/gemfiles/rails_7.0.gemfile +0 -8
  119. data/lib/activerecord-multi-tenant/with_lock.rb +0 -15
  120. data/spec/activerecord-multi-tenant/schema_dumper_tester.rb +0 -0
@@ -0,0 +1,126 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.9.34
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" />
16
+
17
+ <script type="text/javascript">
18
+ pathId = "";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+
41
+
42
+ <span class="title">Top Level Namespace</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <div id="content"><h1>Top Level Namespace
63
+
64
+
65
+
66
+ </h1>
67
+ <div class="box_info">
68
+
69
+
70
+
71
+
72
+
73
+
74
+ <dl>
75
+ <dt>Includes:</dt>
76
+ <dd><span class='object_link'><a href="MultiTenantFindBy.html" title="MultiTenantFindBy (module)">MultiTenantFindBy</a></span></dd>
77
+ </dl>
78
+
79
+
80
+
81
+
82
+
83
+
84
+ </div>
85
+
86
+ <h2>Defined Under Namespace</h2>
87
+ <p class="children">
88
+
89
+
90
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="ActiveRecord.html" title="ActiveRecord (module)">ActiveRecord</a></span>, <span class='object_link'><a href="MultiTenant.html" title="MultiTenant (module)">MultiTenant</a></span>, <span class='object_link'><a href="MultiTenantFindBy.html" title="MultiTenantFindBy (module)">MultiTenantFindBy</a></span>, <span class='object_link'><a href="Sidekiq.html" title="Sidekiq (module)">Sidekiq</a></span>
91
+
92
+
93
+
94
+
95
+ </p>
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+ <h2>Method Summary</h2>
111
+
112
+ <h3 class="inherited">Methods included from <span class='object_link'><a href="MultiTenantFindBy.html" title="MultiTenantFindBy (module)">MultiTenantFindBy</a></span></h3>
113
+ <p class="inherited"><span class='object_link'><a href="MultiTenantFindBy.html#cached_find_by_statement-instance_method" title="MultiTenantFindBy#cached_find_by_statement (method)">#cached_find_by_statement</a></span></p>
114
+
115
+
116
+ </div>
117
+
118
+ <div id="footer">
119
+ Generated on Sat May 27 10:16:23 2023 by
120
+ <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
121
+ 0.9.34 (ruby-3.2.2).
122
+ </div>
123
+
124
+ </div>
125
+ </body>
126
+ </html>
@@ -0,0 +1,4 @@
1
+ # Ignore everything in this directory
2
+ *
3
+ # Except this file
4
+ !.gitignore
@@ -0,0 +1,8 @@
1
+ .. _api-reference:
2
+
3
+ API Reference
4
+ =============
5
+
6
+ This section provides a detailed overview of the available classes, modules, and methods in the ``activerecord-multi-tenant`` gem.
7
+
8
+ `Click here to open the HTML file <_static/api-reference/index.html>`_
@@ -0,0 +1,26 @@
1
+ .. _appendix:
2
+
3
+ Appendix
4
+ ========
5
+
6
+ This section provides additional resources and acknowledgments related to ``activerecord-multi-tenant``.
7
+
8
+ Glossary of Terms and Abbreviations
9
+ -----------------------------------
10
+
11
+ - **Multi-tenancy:** A software architecture in which a single instance of software serves multiple tenants.
12
+ - **Tenant:** A group of users who share a common access with specific privileges to the software instance.
13
+ - **Tenant ID:** A unique identifier for a tenant.
14
+
15
+
16
+ References to External Resources
17
+ --------------------------------
18
+
19
+ - `Official Rails Guide <https://guides.rubyonrails.org/>`_: A comprehensive guide to Ruby on Rails.
20
+ - `ActiveRecord Documentation <https://api.rubyonrails.org/classes/ActiveRecord/Base.html>`_: Detailed documentation for ActiveRecord, the database toolkit used by ``activerecord-multi-tenant``.
21
+
22
+ Acknowledgments and Credits
23
+ ---------------------------
24
+
25
+ We would like to thank the Ruby on Rails community for their contributions to open source, which have made projects like ``activerecord-multi-tenant`` possible.
26
+ This gem was initially based on `acts_as_tenant <https://github.com/ErwinM/acts_as_tenant>`, and still shares some code. We thank the authors for their efforts.
@@ -0,0 +1,8 @@
1
+ .. _changelog:
2
+
3
+ Changelog
4
+ =========
5
+
6
+ This section provides a history of changes for each version of ``activerecord-multi-tenant``.
7
+ For a complete history of changes, please refer to the official changelog on `GitHub <https://github.com/citusdata/activerecord-multi-tenant/blob/master/CHANGELOG.md>`_.
8
+
@@ -0,0 +1,26 @@
1
+ .. _community-and-support:
2
+
3
+ Community and Support
4
+ =====================
5
+
6
+ If you need help with ``activerecord-multi-tenant``, there are several resources available to you.
7
+
8
+ GitHub Repository
9
+ -----------------
10
+
11
+ The `activerecord-multi-tenant GitHub repository <https://github.com/citusdata/activerecord-multi-tenant>`_ is the first place to look for code, issues, and pull requests.
12
+
13
+ Issue Tracker
14
+ -------------
15
+
16
+ If you encounter issues with ``activerecord-multi-tenant``, you can report them in the `issue tracker <https://github.com/citusdata/activerecord-multi-tenant/issues>`_. Please provide as much detail as possible so we can address the issue effectively.
17
+
18
+ Discussion Forums
19
+ -----------------
20
+
21
+ For general discussions about ``activerecord-multi-tenant``, you can use the `discussion forums <https://github.com/citusdata/activerecord-multi-tenant/discussions>`_. This is a great place to ask questions, share ideas, and engage with the ``activerecord-multi-tenant`` community.
22
+
23
+ Documentation Feedback
24
+ ----------------------
25
+
26
+ We strive to provide high-quality documentation for ``activerecord-multi-tenant``. If you have feedback or suggestions for improving the documentation, please open an issue in the `issue tracker <https://github.com/citusdata/activerecord-multi-tenant/issues>`_.
@@ -0,0 +1,30 @@
1
+ from datetime import date
2
+
3
+ # Configuration file for the Sphinx documentation builder.
4
+ #
5
+ # For the full list of built-in configuration values, see the documentation:
6
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html
7
+
8
+ # -- Project information -----------------------------------------------------
9
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
10
+
11
+ project = "ActiveRecord Multi-tenant"
12
+ copyright = f"{date.today().year} .Citus Data Licensed under the MIT license, see License for details. "
13
+ author = "Citus Data"
14
+ release = "2.2.0"
15
+
16
+ # -- General configuration ---------------------------------------------------
17
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
18
+
19
+ extensions = ["sphinxnotes.strike"]
20
+
21
+ templates_path = ["_templates"]
22
+ exclude_patterns = []
23
+
24
+ language = "python"
25
+
26
+ # -- Options for HTML output -------------------------------------------------
27
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
28
+
29
+ html_theme = "sphinx_rtd_theme"
30
+ html_static_path = ["_static"]
@@ -0,0 +1,70 @@
1
+ .. _contributing:
2
+
3
+ Contributing
4
+ ============
5
+
6
+ We welcome contributions to ``activerecord-multi-tenant``! This section provides guidelines for contributing to the project.
7
+
8
+ Overview of the Development Process
9
+ -----------------------------------
10
+
11
+ ``activerecord-multi-tenant`` is developed using a standard fork and pull request model. The `activerecord-multi-tenant GitHub repository <https://github.com/citusdata/activerecord-multi-tenant>`_ is the starting point for code contributions.
12
+
13
+ Guidelines for Contributing
14
+ ---------------------------
15
+
16
+ 1. **Fork the Repository:** Start by forking the official ``activerecord-multi-tenant`` repository to your own GitHub account.
17
+
18
+ 2. **Clone the Repository:** Clone the forked repository to your local machine and add the official repository as an upstream remote:
19
+
20
+ .. code-block:: bash
21
+
22
+ $ git clone https://github.com/citusdata/activerecord-multi-tenant.git
23
+ $ cd activerecord-multi-tenant
24
+ $ git remote add upstream https://github.com/your-github-account/activerecord-multi-tenant.git
25
+
26
+ 3. **Create a Feature Branch:** Create a new branch for each feature or bugfix:
27
+
28
+ .. code-block:: bash
29
+
30
+ $ git checkout -b my-feature-branch
31
+
32
+ 4. **Commit Your Changes:** Make your changes and commit them to your feature branch.
33
+
34
+ 5. **Push to GitHub:** Push your changes to your fork on GitHub:
35
+
36
+ .. code-block:: bash
37
+
38
+ $ git push origin my-feature-branch
39
+
40
+ 6. **Submit a Pull Request:** Open a pull request from your feature branch to the master branch of the official ``activerecord-multi-tenant`` repository.
41
+
42
+ Please ensure your code adheres to the existing style conventions of the project. Include tests for any new features or bug fixes, and update the documentation as necessary.
43
+
44
+ Setting Up a Development Environment
45
+ ------------------------------------
46
+
47
+ To set up a development environment for ``activerecord-multi-tenant``, follow these steps:
48
+
49
+ 1. Clone the repository as described in the contributing guidelines.
50
+
51
+ 2. Install the required dependencies:
52
+
53
+ .. code-block:: bash
54
+
55
+ $ bundle install
56
+
57
+ 3. Run the tests to ensure everything is set up correctly:
58
+
59
+ .. code-block:: bash
60
+
61
+ $ bundle exec rake spec
62
+
63
+ 4. Compile documentation for the project:
64
+
65
+ .. code-block:: bash
66
+
67
+ $ cd docs
68
+ $ make pre-build
69
+ $ make html
70
+
@@ -0,0 +1,37 @@
1
+ .. _getting-started:
2
+
3
+ Getting Started
4
+ ===============
5
+
6
+ This section will guide you through the process of installing and setting up ``activerecord-multi-tenant`` in your Rails application.
7
+
8
+ Installation
9
+ ------------
10
+
11
+ To install ``activerecord-multi-tenant``, add the following line to your application's Gemfile:
12
+
13
+ .. code-block:: ruby
14
+
15
+ gem install activerecord-multi-tenant
16
+
17
+ Then execute:
18
+
19
+ .. code-block:: bash
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ .. code-block:: bash
26
+
27
+ $ gem install activerecord-multi-tenant
28
+
29
+ Dependencies
30
+ ------------
31
+
32
+ ``activerecord-multi-tenant`` requires:
33
+
34
+ - Ruby version 3.0.0 or later
35
+ - Rails version 6.0.0 or later
36
+
37
+ Please ensure that your application meets these requirements before installing the gem.
@@ -0,0 +1,129 @@
1
+ .. _guides-and-tutorials:
2
+
3
+ Guides and Tutorials
4
+ ====================
5
+
6
+ This section provides step-by-step guides and tutorials on how to use ``activerecord-multi-tenant`` in various scenarios.
7
+
8
+ Setting Up Multi tenancy in a New Project
9
+ ------------------------------------------
10
+
11
+ To set up multi-tenancy in a new Rails project, follow these steps:
12
+
13
+ 1. Install the ``activerecord-multi-tenant`` gem as described in the :ref:`getting-started` section.
14
+
15
+ 2. Declare your tenant model in your ActiveRecord models:
16
+
17
+ .. code-block:: ruby
18
+
19
+ class User < ActiveRecord::Base
20
+ multi_tenant :company
21
+ end
22
+
23
+ 3. Set the current tenant before executing queries:
24
+
25
+ .. code-block:: ruby
26
+
27
+ ActiveRecord::MultiTenant.current_tenant = Company.first
28
+ @users = User.all
29
+
30
+ Migrating an Existing Project to Use ``activerecord-multi-tenant``
31
+ ------------------------------------------------------------------
32
+
33
+ If you have an existing Rails project and you want to add multi-tenancy support, follow these steps:
34
+
35
+ 1. Install the ``activerecord-multi-tenant`` gem as described in the :ref:`getting-started` section.
36
+
37
+ 2. Update your ActiveRecord models to declare the tenant model:
38
+
39
+ .. code-block:: ruby
40
+
41
+ class User < ActiveRecord::Base
42
+ multi_tenant :company
43
+ end
44
+
45
+ 3. Update your application logic to set the current tenant before executing queries.
46
+
47
+
48
+ Using ``has_many`` , ``has_one`` , and ``belongs_to`` Associations
49
+ ------------------------------------------------------------------
50
+
51
+ When using ``has_many``, ``has_one``, and ``belongs_to`` associations,
52
+ there is nothing special you need to do to make them work with ``activerecord-multi-tenant``.
53
+ The gem will automatically scope the associations to the current tenant.:
54
+
55
+ .. code-block:: ruby
56
+
57
+ class User < ActiveRecord::Base
58
+ multi_tenant :company
59
+ has_many :posts
60
+ end
61
+
62
+ class Post < ActiveRecord::Base
63
+ belongs_to :user
64
+ end
65
+
66
+ ActiveRecord::MultiTenant.with(Company.first) do
67
+ @user = User.first
68
+ @user.posts # => Returns posts belonging to Company.first
69
+ end
70
+
71
+ Using ``has_and_belongs_to_many`` Associations
72
+ -----------------------------------------------
73
+
74
+ When using ``has_and_belongs_to_many`` associations, you need to specify the tenant column and tenant class name to
75
+ scope the association to the current tenant. If you set the ``tenant_enabled`` option to ``false``, the gem will
76
+ not scope the association to the current tenant.
77
+
78
+ .. code-block:: ruby
79
+
80
+ class Account < ActiveRecord::Base
81
+ multi_tenant :account
82
+ has_many :projects
83
+ has_one :manager, inverse_of: :account
84
+ has_many :optional_sub_tasks
85
+ end
86
+
87
+ class Manager < ActiveRecord::Base
88
+ multi_tenant :account
89
+ belongs_to :project
90
+ has_and_belongs_to_many :tasks, { tenant_column: :account_id, tenant_enabled: true,
91
+ tenant_class_name: 'Account' }
92
+ end
93
+
94
+ # Tests to check if the tenant column is set correctly
95
+ let(:task1) { Task.create! name: 'task1', project: project1, account: account1 }
96
+ let(:manager1) { Manager.create! name: 'manager1', account: account1, tasks: [task1] }
97
+
98
+ MultiTenant.with(account1) do
99
+ expect(manager1.tasks.first.account_id).to eq(task1.account_id) # true
100
+ end
101
+
102
+ Using ``activerecord-multi-tenant`` with Controllers
103
+ -----------------------------------------------------
104
+
105
+ When using ``activerecord-multi-tenant`` with controllers, you need to set the current tenant in the controller
106
+ before executing queries. You can do this by overriding the ``set_current_tenant`` method in your controller:
107
+
108
+ .. code-block:: ruby
109
+
110
+ class ApplicationController < ActionController::Base
111
+ set_current_tenant_through_filter # Required to opt into this behavior
112
+ before_action :set_customer_as_tenant
113
+
114
+ def set_customer_as_tenant
115
+ customer = Customer.find(session[:current_customer_id])
116
+ set_current_tenant(customer)
117
+ end
118
+ end
119
+
120
+ Best Practices and Recommendations
121
+ -----------------------------------
122
+
123
+ When using ``activerecord-multi-tenant``, keep the following best practices in mind:
124
+
125
+ - Always set the current tenant before executing queries in a multitenant context.
126
+ - Be mindful of the tenant scope when writing complex queries or joins.
127
+ - If you prefer not to set a tenant for the global context, but need to specify one for certain sections of code,
128
+ you can utilize the `MultiTenant.with(tenant)` function. This will assign the `tenant` value
129
+ to the specific code block where it's used.
@@ -0,0 +1,54 @@
1
+ .. Django Multi-tenant documentation master file, created by
2
+ sphinx-quickstart on Mon Feb 13 13:32:28 2023.
3
+ You can adapt this file completely to your liking, but it should at least
4
+ contain the root `toctree` directive.
5
+
6
+ Welcome to ActiveRecord Multi-tenant's documentation!
7
+ ======================================================
8
+
9
+ |Build Status| |Coverage Status| |RubyGems Version| |Gem Downloads| |Latest Documentation Status|
10
+
11
+ .. |Build Status| image:: https://github.com/citusdata/activerecord-multi-tenant/actions/workflows/active-record-multi-tenant-tests.yml/badge.svg
12
+ :target: https://github.com/citusdata/activerecord-multi-tenant/actions/workflows/active-record-multi-tenant-tests.yml
13
+ :alt: Build Status
14
+
15
+ .. |Coverage Status| image:: https://codecov.io/gh/citusdata/activerecord-multi-tenant/branch/master/graph/badge.svg?token=rw0TsEk4Ld
16
+ :target: https://codecov.io/gh/citusdata/activerecord-multi-tenant
17
+ :alt: Coverage Status
18
+
19
+ .. |RubyGems Version| image:: https://img.shields.io/gem/v/activerecord-multi-tenant.svg
20
+ :target: https://rubygems.org/gems/activerecord-multi-tenant
21
+
22
+ .. |Gem Downloads| image:: https://img.shields.io/gem/dt/activerecord-multi-tenant.svg
23
+ :target: https://rubygems.org/gems/activerecord-multi-tenant
24
+ :alt: Gem Downloads
25
+
26
+ .. |Latest Documentation Status| image:: https://readthedocs.org/projects/django-multitenant/badge/?version=latest
27
+ :target: https://activerecord-multi-tenant.readthedocs.io/en/latest/?badge=latest
28
+ :alt: Documentation Status
29
+
30
+
31
+ activerecord-multi-tenant Documentation
32
+ ========================================
33
+
34
+ Welcome to the official documentation for ``activerecord-multi-tenant``, a powerful and flexible gem for adding multi-tenancy support to your Rails applications using ActiveRecord.
35
+
36
+ .. toctree::
37
+ :maxdepth: 2
38
+ :caption: Table of Contents
39
+
40
+ introduction
41
+ getting-started
42
+ usage-guide
43
+ guides-and-tutorials
44
+ api-reference
45
+ troubleshooting
46
+ contributing
47
+ changelog
48
+ community-and-support
49
+ appendix
50
+ license
51
+
52
+ This documentation provides a comprehensive guide to using ``activerecord-multi-tenant``, including installation and configuration instructions, usage examples, API reference, troubleshooting tips, and more.
53
+
54
+ Whether you're new to multi-tenancy or an experienced developer looking to integrate ``activerecord-multi-tenant`` into your existing Rails application, this documentation aims to provide the information you need.
@@ -0,0 +1,33 @@
1
+ .. _introduction:
2
+
3
+ Introduction
4
+ ============
5
+
6
+ Welcome to the official documentation for ``activerecord-multi-tenant``, a powerful and flexible gem for adding multi-tenancy support to your Rails applications using ActiveRecord.
7
+
8
+ Overview
9
+ --------
10
+
11
+ ``activerecord-multi-tenant`` is designed to help developers manage and interact with data in multi-tenant environments. It provides a simple and intuitive API to scope your ActiveRecord models to specific tenants, ensuring data isolation and security while maintaining the simplicity and elegance that Rails developers love.
12
+
13
+ Purpose
14
+ -------
15
+
16
+ The purpose of this documentation is to provide a comprehensive guide to using ``activerecord-multi-tenant``. Whether you're new to multi-tenancy or an experienced developer looking to integrate ``activerecord-multi-tenant`` into your existing Rails application, this documentation aims to provide the information you need.
17
+
18
+ Benefits of Using ``activerecord-multi-tenant``
19
+ ------------------------------------------------
20
+
21
+ With ``activerecord-multi-tenant``, you can:
22
+
23
+ - Easily scope your ActiveRecord models to specific tenants
24
+ - Maintain data isolation and security in multi-tenant environments
25
+ - Seamlessly integrate with your existing Rails applications
26
+ - Benefit from the simplicity and elegance of the Rails framework
27
+
28
+ Target Audience
29
+ ---------------
30
+
31
+ This documentation is intended for developers who are familiar with Ruby on Rails and ActiveRecord. Knowledge of multi-tenancy concepts is beneficial but not required, as we will cover these topics in the following sections.
32
+
33
+ We hope you find this documentation helpful as you explore the capabilities of ``activerecord-multi-tenant``. Let's get started!
@@ -0,0 +1,22 @@
1
+ .. _license:
2
+
3
+ License
4
+ ===============================================
5
+ Copyright (c) 2023 , Citus Data Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
8
+ this software and associated documentation files (the "Software"), to deal in
9
+ the Software without restriction, including without limitation the rights to use,
10
+ copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
11
+ Software, and to permit persons to whom the Software is furnished to do so,
12
+ subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ .. _troubleshooting:
2
+
3
+ Troubleshooting
4
+ ===============
5
+
6
+ This section provides solutions to common issues you might encounter when using ``activerecord-multi-tenant``.
7
+
8
+ Common Issues and Their Solutions
9
+ ---------------------------------
10
+
11
+ **Issue:** Tenant scope is not applied to queries.
12
+
13
+ **Solution:** Make sure you've set the current tenant before executing queries. Use the MultiTenant.with method to set the current tenant. For example:
14
+
15
+ .. code-block:: ruby
16
+
17
+ MultiTenant.with(customer) do
18
+ site = Site.find(params[:site_id])
19
+ site.update! last_accessed_at: Time.now
20
+ site.page_views.count
21
+ end
22
+
23
+
24
+
25
+ FAQs and Known Limitations
26
+ --------------------------
27
+
28
+ **Q: Can I use multiple tenant models in the same application?**
29
+
30
+ **A:** Yes, you can declare different tenant models in different ActiveRecord models. However, you can only set one current tenant at a time.
31
+
32
+ **Q: Does ``activerecord-multi-tenant`` support Rails version 5.X?**
33
+
34
+ **A:** ``activerecord-multi-tenant`` supports Rails 6.0.0 and later. For older versions of Rails, please use the appropriate version of the gem.
35
+
36
+
37
+
38
+ Reporting Bugs and Requesting Features
39
+ --------------------------------------
40
+
41
+ If you encounter a bug or have a feature request, please open an issue on the `activerecord-multi-tenant GitHub repository <https://github.com/citusdata/activerecord-multi-tenant/issues>`_. Please provide as much detail as possible so we can address the issue effectively.