Skip to content

Commit

Permalink
Add a EssenceSpreeVariant essence
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Nov 7, 2019
1 parent 3258bf6 commit 616bd7a
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
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)
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.
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
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

0 comments on commit 616bd7a

Please sign in to comment.