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

Add essence variants #48

Merged
merged 3 commits into from
Nov 8, 2019
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
1 change: 1 addition & 0 deletions alchemy-solidus.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
gem.add_dependency('deface', ['~> 1.0'])

gem.add_development_dependency('rspec-rails', ['~> 3.7'])
gem.add_development_dependency('shoulda-matchers', ['~> 4.0'])
gem.add_development_dependency('capybara', ['~> 2.15'])
gem.add_development_dependency('capybara-screenshot', ['~> 1.0'])
gem.add_development_dependency('factory_bot', ['~> 4.8'])
Expand Down
25 changes: 25 additions & 0 deletions app/models/alchemy/essence_spree_variant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Alchemy
class EssenceSpreeVariant < ActiveRecord::Base
VARIANT_ID = /\A\d+\z/

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

acts_as_essence(
ingredient_column: :variant,
preview_text_method: :name
)

def ingredient=(variant)
case variant
when VARIANT_ID
self.variant = Spree::Variant.new(id: variant)
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
when Spree::Variant
self.variant = variant
else
super
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="content_editor essence_spree_variant" id="<%= content.dom_id %>" data-content-id="<%= content.id %>">
<%= content_label(content) %>
<%= select_tag(
content.form_field_name,
options_from_collection_for_select(Spree::Variant.all, :id, :name, content.essence.variant_id),
id: content.form_field_id,
class: 'alchemy_selectbox full_width'
) %>
</div>
Empty file.
2 changes: 1 addition & 1 deletion bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ APP_PATH = File.expand_path('../../spec/dummy/config/application', __FILE__)
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])

require 'rails/all'
require 'rspec-rails'
require 'rails/engine/commands'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateAlchemyEssenceSpreeVariants < ActiveRecord::Migration[5.2]
def change
create_table :alchemy_essence_spree_variants do |t|
t.references :variant, null: true, foreign_key: { to_table: Spree::Variant.table_name }

t.timestamps
end
end
end
59 changes: 59 additions & 0 deletions spec/models/alchemy/essence_spree_variant_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require 'rails_helper'
require 'alchemy/test_support/essence_shared_examples'
require 'alchemy/test_support/factories/page_factory'
require 'alchemy/test_support/factories/element_factory'
require 'alchemy/test_support/factories/content_factory'
require 'spree/testing_support/factories/variant_factory'

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
let(:ingredient_value) { variant }
end

describe 'ingredient=' do
subject(:ingredient) { essence.variant }

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

before do
essence.ingredient = value
end

it 'sets variant to an variant instance with that id' do
is_expected.to be_a(Spree::Variant)
expect(ingredient.id).to eq(101)
end
end

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

before do
essence.ingredient = value
end

it 'sets variant to an variant instance with that id' do
is_expected.to be_a(Spree::Variant)
expect(ingredient).to eq(variant)
end
end

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

it do
expect {
essence.ingredient = value
}.to raise_error(ActiveRecord::AssociationTypeMismatch)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
# If you are not using ActiveRecord, you can remove this line.
# ActiveRecord::Migration.maintain_test_schema!

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

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
Expand Down Expand Up @@ -59,4 +67,6 @@
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

config.include FactoryBot::Syntax::Methods
end
33 changes: 33 additions & 0 deletions spec/views/alchemy/essences/essence_spree_variant_editor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'alchemy/essences/_essence_spree_variant_editor' do
let(:content) { Alchemy::Content.new(essence: essence) }
let(:essence) { Alchemy::EssenceSpreeVariant.new }

before do
view.class.send(:include, Alchemy::Admin::EssencesHelper)
allow(view).to receive(:content_label).and_return(content.name)
end

it "renders a variant select box" do
render 'alchemy/essences/essence_spree_variant_editor', content: content
expect(rendered).to have_css('select.alchemy_selectbox.full_width')
end

context 'with a variant related to essence' do
let(:product) { Spree::Product.new(name: 'Chocolate') }
let(:variant) { Spree::Variant.new(id: 1, product: product) }
let(:essence) { Alchemy::EssenceSpreeVariant.new(variant_id: variant.id) }

before do
expect(Spree::Variant).to receive(:all) { [variant] }
end

it "selects variant in select box" do
render 'alchemy/essences/essence_spree_variant_editor', content: content
expect(rendered).to have_css('option[value="1"][selected]')
end
end
end