diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 7b9c91c..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,62 +0,0 @@ -version: 2.1 - -orbs: - browser-tools: circleci/browser-tools@1.1 - solidusio_extensions: solidusio/extensions@volatile - -jobs: - run-specs-with-sqlite: - executor: - name: solidusio_extensions/sqlite - ruby_version: '3.1' - steps: - - browser-tools/install-chrome - - solidusio_extensions/run-tests - run-specs-with-postgres: - executor: - name: solidusio_extensions/postgres - ruby_version: '3.1' - steps: - - browser-tools/install-chrome - - solidusio_extensions/run-tests - run-specs-with-mysql: - executor: - name: solidusio_extensions/mysql - ruby_version: '3.1' - steps: - - browser-tools/install-chrome - - solidusio_extensions/run-tests - lint-code: - executor: - name: solidusio_extensions/sqlite-memory - ruby_version: '3.1' - steps: - - solidusio_extensions/lint-code - -workflows: - "Run specs on supported Solidus versions": - jobs: - - run-specs-with-sqlite: - context: acts_as_tenant - - run-specs-with-postgres: - context: acts_as_tenant - - run-specs-with-mysql: - context: acts_as_tenant - - lint-code: - context: acts_as_tenant - - "Weekly run specs against main": - triggers: - - schedule: - cron: "0 0 * * 4" # every Thursday - filters: - branches: - only: - - main - jobs: - - run-specs-with-sqlite: - context: acts_as_tenant - - run-specs-with-postgres: - context: acts_as_tenant - - run-specs-with-mysql: - context: acts_as_tenant diff --git a/.gem_release.yml b/.gem_release.yml index 72c6090..773e6ec 100644 --- a/.gem_release.yml +++ b/.gem_release.yml @@ -1,5 +1,5 @@ bump: recurse: false - file: 'lib/solidus_act_as_tenant/version.rb' - message: Bump SolidusActAsTenant to %{version} + file: 'lib/solidus_acts_as_tenant/version.rb' + message: Bump SolidusActsAsTenant to %{version} tag: true diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..dc2a0cf --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,25 @@ +name: Lint + +on: [pull_request] + +concurrency: + group: lint-${{ github.ref_name }} + cancel-in-progress: ${{ github.ref_name != 'main' }} + +permissions: + contents: read + +jobs: + ruby: + name: Check Ruby + runs-on: ubuntu-24.04 + steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: Install Ruby and gems + uses: ruby/setup-ruby@v1 + with: + ruby-version: "3.2" + bundler-cache: true + - name: Lint Ruby files + run: bundle exec rubocop -ESP diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..4579edb --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,71 @@ +name: Test + +on: + push: + branches: + - main + pull_request: + schedule: + - cron: "0 0 * * 4" # every Thursday + +concurrency: + group: test-${{ github.ref_name }} + cancel-in-progress: ${{ github.ref_name != 'main' }} + +permissions: + contents: read + +jobs: + rspec: + name: Solidus ${{ matrix.solidus-branch }}, Rails ${{ matrix.rails-version }} and Ruby ${{ matrix.ruby-version }} on ${{ matrix.database }} + runs-on: ubuntu-24.04 + strategy: + fail-fast: true + matrix: + rails-version: + - "7.0" + - "7.1" + - "7.2" + ruby-version: + - "3.1" + - "3.4" + solidus-branch: + - "v4.1" + - "v4.2" + - "v4.3" + - "v4.4" + - "v4.5" + database: + - "postgresql" + - "mysql" + - "sqlite" + exclude: + - rails-version: "7.2" + solidus-branch: "v4.3" + - rails-version: "7.2" + solidus-branch: "v4.2" + - rails-version: "7.2" + solidus-branch: "v4.1" + - rails-version: "7.1" + solidus-branch: "v4.2" + - rails-version: "7.1" + solidus-branch: "v4.1" + - ruby-version: "3.4" + rails-version: "7.0" + env: + CODECOV_COVERAGE_PATH: ./coverage/coverage.xml + steps: + - uses: actions/checkout@v4 + - name: Run extension tests + uses: solidusio/test-solidus-extension@main + with: + database: ${{ matrix.database }} + rails-version: ${{ matrix.rails-version }} + ruby-version: ${{ matrix.ruby-version }} + solidus-branch: ${{ matrix.solidus-branch }} + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v5 + continue-on-error: true + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ${{ env.CODECOV_COVERAGE_PATH }} diff --git a/.rubocop.yml b/.rubocop.yml index b075a8f..c13ba75 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,24 @@ require: - solidus_dev_support/rubocop +FactoryBot/CreateList: + Enabled: false + +FactoryBot/AssociationStyle: + Enabled: false + +FactoryBot/FactoryNameStyle: + Enabled: false + +FactoryBot/ConsistentParenthesesStyle: + Enabled: false + +Capybara/RSpec/PredicateMatcher: + Enabled: false + +Gemspec/RequiredRubyVersion: + Enabled: false + AllCops: NewCops: disable + TargetRubyVersion: 3.2 # Matching the .github/workflow/lint.yml ruby version diff --git a/CHANGELOG.md b/CHANGELOG.md index de29776..9a3534b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,3 @@ # Changelog -See https://github.com/solidusio-contrib/solidus_act_as_tenant/releases or OLD_CHANGELOG.md for older versions. +See https://github.com/nebulab/solidus_acts_as_tenant/releases or OLD_CHANGELOG.md for older versions. diff --git a/Gemfile b/Gemfile index 1c6e5fe..c6ece8c 100644 --- a/Gemfile +++ b/Gemfile @@ -15,13 +15,8 @@ else gem 'solidus_frontend', github: 'solidusio/solidus', branch: branch end -# Needed to help Bundler figure out how to resolve dependencies, -# otherwise it takes forever to resolve them. -# See https://github.com/bundler/bundler/issues/6677 -gem 'rails', '>0.a' - -# Provides basic authentication functionality for testing parts of your engine -gem 'solidus_auth_devise' +rails_version = ENV.fetch('RAILS_VERSION', '7.0') +gem 'rails', "~> #{rails_version}" case ENV.fetch('DB', nil) when 'mysql' @@ -29,7 +24,16 @@ when 'mysql' when 'postgresql' gem 'pg' else - gem 'sqlite3', '~> 1.4' + gem 'sqlite3', rails_version < '7.2' ? '~> 1.4' : '~> 2.0' +end + +if rails_version == '7.0' + gem 'concurrent-ruby', '< 1.3.5' +end + +if RUBY_VERSION >= '3.4' + # Solidus Promotions uses CSV but does not have it as dependency yet. + gem 'csv' end # While we still support Ruby < 3 we need to workaround a limitation in diff --git a/README.md b/README.md index 2077415..3ef87de 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ -# Solidus Act As Tenant +# Solidus Acts As Tenant -[![CircleCI](https://dl.circleci.com/status-badge/img/gh/nebulab/solidus_acts_as_tenant/tree/Add-acts_as_tenant-to-Solidus.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/nebulab/solidus_acts_as_tenant/tree/Add-acts_as_tenant-to-Solidus) -[![codecov](https://codecov.io/gh/solidusio-contrib/solidus_act_as_tenant/branch/main/graph/badge.svg)](https://codecov.io/gh/solidusio-contrib/solidus_act_as_tenant) +[![codecov](https://codecov.io/gh/nebulab/solidus_acts_as_tenant/branch/main/graph/badge.svg)](https://codecov.io/gh/nebulab/solidus_acts_as_tenant) This extension adds multi-tenant support to solidus using the row-level tenancy [acts_as_tenant](https://github.com/ErwinM/acts_as_tenant/commits/master/) gem. @@ -10,16 +9,16 @@ It adds tenant scoping to a configurabale set of models and adds a console utili ## Installation -Add solidus_act_as_tenant to your Gemfile: +Add solidus_acts_as_tenant to your Gemfile: ```ruby -gem 'solidus_act_as_tenant' +gem 'solidus_acts_as_tenant' ``` Bundle your dependencies and run the installation generator: ```shell -bin/rails generate solidus_act_as_tenant:install +bin/rails generate solidus_acts_as_tenant:install ``` ## Usage @@ -38,7 +37,7 @@ To use it, you can add it to your `~/.irbrc` file or `~/.pryrc` file: ```ruby if defined?(Rails) - TS = SolidusActAsTenant::Utils::TenantSelector.new + TS = SolidusActsAsTenant::Utils::TenantSelector.new IRB.conf[:IRB_RC] = proc do # * TS.ask => anytime in console, to switch tenant from a list @@ -72,7 +71,7 @@ When testing your application's integration with this extension you may use its You can load Solidus core factories along with this extension's factories using this statement: ```ruby -SolidusDevSupport::TestingSupport::Factories.load_for(SolidusActAsTenant::Engine) +SolidusDevSupport::TestingSupport::Factories.load_for(SolidusActsAsTenant::Engine) ``` ### Running the sandbox diff --git a/app/decorators/solidus_acts_as_tenant/store_credit_decorator.rb b/app/decorators/solidus_acts_as_tenant/store_credit_decorator.rb new file mode 100644 index 0000000..0b35941 --- /dev/null +++ b/app/decorators/solidus_acts_as_tenant/store_credit_decorator.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module SolidusActsAsTenant::StoreCreditDecorator # rubocop:disable Style/ClassAndModuleChildren + # Fixes no method error related to validations added through acts_as_tenant + def payment_method_id + return if payment_method.nil? + + super + end + + ::Spree::StoreCredit.prepend(self) +end diff --git a/app/decorators/solidus_acts_as_tenant/variant_decorator.rb b/app/decorators/solidus_acts_as_tenant/variant_decorator.rb new file mode 100644 index 0000000..47f2984 --- /dev/null +++ b/app/decorators/solidus_acts_as_tenant/variant_decorator.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module SolidusActsAsTenant::VariantDecorator # rubocop:disable Style/ClassAndModuleChildren + # Fixes errors related to validations added through acts_as_tenant + + def product_shipping_category_id + return if product.nil? + + super + end + + def product_shipping_category + return if product.nil? + + super + end + + def product_tax_category_id + return if product.nil? + + super + end + + def product_tax_category + return if product.nil? + + super + end + + ::Spree::Variant.prepend(self) +end diff --git a/app/decorators/spree/store_credit_decorator.rb b/app/decorators/spree/store_credit_decorator.rb deleted file mode 100644 index f954f6d..0000000 --- a/app/decorators/spree/store_credit_decorator.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -module SolidusActAsTenant - module Spree - module StoreCreditDecorator - # Fixes no method error related to validations added through acts_as_tenant - def payment_method_id - super - rescue NoMethodError - nil - end - - ::Spree::StoreCredit.prepend(self) - end - end -end diff --git a/app/decorators/spree/variant_decorator.rb b/app/decorators/spree/variant_decorator.rb deleted file mode 100644 index dd54b9a..0000000 --- a/app/decorators/spree/variant_decorator.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -module SolidusActAsTenant - module Spree - module VariantDecorator - # Fixes no method error related to validations added through acts_as_tenant - def shipping_category_id - super - rescue NoMethodError - product && product_shipping_category_id - end - - def shipping_category - super - rescue NoMethodError - product && product_shipping_category - end - - ::Spree::Variant.prepend(self) - end - end -end diff --git a/app/models/spree/tenant.rb b/app/models/spree/tenant.rb index 111805d..5e60819 100644 --- a/app/models/spree/tenant.rb +++ b/app/models/spree/tenant.rb @@ -2,7 +2,7 @@ module Spree class Tenant < Spree::Base - if SolidusActAsTenant.config.class_name == 'Spree::Tenant' + if SolidusActsAsTenant.config.class_name == 'Spree::Tenant' validates :name, presence: true, uniqueness: true validates :subdomain, uniqueness: true validates :domain, uniqueness: true diff --git a/bin/console b/bin/console index 1c6f28d..e26c347 100755 --- a/bin/console +++ b/bin/console @@ -3,7 +3,7 @@ # frozen_string_literal: true require "bundler/setup" -require "solidus_act_as_tenant" +require "solidus_acts_as_tenant" # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. diff --git a/bin/rails-engine b/bin/rails-engine index 7341e21..9106228 100755 --- a/bin/rails-engine +++ b/bin/rails-engine @@ -3,7 +3,7 @@ # installed from the root of your application. ENGINE_ROOT = File.expand_path('..', __dir__) -ENGINE_PATH = File.expand_path('../lib/solidus_act_as_tenant/engine', __dir__) +ENGINE_PATH = File.expand_path('../lib/solidus_acts_as_tenant/engine', __dir__) # Set up gems listed in the Gemfile. ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) diff --git a/bin/sandbox b/bin/sandbox index 069c47f..88ed092 100755 --- a/bin/sandbox +++ b/bin/sandbox @@ -17,7 +17,7 @@ then fi echo "~~> Using branch $SOLIDUS_BRANCH of solidus" -extension_name="solidus_act_as_tenant" +extension_name="solidus_acts_as_tenant" # Stay away from the bundler env of the containing extension. function unbundled { @@ -30,7 +30,8 @@ echo "~~~> Removing the old sandbox" rm -rf ./sandbox echo "~~~> Creating a pristine Rails app" -rails new sandbox \ +rails_version=`bundle exec ruby -e'require "rails"; puts Rails.version'` +rails _${rails_version}_ new sandbox \ --database="${DB:-sqlite3}" \ --skip-git \ --skip-keeps \ diff --git a/db/migrate/20241204043609_add_tenant_id_to_solidus_tables.rb b/db/migrate/20241204043609_add_tenant_id_to_solidus_tables.rb index 15102f3..9748fee 100644 --- a/db/migrate/20241204043609_add_tenant_id_to_solidus_tables.rb +++ b/db/migrate/20241204043609_add_tenant_id_to_solidus_tables.rb @@ -1,5 +1,5 @@ class AddTenantIdToSolidusTables < ActiveRecord::Migration[7.0] - # If using a different tenant model, you will need to change the ::SolidusActAsTenant.config.tenant_column_name + # If using a different tenant model, you will need to change the ::SolidusActsAsTenant.config.tenant_column_name # that is added to the tables. # These are tables that don't need a tenancy but may be desired: @@ -89,15 +89,15 @@ class AddTenantIdToSolidusTables < ActiveRecord::Migration[7.0] def up TABLES.each do |table| - add_column table, ::SolidusActAsTenant.config.tenant_column_name, :bigint - add_index table, ::SolidusActAsTenant.config.tenant_column_name + add_column table, ::SolidusActsAsTenant.config.tenant_column_name, :bigint + add_index table, ::SolidusActsAsTenant.config.tenant_column_name end end def down TABLES.each do |table| remove_index table - remove_column table, ::SolidusActAsTenant.config.tenant_column_name + remove_column table, ::SolidusActsAsTenant.config.tenant_column_name end end end diff --git a/db/migrate/20241204055845_update_spree_unique_indexes_with_tenant_scope.rb b/db/migrate/20241204055845_update_spree_unique_indexes_with_tenant_scope.rb index 2497157..ed4b08f 100644 --- a/db/migrate/20241204055845_update_spree_unique_indexes_with_tenant_scope.rb +++ b/db/migrate/20241204055845_update_spree_unique_indexes_with_tenant_scope.rb @@ -1,13 +1,13 @@ class UpdateSpreeUniqueIndexesWithTenantScope < ActiveRecord::Migration[7.0] - # If using a different tenant model, you will need to change the :::SolidusActAsTenant.config.tenant_column_name, + # If using a different tenant model, you will need to change the :::SolidusActsAsTenant.config.tenant_column_name, # in the following indexes. def up remove_index :spree_preferences, name: :index_spree_preferences_on_key remove_index :spree_products, name: :index_spree_products_on_slug - add_index :spree_preferences, [::SolidusActAsTenant.config.tenant_column_name, :key], unique: true, + add_index :spree_preferences, [::SolidusActsAsTenant.config.tenant_column_name, :key], unique: true, name: :index_spree_preferences_on_tenant_and_key - add_index :spree_products, [::SolidusActAsTenant.config.tenant_column_name, :slug], unique: true, + add_index :spree_products, [::SolidusActsAsTenant.config.tenant_column_name, :slug], unique: true, name: :index_spree_products_on_tenant_and_slug end diff --git a/lib/generators/solidus_act_as_tenant/install/install_generator.rb b/lib/generators/solidus_acts_as_tenant/install/install_generator.rb similarity index 85% rename from lib/generators/solidus_act_as_tenant/install/install_generator.rb rename to lib/generators/solidus_acts_as_tenant/install/install_generator.rb index 5e8d28b..9c81715 100644 --- a/lib/generators/solidus_act_as_tenant/install/install_generator.rb +++ b/lib/generators/solidus_acts_as_tenant/install/install_generator.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module SolidusActAsTenant +module SolidusActsAsTenant module Generators class InstallGenerator < Rails::Generators::Base class_option :auto_run_migrations, type: :boolean, default: false @@ -11,7 +11,7 @@ def self.exit_on_failure? end def copy_initializer - template 'initializer.rb', 'config/initializers/solidus_act_as_tenant.rb' + template 'initializer.rb', 'config/initializers/solidus_acts_as_tenant.rb' end def copy_config @@ -19,14 +19,12 @@ def copy_config end def add_migrations - run 'bin/rails railties:install:migrations FROM=solidus_act_as_tenant' + run 'bin/rails railties:install:migrations FROM=solidus_acts_as_tenant' end def run_migrations puts 'Remember to inspect and adapt the migration files before migrating!' # rubocop:disable Rails/Output - run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?( - ask('Would you like to run the migrations now? [Y/n]') - ) + run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]')) # rubocop:disable Layout/LineLength if run_migrations run 'bin/rails db:migrate' else diff --git a/lib/generators/solidus_act_as_tenant/install/templates/initializer.rb b/lib/generators/solidus_acts_as_tenant/install/templates/initializer.rb similarity index 93% rename from lib/generators/solidus_act_as_tenant/install/templates/initializer.rb rename to lib/generators/solidus_acts_as_tenant/install/templates/initializer.rb index b81e00e..1cdd01c 100644 --- a/lib/generators/solidus_act_as_tenant/install/templates/initializer.rb +++ b/lib/generators/solidus_acts_as_tenant/install/templates/initializer.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -SolidusActAsTenant.configure do |config| +SolidusActsAsTenant.configure do |config| # This configuration is used to dynamically prepend existing models with the acts_as_tenant concern. # See the tenant_aware_classes.yml file for inormation on how to configure this file. config.tenant_aware_models = diff --git a/lib/generators/solidus_act_as_tenant/install/templates/tenant_aware_models.yml b/lib/generators/solidus_acts_as_tenant/install/templates/tenant_aware_models.yml similarity index 90% rename from lib/generators/solidus_act_as_tenant/install/templates/tenant_aware_models.yml rename to lib/generators/solidus_acts_as_tenant/install/templates/tenant_aware_models.yml index c356b9d..5a76781 100644 --- a/lib/generators/solidus_act_as_tenant/install/templates/tenant_aware_models.yml +++ b/lib/generators/solidus_acts_as_tenant/install/templates/tenant_aware_models.yml @@ -22,9 +22,12 @@ # These are not needed at all as they are internal to Solidus (and may cause issues): # [Spree::OrderMutex, []], + # The user model conflicts with devise, so it is not included here. + # But it may be included if the TenantNotSet error is bypassed using the config added on this fork's branch: + # https://github.com/nebulab/acts_as_tenant/tree/Pass-the-ar-relation-for-context-to-should_require_tenant + # [<%= ::Spree.user_class.to_s %>, []] --- -- [<%= ::Spree.user_class.to_s %>, [email]] - [Spree::Address, []] - [Spree::AdjustmentReason, [name, code]] - [Spree::Adjustment, []] diff --git a/lib/solidus_act_as_tenant.rb b/lib/solidus_act_as_tenant.rb deleted file mode 100644 index 35ad25c..0000000 --- a/lib/solidus_act_as_tenant.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -require 'acts_as_tenant' -require 'solidus_act_as_tenant/configuration' -require 'solidus_act_as_tenant/tenant_aware' -require 'solidus_act_as_tenant/utils/tenant_selector' -require 'solidus_act_as_tenant/version' -require 'solidus_act_as_tenant/engine' diff --git a/lib/solidus_act_as_tenant/version.rb b/lib/solidus_act_as_tenant/version.rb deleted file mode 100644 index 52ae551..0000000 --- a/lib/solidus_act_as_tenant/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -module SolidusActAsTenant - VERSION = '0.0.4' -end diff --git a/lib/solidus_acts_as_tenant.rb b/lib/solidus_acts_as_tenant.rb new file mode 100644 index 0000000..f79cb60 --- /dev/null +++ b/lib/solidus_acts_as_tenant.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require 'acts_as_tenant' +require 'solidus_acts_as_tenant/configuration' +require 'solidus_acts_as_tenant/tenant_aware' +require 'solidus_acts_as_tenant/utils/tenant_selector' +require 'solidus_acts_as_tenant/version' +require 'solidus_acts_as_tenant/engine' diff --git a/lib/solidus_act_as_tenant/configuration.rb b/lib/solidus_acts_as_tenant/configuration.rb similarity index 95% rename from lib/solidus_act_as_tenant/configuration.rb rename to lib/solidus_acts_as_tenant/configuration.rb index 87278c4..44a72f5 100644 --- a/lib/solidus_act_as_tenant/configuration.rb +++ b/lib/solidus_acts_as_tenant/configuration.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module SolidusActAsTenant +module SolidusActsAsTenant class Configuration attr_accessor :tenant_aware_models attr_writer :tenant_column_name, :acts_as_tenant_args, :class_name diff --git a/lib/solidus_act_as_tenant/engine.rb b/lib/solidus_acts_as_tenant/engine.rb similarity index 65% rename from lib/solidus_act_as_tenant/engine.rb rename to lib/solidus_acts_as_tenant/engine.rb index aa0a154..0026365 100644 --- a/lib/solidus_act_as_tenant/engine.rb +++ b/lib/solidus_acts_as_tenant/engine.rb @@ -3,21 +3,21 @@ require 'solidus_core' require 'solidus_support' -module SolidusActAsTenant +module SolidusActsAsTenant class Engine < Rails::Engine include SolidusSupport::EngineExtensions isolate_namespace ::Spree - engine_name 'solidus_act_as_tenant' + engine_name 'solidus_acts_as_tenant' # use rspec for tests config.generators do |g| g.test_framework :rspec end - config.after_initialize do - SolidusActAsTenant::TenantAware.setup_tenant_aware_models + config.to_prepare do + SolidusActsAsTenant::TenantAware.setup_tenant_aware_models end end end diff --git a/lib/solidus_act_as_tenant/factories/preference_factory.rb b/lib/solidus_acts_as_tenant/factories/preference_factory.rb similarity index 65% rename from lib/solidus_act_as_tenant/factories/preference_factory.rb rename to lib/solidus_acts_as_tenant/factories/preference_factory.rb index 95e8d3f..52927cb 100644 --- a/lib/solidus_act_as_tenant/factories/preference_factory.rb +++ b/lib/solidus_acts_as_tenant/factories/preference_factory.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true FactoryBot.define do - factory :preference, class: Spree::Preference do + factory :preference, class: 'Spree::Preference' do sequence(:key) { |i| "Key#{i}" } end end diff --git a/lib/solidus_act_as_tenant/factories/tenant_factory.rb b/lib/solidus_acts_as_tenant/factories/tenant_factory.rb similarity index 73% rename from lib/solidus_act_as_tenant/factories/tenant_factory.rb rename to lib/solidus_acts_as_tenant/factories/tenant_factory.rb index 867fe7e..4dd7397 100644 --- a/lib/solidus_act_as_tenant/factories/tenant_factory.rb +++ b/lib/solidus_acts_as_tenant/factories/tenant_factory.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true FactoryBot.define do - factory :tenant, class: ::SolidusActAsTenant.config.class_name do + factory :tenant, class: ::SolidusActsAsTenant.config.class_name do sequence(:name) { |i| "Demo #{i}" } sequence(:subdomain) { |i| "demo-#{i}" } sequence(:domain) { |i| "demo-#{i}.com" } diff --git a/lib/solidus_act_as_tenant/tenant_aware.rb b/lib/solidus_acts_as_tenant/tenant_aware.rb similarity index 88% rename from lib/solidus_act_as_tenant/tenant_aware.rb rename to lib/solidus_acts_as_tenant/tenant_aware.rb index 4fa92d3..12682e4 100644 --- a/lib/solidus_act_as_tenant/tenant_aware.rb +++ b/lib/solidus_acts_as_tenant/tenant_aware.rb @@ -1,18 +1,18 @@ # frozen_string_literal: true -module SolidusActAsTenant +module SolidusActsAsTenant # TenantAware automatically adds multi-tenant support to models # by adding tenant scoping and updating uniqueness validations to be tenant-aware. module TenantAware class << self def setup_tenant_aware_models - tenant_aware_models = SolidusActAsTenant.config.tenant_aware_models || [] + tenant_aware_models = SolidusActsAsTenant.config.tenant_aware_models || [] tenant_aware_models.each do |klass, validator_attributes| Module.new do @validator_attributes = validator_attributes define_singleton_method(:prepended) do |base| - options = ::SolidusActAsTenant.config.acts_as_tenant_args + options = ::SolidusActsAsTenant.config.acts_as_tenant_args base.acts_as_tenant(*options[0..-2], **options.last) @validator_attributes&.each do |attribute| @@ -31,7 +31,7 @@ def update_uniqueness_validation(base, attribute) raise "No uniqueness validator found for #{attribute} on #{base}" unless validator new_options = validator.options.dup - new_options[:scope] = Array(new_options[:scope]).push(::SolidusActAsTenant.config.tenant_column_name) + new_options[:scope] = Array(new_options[:scope]).push(::SolidusActsAsTenant.config.tenant_column_name) remove_existing_validation(base, attribute) base.validates_uniqueness_of attribute, **new_options diff --git a/lib/solidus_act_as_tenant/testing_support/factories.rb b/lib/solidus_acts_as_tenant/testing_support/factories.rb similarity index 100% rename from lib/solidus_act_as_tenant/testing_support/factories.rb rename to lib/solidus_acts_as_tenant/testing_support/factories.rb diff --git a/lib/solidus_act_as_tenant/utils/tenant_selector.rb b/lib/solidus_acts_as_tenant/utils/tenant_selector.rb similarity index 89% rename from lib/solidus_act_as_tenant/utils/tenant_selector.rb rename to lib/solidus_acts_as_tenant/utils/tenant_selector.rb index 324edfe..afb75e9 100644 --- a/lib/solidus_act_as_tenant/utils/tenant_selector.rb +++ b/lib/solidus_acts_as_tenant/utils/tenant_selector.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module SolidusActAsTenant +module SolidusActsAsTenant module Utils class TenantSelector MESSAGES = { @@ -51,13 +51,13 @@ def fetch_tenants end def tenant_names - @tenant_names ||= ::SolidusActAsTenant.config.class_name.constantize.pluck(:name).sort + @tenant_names ||= ::SolidusActsAsTenant.config.class_name.constantize.pluck(:name).sort end def switch(tenant_name) return false unless tenants.value?(tenant_name) - tenant = ::SolidusActAsTenant.config.class_name.constantize.find_by(name: tenant_name) + tenant = ::SolidusActsAsTenant.config.class_name.constantize.find_by(name: tenant_name) ActsAsTenant.current_tenant = tenant true end diff --git a/lib/solidus_acts_as_tenant/version.rb b/lib/solidus_acts_as_tenant/version.rb new file mode 100644 index 0000000..a5568dc --- /dev/null +++ b/lib/solidus_acts_as_tenant/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module SolidusActsAsTenant + VERSION = '0.0.5' +end diff --git a/solidus_act_as_tenant.gemspec b/solidus_acts_as_tenant.gemspec similarity index 66% rename from solidus_act_as_tenant.gemspec rename to solidus_acts_as_tenant.gemspec index efb1b30..267152d 100644 --- a/solidus_act_as_tenant.gemspec +++ b/solidus_acts_as_tenant.gemspec @@ -1,21 +1,21 @@ # frozen_string_literal: true -require_relative 'lib/solidus_act_as_tenant/version' +require_relative 'lib/solidus_acts_as_tenant/version' Gem::Specification.new do |spec| - spec.name = 'solidus_act_as_tenant' - spec.version = SolidusActAsTenant::VERSION + spec.name = 'solidus_acts_as_tenant' + spec.version = SolidusActsAsTenant::VERSION spec.authors = ['Ikraam Ghoor'] spec.email = 'consult.ikraam@gmail.com' spec.summary = 'Adds tenant functionality to solidus using the row level acts_as_tenant gem' spec.description = 'Adds tenant functionality to solidus using the row level acts_as_tenant gem' - spec.homepage = 'https://github.com/solidusio-contrib/solidus_act_as_tenant#readme' + spec.homepage = 'https://github.com/nebulab/solidus_acts_as_tenant#readme' spec.license = 'BSD-3-Clause' spec.metadata['homepage_uri'] = spec.homepage - spec.metadata['source_code_uri'] = 'https://github.com/solidusio-contrib/solidus_act_as_tenant' - spec.metadata['changelog_uri'] = 'https://github.com/solidusio-contrib/solidus_act_as_tenant/blob/main/CHANGELOG.md' + spec.metadata['source_code_uri'] = 'https://github.com/nebulab/solidus_acts_as_tenant' + spec.metadata['changelog_uri'] = 'https://github.com/nebulab/solidus_acts_as_tenant/blob/main/CHANGELOG.md' spec.required_ruby_version = Gem::Requirement.new('>= 2.5', '< 4') @@ -31,8 +31,8 @@ Gem::Specification.new do |spec| spec.add_dependency 'acts_as_tenant', '~> 1.0.1' spec.add_dependency 'solidus_core', ['>= 2.0.0', '< 5'] - spec.add_dependency 'solidus_support', '~> 0.5' + spec.add_dependency 'solidus_support', '>= 0.14.1' spec.add_development_dependency 'pry' - spec.add_development_dependency 'solidus_dev_support', '~> 2.9' + spec.add_development_dependency 'solidus_dev_support', '~> 2.11' end diff --git a/spec/lib/solidus_act_as_tenant/configuration_spec.rb b/spec/lib/solidus_acts_as_tenant/configuration_spec.rb similarity index 61% rename from spec/lib/solidus_act_as_tenant/configuration_spec.rb rename to spec/lib/solidus_acts_as_tenant/configuration_spec.rb index 001fba0..9952db3 100644 --- a/spec/lib/solidus_act_as_tenant/configuration_spec.rb +++ b/spec/lib/solidus_acts_as_tenant/configuration_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe SolidusActAsTenant::Configuration do +RSpec.describe SolidusActsAsTenant::Configuration do let(:configuration) { described_class.new } describe 'default values' do @@ -41,29 +41,3 @@ end end end - -RSpec.describe SolidusActAsTenant do - describe '.configuration' do - it 'returns the same configuration instance' do - expect(described_class.configuration).to eq(described_class.configuration) - end - - it 'aliases config to configuration' do - expect(described_class.config).to eq(described_class.configuration) - end - end - - describe '.configure' do - it 'yields the configuration instance' do - expect { |b| described_class.configure(&b) }.to yield_with_args(SolidusActAsTenant::Configuration) - end - - it 'allows configuring through a block' do - described_class.configure do |config| - config.tenant_column_name = :custom_tenant_id - end - - expect(described_class.configuration.tenant_column_name).to eq(:custom_tenant_id) - end - end -end diff --git a/spec/lib/solidus_acts_as_tenant/solidus_acts_as_tenant_spec.rb b/spec/lib/solidus_acts_as_tenant/solidus_acts_as_tenant_spec.rb new file mode 100644 index 0000000..9346150 --- /dev/null +++ b/spec/lib/solidus_acts_as_tenant/solidus_acts_as_tenant_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +RSpec.describe SolidusActsAsTenant do + describe '.configuration' do + it 'returns the same configuration instance' do + expect(described_class.configuration).to eq(described_class.configuration) + end + + it 'aliases config to configuration' do + expect(described_class.config).to eq(described_class.configuration) + end + end + + describe '.configure' do + it 'yields the configuration instance' do + expect { |b| described_class.configure(&b) }.to yield_with_args(SolidusActsAsTenant::Configuration) + end + + it 'allows configuring through a block' do + described_class.configure do |config| + config.tenant_column_name = :custom_tenant_id + end + + expect(described_class.configuration.tenant_column_name).to eq(:custom_tenant_id) + end + end +end diff --git a/spec/lib/solidus_act_as_tenant/tenant_aware_spec.rb b/spec/lib/solidus_acts_as_tenant/tenant_aware_spec.rb similarity index 86% rename from spec/lib/solidus_act_as_tenant/tenant_aware_spec.rb rename to spec/lib/solidus_acts_as_tenant/tenant_aware_spec.rb index 51ecee2..3b061b7 100644 --- a/spec/lib/solidus_act_as_tenant/tenant_aware_spec.rb +++ b/spec/lib/solidus_acts_as_tenant/tenant_aware_spec.rb @@ -1,9 +1,9 @@ -describe SolidusActAsTenant::TenantAware do +describe SolidusActsAsTenant::TenantAware do describe '#setup_tenant_aware_models' do - let!(:tenant) { ::SolidusActAsTenant.config.class_name.constantize.find_or_create_by!(name: 'Test') } + let!(:tenant) { ::SolidusActsAsTenant.config.class_name.constantize.find_or_create_by!(name: 'Test') } let!(:tenant2) { create(:tenant, name: 'Test2') } - SolidusActAsTenant.config.tenant_aware_models.each do |klass, validator_attributes| + SolidusActsAsTenant.config.tenant_aware_models.each do |klass, validator_attributes| describe "for #{klass}" do it 'has a tenant accessor and the current tenant is already set', set_tenant: true do expect(klass.constantize.new.tenant).to eq tenant diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 385de30..0322b7e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,8 +21,8 @@ Dir["#{__dir__}/support/**/*.rb"].sort.each { |f| require f } # Requires factories defined in Solidus core and this extension. -# See: lib/solidus_act_as_tenant/testing_support/factories.rb -SolidusDevSupport::TestingSupport::Factories.load_for(SolidusActAsTenant::Engine) +# See: lib/solidus_acts_as_tenant/testing_support/factories.rb +SolidusDevSupport::TestingSupport::Factories.load_for(SolidusActsAsTenant::Engine) RSpec.configure do |config| config.infer_spec_type_from_file_location! diff --git a/spec/support/config.rb b/spec/support/config.rb index e987728..bb7bdd0 100644 --- a/spec/support/config.rb +++ b/spec/support/config.rb @@ -6,14 +6,14 @@ config.before(:each, set_tenant: true) do unless ::ActsAsTenant.current_tenant ::ActsAsTenant.current_tenant = - ::SolidusActAsTenant.config.class_name.constantize.find_or_create_by!(name: 'Test') + ::SolidusActsAsTenant.config.class_name.constantize.find_or_create_by!(name: 'Test') end end config.before(:suite, set_tenant: true) do unless ::ActsAsTenant.current_tenant ::ActsAsTenant.current_tenant = - ::SolidusActAsTenant.config.class_name.constantize.find_or_create_by!(name: 'Test') + ::SolidusActsAsTenant.config.class_name.constantize.find_or_create_by!(name: 'Test') end end