Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added support for color swatches (#13866)

Damian Legawiec9edd26fd5880
diff --git a/CLAUDE.md b/CLAUDE.md
index 0c0d58aa3e..246f66af5a 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -33,7 +33,7 @@ Current plans:
- `5.4-disjunctive-option-faceting.md` — Per-option-type filter params with disjunctive facet counts (OR within option type, AND across)
-- `5.5-option-type-enhancements.md` — Add `kind` (dropdown/color_swatch/buttons) to OptionType, `color_code` + `image` to OptionValue for swatch support
+- `5.4-option-type-enhancements.md` — Add `kind` (dropdown/color_swatch/buttons) to OptionType, `color_code` + `image` to OptionValue for swatch support
- `5.4-6.0-custom-fields-rename.md` — Rename Metafields → Custom Fields (5.4 API bridge + 6.0 model rename)
Completed plans:
diff --git a/docs/plans/5.5-option-type-enhancements.md b/docs/plans/5.4-option-type-enhancements.md
similarity index 99%
rename from docs/plans/5.5-option-type-enhancements.md
rename to docs/plans/5.4-option-type-enhancements.md
index 2b82a132a6..676ac1278f 100644
--- a/docs/plans/5.5-option-type-enhancements.md
+++ b/docs/plans/5.4-option-type-enhancements.md
@@ -1,10 +1,10 @@
# Option Type Enhancements
-**Status:** Draft
-**Target:** Spree 5.5
+**Status:** Implemented
+**Target:** Spree 5.4
**Depends on:** `5.4-store-api-naming-standardization.md` (`presentation` → `label` bridge)
**Author:** Damian + Claude
-**Last updated:** 2026-03-26
+**Last updated:** 2026-04-02
## Summary
diff --git a/packages/admin-sdk/src/types/generated/OptionType.ts b/packages/admin-sdk/src/types/generated/OptionType.ts
index 602ea70600..e5694629f3 100644
--- a/packages/admin-sdk/src/types/generated/OptionType.ts
+++ b/packages/admin-sdk/src/types/generated/OptionType.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 417b6bff08abe4057131c4771136e22b
+// Typelizer digest b67ef90e0d3df34451e901209ceb5e6e
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {OptionValue} from '@/types'
@@ -8,6 +8,7 @@ interface OptionType {
name: string;
label: string;
position: number;
+ kind: string;
created_at: string;
updated_at: string;
option_values?: Array<OptionValue>;
diff --git a/packages/admin-sdk/src/types/generated/OptionValue.ts b/packages/admin-sdk/src/types/generated/OptionValue.ts
index 979f59e15a..29f6b8ccb1 100644
--- a/packages/admin-sdk/src/types/generated/OptionValue.ts
+++ b/packages/admin-sdk/src/types/generated/OptionValue.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 8c8614fb6bee0678b9cf4477dd0d59e4
+// Typelizer digest 99e45880315db7b7b4e5862ba39ad816
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {OptionType} from '@/types'
@@ -9,8 +9,10 @@ interface OptionValue {
name: string;
label: string;
position: number;
+ color_code: string | null;
option_type_name: string;
option_type_label: string;
+ image_url: string | null;
created_at: string;
updated_at: string;
option_type?: OptionType;
diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md
index 3bf780c715..b05ea35a3d 100644
--- a/packages/sdk/CHANGELOG.md
+++ b/packages/sdk/CHANGELOG.md
@@ -1,5 +1,11 @@
# @spree/sdk
+## 0.20.0
+
+### Minor Changes
+
+- Add option type `kind` field (dropdown/color_swatch/buttons) and option value `color_code` + `image_url` fields for swatch support
+
## 0.19.1
### Patch Changes
diff --git a/packages/sdk/package.json b/packages/sdk/package.json
index 9c4142c3a0..9e776f9891 100644
--- a/packages/sdk/package.json
+++ b/packages/sdk/package.json
@@ -1,6 +1,6 @@
{
"name": "@spree/sdk",
- "version": "0.19.1",
+ "version": "0.20.0",
"description": "Official TypeScript SDK for Spree Commerce Store API",
"type": "module",
"main": "./dist/index.cjs",
diff --git a/packages/sdk/src/types/generated/OptionType.ts b/packages/sdk/src/types/generated/OptionType.ts
index 0c3507bff4..5a969aebb6 100644
--- a/packages/sdk/src/types/generated/OptionType.ts
+++ b/packages/sdk/src/types/generated/OptionType.ts
@@ -1,4 +1,4 @@
-// Typelizer digest d69a4794e4392d769eaf50c280554fda
+// Typelizer digest 8034387e53b965dc346fbebd5860ea34
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -7,6 +7,7 @@ interface OptionType {
name: string;
label: string;
position: number;
+ kind: string;
}
export default OptionType;
diff --git a/packages/sdk/src/types/generated/OptionValue.ts b/packages/sdk/src/types/generated/OptionValue.ts
index 77c4c44a2e..1d48e34f19 100644
--- a/packages/sdk/src/types/generated/OptionValue.ts
+++ b/packages/sdk/src/types/generated/OptionValue.ts
@@ -1,4 +1,4 @@
-// Typelizer digest bb5b3c7aa6954dccb01a600d7507f434
+// Typelizer digest d53b83174a583e30bf1ab4530fb76b80
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -8,8 +8,10 @@ interface OptionValue {
name: string;
label: string;
position: number;
+ color_code: string | null;
option_type_name: string;
option_type_label: string;
+ image_url: string | null;
}
export default OptionValue;
diff --git a/packages/sdk/src/zod/generated/OptionType.ts b/packages/sdk/src/zod/generated/OptionType.ts
index 34bfddde99..2567bf17f1 100644
--- a/packages/sdk/src/zod/generated/OptionType.ts
+++ b/packages/sdk/src/zod/generated/OptionType.ts
@@ -6,6 +6,7 @@ export const OptionTypeSchema = z.object({
name: z.string(),
label: z.string(),
position: z.number(),
+ kind: z.string(),
});
export type OptionType = z.infer<typeof OptionTypeSchema>;
diff --git a/packages/sdk/src/zod/generated/OptionValue.ts b/packages/sdk/src/zod/generated/OptionValue.ts
index ff510d6e9d..567d44a913 100644
--- a/packages/sdk/src/zod/generated/OptionValue.ts
+++ b/packages/sdk/src/zod/generated/OptionValue.ts
@@ -7,8 +7,10 @@ export const OptionValueSchema = z.object({
name: z.string(),
label: z.string(),
position: z.number(),
+ color_code: z.string().nullable(),
option_type_name: z.string(),
option_type_label: z.string(),
+ image_url: z.string().nullable(),
});
export type OptionValue = z.infer<typeof OptionValueSchema>;
diff --git a/spree/admin/app/views/spree/admin/option_types/_form.html.erb b/spree/admin/app/views/spree/admin/option_types/_form.html.erb
index 03e37ae150..2456feee80 100644
--- a/spree/admin/app/views/spree/admin/option_types/_form.html.erb
+++ b/spree/admin/app/views/spree/admin/option_types/_form.html.erb
@@ -12,6 +12,13 @@
<%= f.spree_text_field :name, label: Spree.t(:internal_name), data: { slug_form_target: :url }, help_text: raw("This is used internally to identify the option type. <strong>It must be unique.</strong>") %>
</div>
</div>
+ <div class="grid grid-cols-12 gap-6 mb-6">
+ <div class="col-span-12 md:col-span-6">
+ <%= f.spree_select :kind,
+ Spree::OptionType::KINDS.map { |k| [Spree.t("option_type_kinds.#{k}"), k] },
+ { label: Spree.t(:kind), help: Spree.t(:option_type_kind_info) } %>
+ </div>
+ </div>
<div class="grid grid-cols-12 gap-6">
<div class="col-span-12 md:col-span-6">
<%= f.spree_check_box :filterable, help_text: raw(Spree.t('option_type_filterable_info')) %>
@@ -38,6 +45,7 @@
<th scope="col"></th>
<th scope="col"><%= raw(Spree.t(:presentation) + required_span_tag) %></th>
<th scope="col"><%= raw(Spree.t(:internal_name) + required_span_tag) %></th>
+ <th scope="col"><%= Spree.t(:color_code) %></th>
<th scope="col"></th>
</tr>
</thead>
@@ -51,7 +59,7 @@
<tr data-nested-form-target="target"></tr>
<tr class="hover-none">
- <td colspan="4" class="text-center p-2">
+ <td colspan="5" class="text-center p-2">
<button type="button" data-action="nested-form#add" class="btn btn-secondary py-3 w-full" id="add_option_value_button">
<%= icon('plus') %>
<%= Spree.t(:add_one) %>
diff --git a/spree/admin/app/views/spree/admin/option_types/_option_value_fields.html.erb b/spree/admin/app/views/spree/admin/option_types/_option_value_fields.html.erb
index 697962ac8e..64047de70e 100644
--- a/spree/admin/app/views/spree/admin/option_types/_option_value_fields.html.erb
+++ b/spree/admin/app/views/spree/admin/option_types/_option_value_fields.html.erb
@@ -14,12 +14,26 @@
<% end %>
<%= f.hidden_field :id %>
</td>
- <td class="w-45 presentation">
+ <td class="w-40 presentation">
<%= f.text_field :presentation, class: "form-input", data: { slug_form_target: :name, action: 'slug-form#updateUrlFromName' } %>
</td>
<td class="w-40 name">
<%= f.text_field :name, class: "form-input", data: { slug_form_target: :url } %>
</td>
+ <td class="w-5 color_code">
+ <div data-controller="color-picker" data-color-picker-clear-value="true" data-color-picker-default-color-value="#000000">
+ <div class="flex items-center gap-2">
+ <button type="button"
+ data-color-picker-target="picker"
+ class="w-8 h-8 rounded border border-gray-300 cursor-pointer shrink-0"
+ style="background: <%= f.object.color_code.presence || 'white' %>;">
+ </button>
+ <span data-color-picker-target="value" class="text-sm text-gray-500"><%= f.object.color_code %></span>
+ </div>
+ <%= f.hidden_field :color_code, data: { color_picker_target: :input } %>
+ <span data-color-picker-target="display" class="hidden"></span>
+ </div>
+ </td>
<td class="w-10 actions">
<button type="button" class="btn btn-danger btn-sm remove_option_value_button" data-action="nested-form#remove">
<%= icon('trash', class: 'mr-0') %>
diff --git a/spree/api/app/serializers/spree/api/v3/option_type_serializer.rb b/spree/api/app/serializers/spree/api/v3/option_type_serializer.rb
index 3f669fb070..456e597f33 100644
--- a/spree/api/app/serializers/spree/api/v3/option_type_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/option_type_serializer.rb
@@ -2,9 +2,9 @@ module Spree
module Api
module V3
class OptionTypeSerializer < BaseSerializer
- typelize name: :string, label: :string, position: :number
+ typelize name: :string, label: :string, position: :number, kind: :string
- attributes :name, :label, :position
+ attributes :name, :label, :position, :kind
end
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/option_value_serializer.rb b/spree/api/app/serializers/spree/api/v3/option_value_serializer.rb
index 1b0315e93a..f2d4f5fab4 100644
--- a/spree/api/app/serializers/spree/api/v3/option_value_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/option_value_serializer.rb
@@ -3,13 +3,14 @@ module Spree
module V3
class OptionValueSerializer < BaseSerializer
typelize name: :string, label: :string, position: :number, option_type_id: :string,
- option_type_name: :string, option_type_label: :string
+ option_type_name: :string, option_type_label: :string,
+ color_code: 'string | null', image_url: 'string | null'
attribute :option_type_id do |option_value|
option_value.option_type&.prefixed_id
end
- attributes :name, :label, :position
+ attributes :name, :label, :position, :color_code
attribute :option_type_name do |option_value|
option_value.option_type.name
@@ -18,6 +19,10 @@ module Spree
attribute :option_type_label do |option_value|
option_value.option_type.label
end
+
+ attribute :image_url do |option_value|
+ image_url_for(option_value.image)
+ end
end
end
end
diff --git a/spree/api/app/services/spree/api/v3/filters_aggregator.rb b/spree/api/app/services/spree/api/v3/filters_aggregator.rb
index bc7e898a5f..b1bcc6b492 100644
--- a/spree/api/app/services/spree/api/v3/filters_aggregator.rb
+++ b/spree/api/app/services/spree/api/v3/filters_aggregator.rb
@@ -94,6 +94,7 @@ module Spree
type: 'option',
name: option_type.name,
label: option_type.label,
+ kind: option_type.kind,
options: values.map { |ov| option_value_data(count_ids, ov) }
}
end
@@ -112,6 +113,8 @@ module Spree
name: option_value.name,
label: option_value.label,
position: option_value.position,
+ color_code: option_value.color_code,
+ image_url: option_value.image.attached? ? Rails.application.routes.url_helpers.cdn_image_url(option_value.image) : nil,
count: count
}
end
diff --git a/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb b/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
index edfb2f3501..6196e34c19 100644
--- a/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
+++ b/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
@@ -96,6 +96,11 @@ RSpec.describe Spree::Api::V3::FiltersAggregator do
expect(size_filter[:label]).to eq('Size')
end
+ it 'includes kind in option type filter' do
+ size_filter = result[:filters].find { |f| f[:name] == 'size' }
+ expect(size_filter[:kind]).to eq('dropdown')
+ end
+
it 'includes option values with counts' do
size_filter = result[:filters].find { |f| f[:name] == 'size' }
options = size_filter[:options]
@@ -106,6 +111,39 @@ RSpec.describe Spree::Api::V3::FiltersAggregator do
expect(s_option[:count]).to eq(1)
end
+ it 'includes color_code and image_url in option values' do
+ size_filter = result[:filters].find { |f| f[:name] == 'size' }
+ s_option = size_filter[:options].find { |o| o[:label] == 'S' }
+
+ expect(s_option).to have_key(:color_code)
+ expect(s_option).to have_key(:image_url)
+ expect(s_option[:color_code]).to be_nil
+ expect(s_option[:image_url]).to be_nil
+ end
+
+ context 'with color swatch option type' do
+ let(:color_type) { create(:option_type, :color_swatch, filterable: true) }
+ let(:red) { create(:option_value, option_type: color_type, name: 'red', presentation: 'Red', color_code: '#FF0000') }
+
+ let!(:red_product) do
+ create(:product, stores: [store], status: 'active').tap do |p|
+ p.option_types << color_type
+ create(:variant, product: p, option_values: [red])
+ end
+ end
+
+ it 'returns kind as color_swatch' do
+ color_filter = result[:filters].find { |f| f[:name] == 'color' }
+ expect(color_filter[:kind]).to eq('color_swatch')
+ end
+
+ it 'returns color_code on option values' do
+ color_filter = result[:filters].find { |f| f[:name] == 'color' }
+ red_option = color_filter[:options].find { |o| o[:name] == 'red' }
+ expect(red_option[:color_code]).to eq('#FF0000')
+ end
+ end
+
it 'excludes option types with no values in scope' do
empty_option_type = create(:option_type, name: 'material', presentation: 'Material', filterable: true)
create(:option_value, option_type: empty_option_type, name: 'cotton')
diff --git a/spree/core/app/models/spree/option_type.rb b/spree/core/app/models/spree/option_type.rb
index 07785b60e1..bcbb41aa0e 100644
--- a/spree/core/app/models/spree/option_type.rb
+++ b/spree/core/app/models/spree/option_type.rb
@@ -3,6 +3,7 @@ module Spree
has_prefix_id :opt # Spree-specific: option type
COLOR_NAMES = %w[color colour].freeze
+ KINDS = %w[dropdown color_swatch buttons].freeze
include Spree::ParameterizableName
include Spree::UniqueName
@@ -40,12 +41,14 @@ module Spree
# Validations
#
validates :presentation, presence: true
+ validates :kind, presence: true, inclusion: { in: KINDS }
#
# Scopes
#
default_scope { order(:position) }
scope :colors, -> { where(name: COLOR_NAMES) }
+ scope :color_swatches, -> { where(kind: 'color_swatch') }
scope :filterable, -> { where(filterable: true) }
#
@@ -72,8 +75,15 @@ module Spree
colors.first
end
+ def color_swatch?
+ kind == 'color_swatch'
+ end
+
def color?
- name.in?(COLOR_NAMES)
+ Spree::Deprecation.warn(
+ 'Spree::OptionType#color? is deprecated. Use #color_swatch? instead. Will be removed in Spree 6.0.'
+ )
+ color_swatch?
end
private
diff --git a/spree/core/app/models/spree/option_value.rb b/spree/core/app/models/spree/option_value.rb
index a2f42593f7..b593ddad69 100644
--- a/spree/core/app/models/spree/option_value.rb
+++ b/spree/core/app/models/spree/option_value.rb
@@ -20,6 +20,11 @@ module Spree
acts_as_list scope: :option_type
self.whitelisted_ransackable_attributes = ['presentation']
+ #
+ # Attachments
+ #
+ has_one_attached :image
+
#
# Associations
#
@@ -39,6 +44,9 @@ module Spree
validates :presentation
end
+ validates :color_code, format: { with: /\A#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?\z/, message: 'must be a valid hex color (e.g. #FF0000)' },
+ allow_blank: true
+
#
# Scopes
#
diff --git a/spree/core/app/services/spree/sample_data/loader.rb b/spree/core/app/services/spree/sample_data/loader.rb
index 09de8d5f27..5fb069f284 100644
--- a/spree/core/app/services/spree/sample_data/loader.rb
+++ b/spree/core/app/services/spree/sample_data/loader.rb
@@ -14,6 +14,9 @@ module Spree
puts 'Loading sample metafield definitions...'
load_ruby_file('metafield_definitions')
+ puts 'Loading sample options...'
+ load_ruby_file('options')
+
puts 'Loading sample products...'
load_products
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index a692d50e31..9689d5f15b 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -73,8 +73,12 @@ en:
namespace: Namespace
spree/option_type:
filterable: Filterable
+ kind: Kind
name: Name
presentation: Presentation
+ spree/option_value:
+ color_code: Color Code
+ image: Swatch Image
spree/order:
checkout_complete: Checkout Complete
completed_at: Completed At
@@ -897,6 +901,7 @@ en:
close_sidebar: Close sidebar
closed: Closed
code: Code
+ color_code: Color Code
colors: Colors
company: Company
compare_at_amount: Compare at amount
@@ -1573,6 +1578,11 @@ en:
option_name: Option name
option_type: Option Type
option_type_filterable_info: When an option type is set to Filterable, your storefront visitors are presented with the option to filter a taxon of products based on the option type. A typical example of this would be to filter clothing by size and color.<br><br><b>Please Note:</b> Filters will only be visible in the storefront taxons that contain products with this option type set.
+ option_type_kind_info: Controls how the option is displayed on the storefront. Color swatches show color circles, buttons show clickable chips, and dropdown shows a select menu.
+ option_type_kinds:
+ buttons: Buttons
+ color_swatch: Color Swatch
+ dropdown: Dropdown
option_type_placeholder: Choose an option type
option_types: Option Types
option_value: Option Value
diff --git a/spree/core/db/migrate/20260402000001_add_kind_to_spree_option_types.rb b/spree/core/db/migrate/20260402000001_add_kind_to_spree_option_types.rb
new file mode 100644
index 0000000000..42e42cd860
--- /dev/null
+++ b/spree/core/db/migrate/20260402000001_add_kind_to_spree_option_types.rb
@@ -0,0 +1,13 @@
+class AddKindToSpreeOptionTypes < ActiveRecord::Migration[7.2]
+ def change
+ add_column :spree_option_types, :kind, :string, null: false, default: 'dropdown'
+ add_index :spree_option_types, :kind
+
+ # Backfill: option types named 'color'/'colour' get kind 'color_swatch'
+ reversible do |dir|
+ dir.up do
+ execute "UPDATE spree_option_types SET kind = 'color_swatch' WHERE name IN ('color', 'colour')"
+ end
+ end
+ end
+end
diff --git a/spree/core/db/migrate/20260402000002_add_color_code_to_spree_option_values.rb b/spree/core/db/migrate/20260402000002_add_color_code_to_spree_option_values.rb
new file mode 100644
index 0000000000..55989f8b7b
--- /dev/null
+++ b/spree/core/db/migrate/20260402000002_add_color_code_to_spree_option_values.rb
@@ -0,0 +1,5 @@
+class AddColorCodeToSpreeOptionValues < ActiveRecord::Migration[7.2]
+ def change
+ add_column :spree_option_values, :color_code, :string
+ end
+end
diff --git a/spree/core/db/sample_data/options.rb b/spree/core/db/sample_data/options.rb
new file mode 100644
index 0000000000..6bb32cb3b1
--- /dev/null
+++ b/spree/core/db/sample_data/options.rb
@@ -0,0 +1,4 @@
+color = Spree::OptionType.find_or_initialize_by(name: 'color')
+color.presentation = 'Color'
+color.kind = 'color_swatch'
+color.save!
diff --git a/spree/core/lib/spree/permitted_attributes.rb b/spree/core/lib/spree/permitted_attributes.rb
index 1890613d17..248e1c4637 100644
--- a/spree/core/lib/spree/permitted_attributes.rb
+++ b/spree/core/lib/spree/permitted_attributes.rb
@@ -154,10 +154,10 @@ module Spree
@@metafield_definition_attributes = [:key, :name, :namespace, :metafield_type, :resource_type, :display_on]
- @@option_type_attributes = [:name, :presentation, :position, :filterable,
- option_values_attributes: [:id, :name, :presentation, :position, :_destroy]]
+ @@option_type_attributes = [:name, :presentation, :position, :filterable, :kind,
+ option_values_attributes: [:id, :name, :presentation, :position, :color_code, :image, :_destroy]]
- @@option_value_attributes = [:name, :presentation, :position]
+ @@option_value_attributes = [:name, :presentation, :position, :color_code, :image]
@@page_attributes = [:name, :slug, :meta_title, :meta_description, :meta_keywords]
diff --git a/spree/core/lib/spree/testing_support/factories/options_factory.rb b/spree/core/lib/spree/testing_support/factories/options_factory.rb
index 56392b07a1..513b6b350e 100644
--- a/spree/core/lib/spree/testing_support/factories/options_factory.rb
+++ b/spree/core/lib/spree/testing_support/factories/options_factory.rb
@@ -22,6 +22,17 @@ FactoryBot.define do
trait :color do
name { 'color' }
presentation { 'Color' }
+ kind { 'color_swatch' }
+ end
+
+ trait :color_swatch do
+ name { 'color' }
+ presentation { 'Color' }
+ kind { 'color_swatch' }
+ end
+
+ trait :buttons do
+ kind { 'buttons' }
end
end
end
diff --git a/spree/core/spec/models/spree/option_type_spec.rb b/spree/core/spec/models/spree/option_type_spec.rb
index f3b38008f1..c3e837fb00 100644
--- a/spree/core/spec/models/spree/option_type_spec.rb
+++ b/spree/core/spec/models/spree/option_type_spec.rb
@@ -70,8 +70,55 @@ describe Spree::OptionType, type: :model do
end
end
+ describe 'kind' do
+ it 'defaults to dropdown' do
+ option_type = create(:option_type)
+ expect(option_type.kind).to eq('dropdown')
+ end
+
+ it 'validates inclusion in KINDS' do
+ option_type = build(:option_type, kind: 'invalid')
+ expect(option_type).not_to be_valid
+ expect(option_type.errors[:kind]).to include('is not included in the list')
+ end
+
+ it 'allows dropdown, color_swatch, and buttons' do
+ %w[dropdown color_swatch buttons].each do |kind|
+ option_type = build(:option_type, kind: kind)
+ expect(option_type).to be_valid
+ end
+ end
+
+ it 'validates presence' do
+ option_type = build(:option_type, kind: '')
+ expect(option_type).not_to be_valid
+ end
+ end
+
+ describe '#color_swatch?' do
+ it 'returns true when kind is color_swatch' do
+ option_type = build(:option_type, kind: 'color_swatch')
+ expect(option_type.color_swatch?).to be true
+ end
+
+ it 'returns false when kind is dropdown' do
+ option_type = build(:option_type, kind: 'dropdown')
+ expect(option_type.color_swatch?).to be false
+ end
+ end
+
+ describe '.color_swatches' do
+ let!(:color_type) { create(:option_type, :color_swatch) }
+ let!(:size_type) { create(:option_type, :size) }
+
+ it 'returns only color_swatch option types' do
+ expect(described_class.color_swatches).to include(color_type)
+ expect(described_class.color_swatches).not_to include(size_type)
+ end
+ end
+
describe 'color methods' do
- let!(:option_type) { create(:option_type, name: 'Color') }
+ let!(:option_type) { create(:option_type, name: 'Color', kind: 'color_swatch') }
describe '.color' do
it 'should return the first option type with name "color"' do
@@ -80,13 +127,9 @@ describe Spree::OptionType, type: :model do
end
describe '#color?' do
- it 'should return true if the name is "color" or "colour"' do
- expect(option_type.color?).to be_truthy
- end
-
- it 'should return false if the name is not "color" or "colour"' do
- option_type.update(name: 'Size')
- expect(option_type.color?).to be_falsy
+ it 'is deprecated and delegates to color_swatch?' do
+ expect(Spree::Deprecation).to receive(:warn).with(/deprecated/)
+ expect(option_type.color?).to be true
end
end
end
diff --git a/spree/core/spec/models/spree/option_value_spec.rb b/spree/core/spec/models/spree/option_value_spec.rb
index d428f153d7..423bdb37a2 100644
--- a/spree/core/spec/models/spree/option_value_spec.rb
+++ b/spree/core/spec/models/spree/option_value_spec.rb
@@ -63,6 +63,41 @@ describe Spree::OptionValue, type: :model do
end
end
+ describe 'color_code validation' do
+ it 'accepts valid 6-digit hex color' do
+ option_value = build(:option_value, color_code: '#FF0000')
+ expect(option_value).to be_valid
+ end
+
+ it 'accepts valid 8-digit hex color (with alpha)' do
+ option_value = build(:option_value, color_code: '#FF0000AA')
+ expect(option_value).to be_valid
+ end
+
+ it 'accepts lowercase hex' do
+ option_value = build(:option_value, color_code: '#aabbcc')
+ expect(option_value).to be_valid
+ end
+
+ it 'accepts nil color_code' do
+ option_value = build(:option_value, color_code: nil)
+ expect(option_value).to be_valid
+ end
+
+ it 'accepts blank color_code' do
+ option_value = build(:option_value, color_code: '')
+ expect(option_value).to be_valid
+ end
+
+ it 'rejects invalid hex colors' do
+ %w[FF0000 #FFF #GGGGGG red #FF00].each do |invalid|
+ option_value = build(:option_value, color_code: invalid)
+ expect(option_value).not_to be_valid, "Expected #{invalid.inspect} to be invalid"
+ expect(option_value.errors[:color_code]).to be_present
+ end
+ end
+ end
+
describe 'translations' do
let!(:option_value) { create(:option_value, name: 'red', presentation: 'Red') }