diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 12bb10e..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,35 +0,0 @@ -version: 2.1 - -orbs: - # Always take the latest version of the orb, this allows us to - # run specs against Solidus supported versions only without the need - # to change this configuration every time a Solidus version is released - # or goes EOL. - solidusio_extensions: solidusio/extensions@volatile - -jobs: - run-specs-with-postgres: - executor: solidusio_extensions/postgres - steps: - - solidusio_extensions/run-tests - run-specs-with-mysql: - executor: solidusio_extensions/mysql - steps: - - solidusio_extensions/run-tests - -workflows: - "Run specs on supported Solidus versions": - jobs: - - run-specs-with-postgres - - run-specs-with-mysql - "Weekly run specs against master": - triggers: - - schedule: - cron: "0 0 * * 4" # every Thursday - filters: - branches: - only: - - master - jobs: - - run-specs-with-postgres - - run-specs-with-mysql diff --git a/.github/stale.yml b/.github/stale.yml index d9f6563..03407b3 100644 --- a/.github/stale.yml +++ b/.github/stale.yml @@ -1,17 +1,17 @@ # Number of days of inactivity before an issue becomes stale daysUntilStale: 60 # Number of days of inactivity before a stale issue is closed -daysUntilClose: 7 +daysUntilClose: false # Issues with these labels will never be considered stale exemptLabels: - pinned - security # Label to use when marking an issue as stale -staleLabel: wontfix +staleLabel: stale # Comment to post when marking an issue as stale. Set to `false` to disable markComment: > This issue has been automatically marked as stale because it has not had - recent activity. It will be closed if no further activity occurs. Thank you + recent activity. It might be closed if no further activity occurs. Thank you for your contributions. # Comment to post when closing a stale issue. Set to `false` to disable -closeComment: false \ No newline at end of file +closeComment: false diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..395fdef --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,17 @@ +name: Test + +on: + push: + branches: + - master + pull_request: + schedule: + - cron: "0 0 * * 4" # every Thursday + workflow_call: + +permissions: + contents: read + +jobs: + rspec: + uses: solidusio/test-solidus-extension/.github/workflows/test.yml@main diff --git a/.github_changelog_generator b/.github_changelog_generator new file mode 100644 index 0000000..eac0962 --- /dev/null +++ b/.github_changelog_generator @@ -0,0 +1,2 @@ +issues=false +exclude-labels=infrastructure diff --git a/.gitignore b/.gitignore index bcd4aea..325c3c9 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ pkg *.swp spec/dummy spec/examples.txt +/sandbox +.rvmrc +.ruby-version +.ruby-gemset diff --git a/.rspec b/.rspec index 2559e39..83e16f8 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,2 @@ --color ---format progress --require spec_helper diff --git a/.rubocop.yml b/.rubocop.yml index 544964e..b075a8f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,5 @@ require: - solidus_dev_support/rubocop -inherit_from: .rubocop_todo.yml +AllCops: + NewCops: disable diff --git a/Gemfile b/Gemfile index be712db..ce5eb72 100644 --- a/Gemfile +++ b/Gemfile @@ -6,10 +6,16 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } branch = ENV.fetch('SOLIDUS_BRANCH', 'master') gem 'solidus', github: 'solidusio/solidus', branch: branch +# 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' +gem 'solidus_globalize', github: 'solidusio-contrib/solidus_globalize' -case ENV['DB'] +case ENV.fetch('DB', nil) when 'mysql' gem 'mysql2' when 'postgresql' @@ -26,5 +32,8 @@ end gemspec # Use a local Gemfile to include development dependencies that might not be -# relevant for the project or for other contributors, e.g.: `gem 'pry-debug'`. -eval_gemfile 'Gemfile-local' if File.exist? 'Gemfile-local' +# relevant for the project or for other contributors, e.g. pry-byebug. +# +# We use `send` instead of calling `eval_gemfile` to work around an issue with +# how Dependabot parses projects: https://github.com/dependabot/dependabot-core/issues/1658. +send(:eval_gemfile, 'Gemfile-local') if File.exist? 'Gemfile-local' diff --git a/README.md b/README.md index 5302fbb..fd456e7 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,25 @@ -# Product Assembly +# Solidus Product Assembly -[![CircleCI](https://circleci.com/gh/solidusio-contrib/solidus_product_assembly.svg?style=svg)](https://circleci.com/gh/solidusio-contrib/solidus_product_assembly) +[![CircleCI](https://circleci.com/gh/solidusio-contrib/solidus_product_assembly.svg?style=shield)](https://circleci.com/gh/solidusio-contrib/solidus_product_assembly) +[![codecov](https://codecov.io/gh/solidusio-contrib/solidus_product_assembly/branch/master/graph/badge.svg)](https://codecov.io/gh/solidusio-contrib/solidus_product_assembly) Create a product which is composed of other products. ## Installation -Add the following line to your `Gemfile`: +Add solidus_product_assembly to your Gemfile: ```ruby -gem 'solidus_product_assembly', github: 'solidusio-contrib/solidus_product_assembly', branch: 'master' +gem 'solidus_product_assembly' ``` -Run bundle install as well as the extension intall command to copy and run migrations and -append solidus_product_assembly to your js manifest file: +Bundle your dependencies and run the installation generator: - bundle install - rails g solidus_product_assembly:install +```shell +bin/rails generate solidus_product_assembly:install +``` -## Use +## Usage To build a bundle (assembly product) you'd need to first check the "Can be part" flag on each product you want to be part of the bundle. Then create a product @@ -30,85 +31,75 @@ That means you essentially have a product composed of other products. From a customer perspective it's like they are paying a single amount for a collection of products. -## Releasing a new version +## Development + +### Testing the extension -#### 1. Bump gem version and push to RubyGems +First bundle your dependencies, then run `bin/rake`. `bin/rake` will default to building the dummy +app if it does not exist, then it will run specs. The dummy app can be regenerated by using +`bin/rake extension:test_app`. -We use [gem-release](https://github.com/svenfuchs/gem-release) to release this -extension with ease. +```shell +bin/rake +``` -Supposing you are on the master branch and you are working on a fork of this -extension, `upstream` is the main remote and you have write access to it, you -can simply run: +To run [Rubocop](https://github.com/bbatsov/rubocop) static code analysis run -```bash -gem bump --version minor --tag --release +```shell +bundle exec rubocop ``` -This command will: +When testing your application's integration with this extension you may use its factories. +Simply add this require statement to your `spec/spec_helper.rb`: -- bump the gem version to the next minor (changing the `version.rb` file) -- commit the change and push it to upstream master -- create a git tag -- push the tag to the upstream remote -- release the new version on RubyGems +```ruby +require 'solidus_product_assembly/testing_support/factories' +``` -Or you can run these commands individually: +Or, if you are using `FactoryBot.definition_file_paths`, you can load Solidus core +factories along with this extension's factories using this statement: -```bash -gem bump --version minor -gem tag -gem release +```ruby +SolidusDevSupport::TestingSupport::Factories.load_for(SolidusProductAssembly::Engine) ``` -#### 2. Publish the updated CHANGELOG +### Running the sandbox + +To run this extension in a sandboxed Solidus application, you can run `bin/sandbox`. The path for +the sandbox app is `./sandbox` and `bin/rails` will forward any Rails commands to +`sandbox/bin/rails`. + +Here's an example: -After the release is done we can generate the updated CHANGELOG -using -[github-changelog-generator](https://github.com/github-changelog-generator/github-changelog-generator) -by running the following command: +``` +$ bin/rails server +=> Booting Puma +=> Rails 6.0.2.1 application starting in development +* Listening on tcp://127.0.0.1:3000 +Use Ctrl-C to stop +``` +### Updating the changelog -```bash -bundle exec github_changelog_generator solidusio/solidus_auth_devise --token YOUR_GITHUB_TOKEN -git commit -am 'Update CHANGELOG' -git push upstream master +Before and after releases the changelog should be updated to reflect the up-to-date status of +the project: + +```shell +bin/rake changelog +git add CHANGELOG.md +git commit -m "Update the changelog" ``` +### Releasing new versions + +Please refer to the dedicated [page](https://github.com/solidusio/solidus/wiki/How-to-release-extensions) on Solidus wiki. + ## Contributing -Spree is an open source project and we encourage contributions. Please see the [Community Guidelines][1] before contributing. - -In the spirit of [free software][2], **everyone** is encouraged to help improve this project. - -Here are some ways *you* can contribute: - -* by using prerelease versions -* by reporting [bugs][3] -* by suggesting new features -* by writing translations -* by writing or editing documentation -* by writing specifications -* by writing code (*no patch is too small*: fix typos, add comments, clean up inconsistent whitespace) -* by refactoring code -* by resolving [issues][3] -* by reviewing patches - -Starting point: - -* Fork the repo -* Clone your repo -* Run `bundle install` -* Run `bundle exec rake test_app` to create the test application in `spec/test_app` -* Make your changes -* Ensure specs pass by running `bundle exec rspec spec` -* Submit your pull request - -Copyright (c) 2014 [Spree Commerce Inc.][4] and [contributors][5], released under the [New BSD License][6] - -[1]: https://solidus.io/community-guidelines/ -[2]: http://www.fsf.org/licensing/essays/free-sw.html -[3]: https://github.com/spree/spree-product-assembly/issues -[4]: https://github.com/spree -[5]: https://github.com/spree/spree-product-assembly/graphs/contributors -[6]: https://github.com/spree/spree-product-assembly/blob/master/LICENSE.md +Spree is an open source project and we encourage contributions. Please see the [Community Guidelines](https://solidus.io/community-guidelines/) before contributing. + +In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project. + +## License + +Copyright (c) 2014 [Spree Commerce Inc.](https://github.com/spree) and [contributors](https://github.com/spree/spree-product-assembly/graphs/contributors), released under the [New BSD License](LICENSE) diff --git a/app/decorators/models/solidus_product_assembly/spree/product_decorator.rb b/app/decorators/models/solidus_product_assembly/spree/product_decorator.rb index 34ce51e..2447de9 100644 --- a/app/decorators/models/solidus_product_assembly/spree/product_decorator.rb +++ b/app/decorators/models/solidus_product_assembly/spree/product_decorator.rb @@ -14,12 +14,25 @@ def self.prepended(base) scope :individual_saled, -> { where(individual_sale: true) } - scope :search_can_be_part, ->(query){ - not_deleted.available.joins(:master) - .where(arel_table["name"].matches("%#{query}%").or(::Spree::Variant.arel_table["sku"].matches("%#{query}%"))) - .where(can_be_part: true) - .limit(30) - } + if defined?(SolidusGlobalize) + scope :search_can_be_part, ->(query){ + not_deleted.available.joins(:master) + .joins(:translations) + .where( + ::Spree::Product::Translation.arel_table["name"].matches(query) + .or(::Spree::Variant.arel_table["sku"].matches(query)) + ) + .where(can_be_part: true) + .limit(30) + } + else + scope :search_can_be_part, ->(query){ + not_deleted.available.joins(:master) + .where(arel_table["name"].matches(query).or(::Spree::Variant.arel_table["sku"].matches(query))) + .where(can_be_part: true) + .limit(30) + } + end validate :assembly_cannot_be_part, if: :assembly? end diff --git a/bin/rails b/bin/rails index c535fd2..6dbbbc3 100755 --- a/bin/rails +++ b/bin/rails @@ -1,15 +1,7 @@ #!/usr/bin/env ruby -# frozen_string_literal: true - -app_root = 'spec/dummy' - -unless File.exist? "#{app_root}/bin/rails" - system "bin/rake", app_root or begin # rubocop:disable Style/AndOr - warn "Automatic creation of the dummy app failed" - exit 1 - end +if %w[g generate].include? ARGV.first + exec "#{__dir__}/rails-engine", *ARGV +else + exec "#{__dir__}/rails-sandbox", *ARGV end - -Dir.chdir app_root -exec 'bin/rails', *ARGV diff --git a/bin/rails-engine b/bin/rails-engine new file mode 100755 index 0000000..ca1b7f9 --- /dev/null +++ b/bin/rails-engine @@ -0,0 +1,13 @@ +#!/usr/bin/env ruby +# This command will automatically be run when you run "rails" with Rails gems +# installed from the root of your application. + +ENGINE_ROOT = File.expand_path('..', __dir__) +ENGINE_PATH = File.expand_path('../lib/solidus_product_assembly/engine', __dir__) + +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) + +require 'rails/all' +require 'rails/engine/commands' diff --git a/bin/rails-sandbox b/bin/rails-sandbox new file mode 100755 index 0000000..ad2df04 --- /dev/null +++ b/bin/rails-sandbox @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby + +app_root = 'sandbox' + +unless File.exist? "#{app_root}/bin/rails" + warn 'Creating the sandbox app...' + Dir.chdir "#{__dir__}/.." do + system "#{__dir__}/sandbox" or begin + warn 'Automatic creation of the sandbox app failed' + exit 1 + end + end +end + +Dir.chdir app_root +exec 'bin/rails', *ARGV diff --git a/bin/rake b/bin/rake new file mode 100755 index 0000000..1e6eacd --- /dev/null +++ b/bin/rake @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rake", "rake") diff --git a/bin/sandbox b/bin/sandbox new file mode 100755 index 0000000..d1b0b3e --- /dev/null +++ b/bin/sandbox @@ -0,0 +1,86 @@ +#!/usr/bin/env bash + +set -e + +case "$DB" in +postgres|postgresql) + RAILSDB="postgresql" + ;; +mysql) + RAILSDB="mysql" + ;; +sqlite|'') + RAILSDB="sqlite3" + ;; +*) + echo "Invalid DB specified: $DB" + exit 1 + ;; +esac + +if [ ! -z $SOLIDUS_BRANCH ] +then + BRANCH=$SOLIDUS_BRANCH +else + BRANCH="master" +fi + +extension_name="solidus_product_assembly" + +# Stay away from the bundler env of the containing extension. +function unbundled { + ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- $@ +} + +rm -rf ./sandbox +unbundled bundle exec rails new sandbox --database="$RAILSDB" \ + --skip-bundle \ + --skip-git \ + --skip-keeps \ + --skip-rc \ + --skip-spring \ + --skip-test \ + --skip-javascript + +if [ ! -d "sandbox" ]; then + echo 'sandbox rails application failed' + exit 1 +fi + +cd ./sandbox +cat <> Gemfile +gem 'solidus', github: 'solidusio/solidus', branch: '$BRANCH' +gem 'solidus_auth_devise', '>= 2.1.0' +gem 'rails-i18n' +gem 'solidus_i18n' + +gem '$extension_name', path: '..' + +group :test, :development do + platforms :mri do + gem 'pry-byebug' + end +end +RUBY + +unbundled bundle install --gemfile Gemfile + +unbundled bundle exec rake db:drop db:create + +unbundled bundle exec rails generate solidus:install \ + --auto-accept \ + --user_class=Spree::User \ + --enforce_available_locales=true \ + --with-authentication=false \ + --payment-method=none \ + $@ + +unbundled bundle exec rails generate solidus:auth:install +unbundled bundle exec rails generate ${extension_name}:install + +echo +echo "🚀 Sandbox app successfully created for $extension_name!" +echo "🚀 Using $RAILSDB and Solidus $BRANCH" +echo "🚀 Use 'export DB=[postgres|mysql|sqlite]' to control the DB adapter" +echo "🚀 Use 'export SOLIDUS_BRANCH=' to control the Solidus version" +echo "🚀 This app is intended for test purposes." diff --git a/bin/setup b/bin/setup index 40d7811..67d9193 100755 --- a/bin/setup +++ b/bin/setup @@ -5,4 +5,4 @@ set -vx gem install bundler --conservative bundle update -bundle exec rake clobber +bin/rake clobber diff --git a/lib/generators/solidus_product_assembly/install/install_generator.rb b/lib/generators/solidus_product_assembly/install/install_generator.rb index 19481f4..7707974 100644 --- a/lib/generators/solidus_product_assembly/install/install_generator.rb +++ b/lib/generators/solidus_product_assembly/install/install_generator.rb @@ -4,24 +4,32 @@ module SolidusProductAssembly module Generators class InstallGenerator < Rails::Generators::Base class_option :auto_run_migrations, type: :boolean, default: false + source_root File.expand_path('templates', __dir__) - def add_migrations - run 'bundle exec rake railties:install:migrations FROM=solidus_product_assembly' + def copy_initializer + template 'initializer.rb', 'config/initializers/solidus_product_assembly.rb' end def add_javascripts - append_file( - 'vendor/assets/javascripts/spree/backend/all.js', - "//= require spree/backend/solidus_product_assembly\n" - ) + append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/solidus_product_assembly\n" + append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/solidus_product_assembly\n" + end + + def add_stylesheets + inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/solidus_product_assembly\n", before: %r{\*/}, verbose: true # rubocop:disable Layout/LineLength + inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/solidus_product_assembly\n", before: %r{\*/}, verbose: true # rubocop:disable Layout/LineLength + end + + def add_migrations + run 'bin/rails railties:install:migrations FROM=solidus_product_assembly' end def run_migrations - 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 'bundle exec rake db:migrate' + run 'bin/rails db:migrate' else - puts 'Skipping rake db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output + puts 'Skipping bin/rails db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output end end end diff --git a/lib/generators/solidus_product_assembly/install/templates/initializer.rb b/lib/generators/solidus_product_assembly/install/templates/initializer.rb new file mode 100644 index 0000000..7563249 --- /dev/null +++ b/lib/generators/solidus_product_assembly/install/templates/initializer.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +SolidusProductAssembly.configure do |config| + # TODO: Remember to change this with the actual preferences you have implemented! + # config.sample_preference = 'sample_value' +end diff --git a/lib/solidus_product_assembly.rb b/lib/solidus_product_assembly.rb index abe78f1..ff4b12b 100644 --- a/lib/solidus_product_assembly.rb +++ b/lib/solidus_product_assembly.rb @@ -2,6 +2,8 @@ require 'solidus_core' require 'solidus_support' +require 'deface' + require 'solidus_product_assembly/version' require 'solidus_product_assembly/engine' -require 'deface' +require 'solidus_product_assembly/configuration' diff --git a/lib/solidus_product_assembly/configuration.rb b/lib/solidus_product_assembly/configuration.rb new file mode 100644 index 0000000..c988d50 --- /dev/null +++ b/lib/solidus_product_assembly/configuration.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module SolidusProductAssembly + class Configuration + # Define here the settings for this extension, e.g.: + # + # attr_accessor :my_setting + end + + class << self + def configuration + @configuration ||= Configuration.new + end + + alias config configuration + + def configure + yield configuration + end + end +end diff --git a/lib/solidus_product_assembly/engine.rb b/lib/solidus_product_assembly/engine.rb index 92d5333..ccd5932 100644 --- a/lib/solidus_product_assembly/engine.rb +++ b/lib/solidus_product_assembly/engine.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true -require 'spree/core' +require 'solidus_core' +require 'solidus_support' module SolidusProductAssembly class Engine < Rails::Engine diff --git a/solidus_product_assembly.gemspec b/solidus_product_assembly.gemspec index aa3128d..cd8e8fa 100644 --- a/solidus_product_assembly.gemspec +++ b/solidus_product_assembly.gemspec @@ -1,40 +1,41 @@ # frozen_string_literal: true -$:.push File.expand_path('lib', __dir__) -require 'solidus_product_assembly/version' +require_relative 'lib/solidus_product_assembly/version' Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY - s.name = 'solidus_product_assembly' - s.version = SolidusProductAssembly::VERSION - s.summary = 'Adds oportunity to make bundle of products to your Spree store' + s.name = 'solidus_product_assembly' + s.version = SolidusProductAssembly::VERSION + s.authors = ['Roman Smirnov'] + s.email = 'roman@railsdog.com' + + s.summary = 'Adds opportunity to make bundle of products to your Spree store' s.description = s.summary - s.license = 'BSD-3-Clause' + s.homepage = 'https://solidus.io' + s.license = 'BSD-3-Clause' + + s.metadata['homepage_uri'] = s.homepage + s.metadata['source_code_uri'] = 'https://github.com/solidusio-contrib/solidus_product_assembly' + s.metadata['changelog_uri'] = 'https://github.com/SuperGoodSoft/solidus_product_assembly/blob/master/CHANGELOG.md' s.required_ruby_version = '>= 2.4' - s.author = 'Roman Smirnov' - s.email = 'roman@railsdog.com' - s.homepage = 'https://solidus.io' + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem that have been added into git. + files = Dir.chdir(__dir__) { `git ls-files -z`.split("\x0") } - s.files = Dir.chdir(File.expand_path(__dir__)) do - `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } - end - s.test_files = Dir['spec/**/*'] + s.files = files.grep_v(%r{^(test|spec|features)/}) + s.test_files = files.grep(%r{^(test|spec|features)/}) s.bindir = "exe" - s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) } + s.executables = files.grep(%r{^exe/}) { |f| File.basename(f) } s.require_paths = ["lib"] - if s.respond_to?(:metadata) - s.metadata["homepage_uri"] = s.homepage if s.homepage - s.metadata["source_code_uri"] = "https://github.com/solidusio-contrib/solidus_product_assembly" - end - - s.add_dependency 'deface' - s.add_dependency 'solidus_core', ['>= 1.0', '< 4'] + s.add_dependency 'solidus_core', ['>= 2.0.0', '< 4'] s.add_dependency 'solidus_support', '~> 0.8' + s.add_dependency 'deface' + s.add_development_dependency 'solidus_dev_support', '~> 2.5' s.add_development_dependency 'github_changelog_generator' s.add_development_dependency 'selenium-webdriver' - s.add_development_dependency 'solidus_dev_support' + s.add_development_dependency 'webdrivers' end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 458f705..f2778cc 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -8,6 +8,24 @@ @master_variant = Spree::Variant.where(is_master: true).find_by(product_id: @product.id) end + describe ".search_can_be_part" do + subject { described_class.search_can_be_part("matching") } + + let!(:name_matching_product) { create :product, can_be_part: true, name: "matching" } + let!(:sku_matching_product) { create :product, can_be_part: true, master: variant } + let(:variant) { create :master_variant, sku: "matching" } + + before do + create :product, can_be_part: false, name: "matching" + create :product, deleted_at: 1.day.ago, can_be_part: true, name: "matching" + create :product, can_be_part: true, name: "Something else" + end + + it "returns non-deleted products matching the search that can be parts" do + expect(subject).to contain_exactly(name_matching_product, sku_matching_product) + end + end + describe "Spree::Product Assembly" do before do @product = create(:product) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 883080e..89ee1fc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,28 +1,33 @@ # frozen_string_literal: true # Configure Rails Environment -ENV['RAILS_ENV'] ||= 'test' +ENV['RAILS_ENV'] = 'test' # Run Coverage report require 'solidus_dev_support/rspec/coverage' -require File.expand_path('dummy/config/environment.rb', __dir__) +# Create the dummy app if it's still missing. +dummy_env = "#{__dir__}/dummy/config/environment.rb" +system 'bin/rake extension:test_app' unless File.exist? dummy_env +require dummy_env # Requires factories and other useful helpers defined in spree_core. require 'solidus_dev_support/rspec/feature_helper' -# This will load Solidus Core factories right before the ones defined in -# lib/extension_name/testing_support/factories/*_factory.rb or -# lib/extension_name/testing_support/factories.rb -SolidusDevSupport::TestingSupport::Factories.load_for(SolidusProductAssembly::Engine) - # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. -Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f } +Dir["#{__dir__}/support/**/*.rb"].sort.each { |f| require f } + +# Requires factories defined in lib/solidus_product_assembly/testing_support/factories.rb +SolidusDevSupport::TestingSupport::Factories.load_for(SolidusProductAssembly::Engine) RSpec.configure do |config| config.infer_spec_type_from_file_location! config.use_transactional_fixtures = false + + if Spree.solidus_gem_version < Gem::Version.new('2.11') + config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :system + end end Capybara.javascript_driver = (ENV['CAPYBARA_DRIVER'] || :selenium_headless).to_sym