Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to save empty taxons, products and variants #73

Merged
merged 10 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
source 'https://rubygems.org'
source "https://rubygems.org"

solidus_branch = ENV.fetch('SOLIDUS_BRANCH', 'master')
solidus_branch = ENV.fetch("SOLIDUS_BRANCH", "v2.11")
gem "solidus_core", github: "solidusio/solidus", branch: solidus_branch
gem "solidus_frontend", github: "solidusio/solidus", branch: solidus_branch
gem "solidus_backend", github: "solidusio/solidus", branch: solidus_branch

alchemy_branch = ENV.fetch('ALCHEMY_BRANCH', 'main')
alchemy_branch = ENV.fetch("ALCHEMY_BRANCH", "main")
gem "alchemy_cms", github: "AlchemyCMS/alchemy_cms", branch: alchemy_branch
gem 'alchemy-devise', github: "AlchemyCMS/alchemy-devise", branch: 'main'
gem "alchemy-devise", github: "AlchemyCMS/alchemy-devise", branch: "main"

# Specify your gem's dependencies in alchemy-solidus.gemspec
gemspec

gem 'sqlite3'
gem 'pry-rails'
gem 'sprockets', '< 4'
gem "sqlite3"
gem "pry-rails"
gem "sprockets", "< 4"
8 changes: 5 additions & 3 deletions app/models/alchemy/essence_spree_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ module Alchemy
class EssenceSpreeProduct < ActiveRecord::Base
PRODUCT_ID = /\A\d+\z/

belongs_to :product, class_name: 'Spree::Product',
optional: true, foreign_key: 'spree_product_id'
belongs_to :product,
class_name: "Spree::Product",
optional: true,
foreign_key: "spree_product_id"

acts_as_essence(ingredient_column: :product)

def ingredient=(product_or_id)
case product_or_id
when PRODUCT_ID
when PRODUCT_ID, ""
self.spree_product_id = product_or_id
when Spree::Product
self.product = product_or_id
Expand Down
8 changes: 5 additions & 3 deletions app/models/alchemy/essence_spree_taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ module Alchemy
class EssenceSpreeTaxon < ActiveRecord::Base
TAXON_ID = /\A\d+\z/

belongs_to :taxon, class_name: 'Spree::Taxon',
optional: true, foreign_key: 'taxon_id'
belongs_to :taxon,
class_name: "Spree::Taxon",
optional: true,
foreign_key: "taxon_id"

acts_as_essence(ingredient_column: :taxon)

def ingredient=(taxon_or_id)
case taxon_or_id
when TAXON_ID
when TAXON_ID, ""
self.taxon_id = taxon_or_id
when Spree::Taxon
self.taxon = taxon_or_id
Expand Down
6 changes: 4 additions & 2 deletions app/models/alchemy/essence_spree_variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ module Alchemy
class EssenceSpreeVariant < ActiveRecord::Base
VARIANT_ID = /\A\d+\z/

belongs_to :variant, class_name: 'Spree::Variant', optional: true
belongs_to :variant,
class_name: "Spree::Variant",
optional: true

acts_as_essence(ingredient_column: :variant)

def ingredient=(variant_or_id)
case variant_or_id
when VARIANT_ID
when VARIANT_ID, ""
self.variant_id = variant_or_id
when Spree::Variant
self.variant = variant_or_id
Expand Down
40 changes: 26 additions & 14 deletions spec/models/alchemy/essence_spree_product_spec.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
# frozen_string_literal: true

require 'rails_helper'
require 'alchemy/test_support/essence_shared_examples'
require "rails_helper"
require "alchemy/test_support/essence_shared_examples"

require_dependency 'alchemy/site'
require_dependency "alchemy/site"

RSpec.describe Alchemy::EssenceSpreeProduct, type: :model do
let(:product) { build_stubbed(:product) }
let(:essence) { described_class.new(product: product) }

it_behaves_like 'an essence' do
it_behaves_like "an essence" do
let(:ingredient_value) { product }
end

describe 'ingredient=' do
context 'when String value is only a number' do
let(:value) { '101' }
describe "ingredient=" do
context "when String value is empty" do
let(:value) { "" }

before do
essence.ingredient = value
end

it 'sets spree_product_id with that id' do
it "sets spree_product_id to nil" do
expect(essence.spree_product_id).to eq(nil)
end
end

context "when String value is only a number" do
let(:value) { "101" }

before do
essence.ingredient = value
end

it "sets spree_product_id with that id" do
expect(essence.spree_product_id).to eq(101)
end
end

context 'when value is an Spree Variant' do
context "when value is an Spree Variant" do
let(:value) { product }

before do
essence.ingredient = value
end

it 'sets product to that product' do
it "sets product to that product" do
expect(essence.product).to eq(product)
end
end

context 'when value is not only a number' do
let(:value) { 'product1' }
context "when value is not only a number" do
let(:value) { "product1" }

it do
expect {
Expand All @@ -49,10 +61,10 @@
end
end

describe '#preview_text' do
describe "#preview_text" do
subject { essence.preview_text(nil) }

it 'returns the products name' do
it "returns the products name" do
is_expected.to eq(product.name)
end
end
Expand Down
40 changes: 26 additions & 14 deletions spec/models/alchemy/essence_spree_taxon_spec.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
# frozen_string_literal: true

require 'rails_helper'
require 'alchemy/test_support/essence_shared_examples'
require "rails_helper"
require "alchemy/test_support/essence_shared_examples"

require_dependency 'alchemy/site'
require_dependency "alchemy/site"

RSpec.describe Alchemy::EssenceSpreeTaxon, type: :model do
let(:taxon) { build_stubbed(:taxon) }
let(:essence) { described_class.new(taxon: taxon) }

it_behaves_like 'an essence' do
it_behaves_like "an essence" do
let(:ingredient_value) { taxon }
end

describe 'ingredient=' do
context 'when String value is only a number' do
let(:value) { '101' }
describe "ingredient=" do
context "when String is empty" do
let(:value) { "" }

before do
essence.ingredient = value
end

it 'sets taxon_id with that id' do
it "sets taxon_id to nil" do
expect(essence.taxon_id).to eq(nil)
end
end

context "when String value is only a number" do
let(:value) { "101" }

before do
essence.ingredient = value
end

it "sets taxon_id with that id" do
expect(essence.taxon_id).to eq(101)
end
end

context 'when value is an Spree Variant' do
context "when value is an Spree Variant" do
let(:value) { taxon }

before do
essence.ingredient = value
end

it 'sets taxon to that taxon' do
it "sets taxon to that taxon" do
expect(essence.taxon).to eq(taxon)
end
end

context 'when value is not only a number' do
let(:value) { 'taxon1' }
context "when value is not only a number" do
let(:value) { "taxon1" }

it do
expect {
Expand All @@ -49,10 +61,10 @@
end
end

describe '#preview_text' do
describe "#preview_text" do
subject { essence.preview_text(nil) }

it 'returns the taxons name' do
it "returns the taxons name" do
is_expected.to eq(taxon.name)
end
end
Expand Down
40 changes: 26 additions & 14 deletions spec/models/alchemy/essence_spree_variant_spec.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
# frozen_string_literal: true

require 'rails_helper'
require 'alchemy/test_support/essence_shared_examples'
require "rails_helper"
require "alchemy/test_support/essence_shared_examples"

require_dependency 'alchemy/site'
require_dependency "alchemy/site"

RSpec.describe Alchemy::EssenceSpreeVariant, type: :model do
let(:variant) { build(:variant) }
let(:essence) { described_class.new(variant: variant) }

it_behaves_like 'an essence' do
it_behaves_like "an essence" do
let(:ingredient_value) { variant }
end

describe 'ingredient=' do
context 'when String value is only a number' do
let(:value) { '101' }
describe "ingredient=" do
context "when String value is empty" do
let(:value) { "" }

before do
essence.ingredient = value
end

it 'sets variant_id with that id' do
it "sets variant_id to nil" do
expect(essence.variant_id).to eq(nil)
end
end

context "when String value is only a number" do
let(:value) { "101" }

before do
essence.ingredient = value
end

it "sets variant_id with that id" do
expect(essence.variant_id).to eq(101)
end
end

context 'when value is an Spree Variant' do
context "when value is an Spree Variant" do
let(:value) { variant }

before do
essence.ingredient = value
end

it 'sets variant to that variant' do
it "sets variant to that variant" do
expect(essence.variant).to eq(variant)
end
end

context 'when value is not only a number' do
let(:value) { 'variant1' }
context "when value is not only a number" do
let(:value) { "variant1" }

it do
expect {
Expand All @@ -49,10 +61,10 @@
end
end

describe '#preview_text' do
describe "#preview_text" do
subject { essence.preview_text(nil) }

it 'returns the variants name' do
it "returns the variants name" do
is_expected.to eq(variant.descriptive_name)
end
end
Expand Down
29 changes: 15 additions & 14 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../dummy/config/environment', __FILE__)
require "spec_helper"
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../dummy/config/environment", __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require "rspec/rails"
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
Expand All @@ -22,32 +22,33 @@
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'factory_bot'
require 'ffaker'
require "capybara/rspec"
require "capybara-screenshot/rspec"
require "factory_bot"
require "ffaker"

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
# ActiveRecord::Migration.maintain_test_schema!

require 'shoulda-matchers'
require "shoulda-matchers"
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :active_record
end
end

require 'spree/testing_support/factories'
require "spree/testing_support/factories"

require "alchemy/version"
if Alchemy.gem_version >= Gem::Version.new("5.2.0")
require 'alchemy/test_support'
require "alchemy/test_support"

FactoryBot.definition_file_paths.concat(Alchemy::TestSupport.factory_paths)
FactoryBot.reload
FactoryBot.definition_file_paths.prepend(Alchemy::TestSupport.factories_path)
FactoryBot.find_definitions
else
require 'alchemy/test_support/factories'
require "alchemy/test_support/factories"
end

RSpec.configure do |config|
Expand Down