-
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.
Merge pull request #52 from tvdeyen/taxon-select2
Use Select2 ajax for Taxon select
- Loading branch information
Showing
11 changed files
with
191 additions
and
30 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
//= require alchemy/solidus/admin/product_select | ||
//= require alchemy/solidus/admin/variant_select | ||
//= require alchemy/solidus/admin/taxon_select |
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
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
32 changes: 32 additions & 0 deletions
32
app/assets/javascripts/alchemy/solidus/admin/taxon_select.js
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,32 @@ | ||
//= require alchemy/solidus/admin/select2_config | ||
|
||
$.fn.alchemyTaxonSelect = function(options) { | ||
var config = Alchemy.Solidus.getSelect2Config(options) | ||
|
||
this.select2($.extend(true, config, { | ||
ajax: { | ||
data: function(term, page) { | ||
return { | ||
q: $.extend({ | ||
name_cont: term | ||
}, options.query_params), | ||
page: page | ||
} | ||
}, | ||
results: function(data, page) { | ||
return { | ||
results: data.taxons.map(function(taxon) { | ||
return { | ||
id: taxon.id, | ||
text: taxon.name | ||
} | ||
}), | ||
more: page * data.per_page < data.total_count | ||
} | ||
} | ||
}, | ||
formatSelection: function(taxon) { | ||
return taxon.text || taxon.name | ||
} | ||
})) | ||
} |
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 |
---|---|---|
@@ -1,14 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class EssenceSpreeTaxon < ActiveRecord::Base | ||
belongs_to :taxon, class_name: "Spree::Taxon", foreign_key: 'taxon_id' | ||
TAXON_ID = /\A\d+\z/ | ||
|
||
belongs_to :taxon, class_name: 'Spree::Taxon', | ||
optional: true, foreign_key: 'taxon_id' | ||
|
||
acts_as_essence( | ||
ingredient_column: 'taxon_id', | ||
preview_text_method: 'name' | ||
) | ||
acts_as_essence(ingredient_column: :taxon) | ||
|
||
def ingredient=(taxon_or_id) | ||
case taxon_or_id | ||
when TAXON_ID | ||
self.taxon_id = taxon_or_id | ||
when Spree::Taxon | ||
self.taxon = taxon_or_id | ||
else | ||
super | ||
end | ||
end | ||
|
||
def ingredient | ||
taxon | ||
def preview_text(_maxlength) | ||
return unless taxon | ||
taxon.name | ||
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
31 changes: 20 additions & 11 deletions
31
app/views/alchemy/essences/_essence_spree_taxon_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 |
---|---|---|
@@ -1,15 +1,24 @@ | ||
<div class="content_editor essence_spree_taxon essence_select"> | ||
<div class="content_editor essence_spree_taxon" id="<%= content.dom_id %>" data-content-id="<%= content.id %>"> | ||
<%= content_label(content) %> | ||
<%= select_tag( | ||
<%= text_field_tag( | ||
content.form_field_name, | ||
options_from_collection_for_select( | ||
local_assigns.fetch(:options, {})[:taxons] || Spree::Taxon.all, | ||
:id, | ||
:pretty_name, | ||
content.essence.taxon_id | ||
), | ||
include_blank: t(".none"), | ||
class: ["alchemy_selectbox", "essence_editor_select", html_options[:class]].join(' '), | ||
style: html_options[:style] | ||
content.essence.taxon_id, | ||
id: content.form_field_id, | ||
class: 'alchemy_selectbox full_width' | ||
) %> | ||
</div> | ||
|
||
<script> | ||
$('#<%= content.form_field_id %>').alchemyTaxonSelect({ | ||
placeholder: "<%= Alchemy.t(:search_taxon, scope: 'solidus') %>", | ||
apiToken: "<%= current_alchemy_user.spree_api_key %>", | ||
baseUrl: "<%= spree.api_taxons_path %>", | ||
query_params: <%== content.settings[:query_params].to_json %>, | ||
<% if content.essence.taxon %> | ||
initialSelection: { | ||
id: <%= content.essence.taxon_id %>, | ||
text: "<%= content.essence.taxon.name %>" | ||
} | ||
<% end %> | ||
}) | ||
</script> |
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
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
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,63 @@ | ||
# 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/taxon_factory' | ||
|
||
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 | ||
let(:ingredient_value) { taxon } | ||
end | ||
|
||
describe 'ingredient=' do | ||
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 | ||
let(:value) { taxon } | ||
|
||
before do | ||
essence.ingredient = value | ||
end | ||
|
||
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' } | ||
|
||
it do | ||
expect { | ||
essence.ingredient = value | ||
}.to raise_error(ActiveRecord::AssociationTypeMismatch) | ||
end | ||
end | ||
end | ||
|
||
describe '#preview_text' do | ||
subject { essence.preview_text(nil) } | ||
|
||
it 'returns the taxons name' do | ||
is_expected.to eq(taxon.name) | ||
end | ||
end | ||
end |
34 changes: 34 additions & 0 deletions
34
spec/views/alchemy/essences/essence_spree_taxon_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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'alchemy/essences/_essence_spree_taxon_editor' do | ||
let(:content) { Alchemy::Content.new(essence: essence) } | ||
let(:essence) { Alchemy::EssenceSpreeTaxon.new } | ||
|
||
before do | ||
view.class.send(:include, Alchemy::Admin::EssencesHelper) | ||
allow(view).to receive(:content_label) { content.name } | ||
end | ||
|
||
subject do | ||
render 'alchemy/essences/essence_spree_taxon_editor', | ||
content: content, | ||
spree: double(api_taxons_path: '/api/taxons'), | ||
current_alchemy_user: double(spree_api_key: '123') | ||
rendered | ||
end | ||
|
||
it "renders a taxon input" do | ||
is_expected.to have_css('input.alchemy_selectbox.full_width') | ||
end | ||
|
||
context 'with a taxon related to essence' do | ||
let(:taxon) { Spree::Taxon.new(id: 1) } | ||
let(:essence) { Alchemy::EssenceSpreeTaxon.new(taxon_id: taxon.id) } | ||
|
||
it "sets taxon id as value" do | ||
is_expected.to have_css('input.alchemy_selectbox[value="1"]') | ||
end | ||
end | ||
end |