-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
app/views/alchemy/essences/_essence_spree_variant_editor.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
9 changes: 9 additions & 0 deletions
9
db/migrate/20191107135822_create_alchemy_essence_spree_variants.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
spec/views/alchemy/essences/essence_spree_variant_editor_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |