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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Improve code for drop-down methods.
Matthew Kennedyf5b37538a175
diff --git a/.solargraph.yml b/.solargraph.ymlindex fa2d038b07..7083c64553 100644--- a/.solargraph.yml+++ b/.solargraph.yml@@ -8,7 +8,7 @@ exclude:- vendor/**/*- ".bundle/**/*"- sample/**/*-- cmd/lib/spree_cmd/templates/extension+- cmd/**/*- guides/**/*- sandbox/**/*- pkg/**/*diff --git a/backend/app/helpers/spree/admin/base_helper.rb b/backend/app/helpers/spree/admin/base_helper.rbindex eb86460ca4..421f8f999a 100644--- a/backend/app/helpers/spree/admin/base_helper.rb+++ b/backend/app/helpers/spree/admin/base_helper.rb@@ -31,30 +31,55 @@ module Spree)end- def spree_humanize_dropdown_values(object, options = {})- formatted_options = []+ # Returns Humanized Dropdown Values From a Constant In the Model+ # Pass the model name as a string and then a hash containing the constant name+ # and the option to paramatize the return value.+ #+ # Example:+ #+ # spree_humanize_dropdown_values('Spree::CmsPage', { const: 'TYPES' })+ # spree_humanize_dropdown_values('Spree::Menu', { const: 'MENU_TYPES', paramterize_values: true })+ #+ def spree_humanize_dropdown_values(model_name, options = {})+ formatted_option_values = []const = options[:const] || nil+ paramterize_values = options[:paramterize_values] || false++ return unless const.present?++ model_name.constantize.const_get(const).map do |type|+ return_value = if paramterize_values+ type.parameterize(separator: '_')+ else+ type+ end++ formatted_option_values << [spree_humanize_type(type), return_value]+ end++ formatted_option_values+ end++ # Returns Custom Dropdown Values+ # Pass the object as the first argument, and then a hash of options as a second arguments.+ # text: returns the visable text in the dropdown option, and value: defaults to id but can be overidden.+ # Example:+ #+ # spree_custom_dropdown_values(@store, { text: 'unique_name' })+ # spree_custom_dropdown_values(@store, { text: 'unique_name', value: 'name' })+ #+ def spree_custom_dropdown_values(object, options = {})+ formatted_option_values = []+text = options[:text] || nilvalue = options[:value] || 'id'- paramterize_value = options[:paramterize_value] || false-- if object.is_a? String- if paramterize_value- object.constantize.const_get(const).- map { |type| formatted_options << [spree_humanize_type(type), type.parameterize(separator: '_')] }- else- object.constantize.const_get(const).- map { |type| formatted_options << [spree_humanize_type(type), type] }- end- elsif text.present? && const.nil?- object.map { |obj| formatted_options << [obj.send(text), obj.send(value)] }- elsif text.nil? && const.present?- object.class.const_get(const).- map { |type| formatted_options << [spree_humanize_type(type), type] }- end- formatted_options+ return unless text.present?++ object.map { |obj| formatted_option_values << [obj.send(text), obj.send(value)] }++ formatted_option_valuesenddef spree_humanize_type(obj)diff --git a/backend/app/views/spree/admin/cms_pages/_form.html.erb b/backend/app/views/spree/admin/cms_pages/_form.html.erbindex 861678d9a3..50d99909ae 100644--- a/backend/app/views/spree/admin/cms_pages/_form.html.erb+++ b/backend/app/views/spree/admin/cms_pages/_form.html.erb@@ -10,7 +10,7 @@<div class="row"><%= f.field_container :type, class: ['form-group col-12 col-md-6'] do %><%= f.label :type, Spree.t('admin.cms.page_type') %>- <%= f.select(:type, spree_humanize_dropdown_values(@cms_page, {const: 'TYPES'}), {include_blank: false}, class: 'select2') %>+ <%= f.select(:type, spree_humanize_dropdown_values('Spree::CmsPage', {const: 'TYPES'}), {include_blank: false}, class: 'select2') %><%= f.error_message_on :type %><% end %>diff --git a/backend/app/views/spree/admin/cms_sections/_form.html.erb b/backend/app/views/spree/admin/cms_sections/_form.html.erbindex e470498ccf..b5b1145731 100644--- a/backend/app/views/spree/admin/cms_sections/_form.html.erb+++ b/backend/app/views/spree/admin/cms_sections/_form.html.erb@@ -17,7 +17,7 @@<div class="col-12 col-md-6"><%= f.field_container :type, class: ['form-group'] do %><%= f.label :type, Spree.t('admin.navigation.section_type') %>- <%= f.select(:type, spree_humanize_dropdown_values(@cms_section, {const: 'TYPES'}), {include_blank: false}) %>+ <%= f.select(:type, spree_humanize_dropdown_values('Spree::CmsSection', {const: 'TYPES'}), {include_blank: false}) %><%= f.error_message_on :type %><% end %></div>diff --git a/backend/app/views/spree/admin/menu_items/_form.html.erb b/backend/app/views/spree/admin/menu_items/_form.html.erbindex be91a06866..e505ebb90a 100644--- a/backend/app/views/spree/admin/menu_items/_form.html.erb+++ b/backend/app/views/spree/admin/menu_items/_form.html.erb@@ -95,7 +95,7 @@<div class="col-12 col-md-6"><%= f.field_container :linked_resource_type, class: ['form-group'] do %><%= f.label :linked_resource_type, Spree.t('admin.navigation.link_to') %>- <%= f.select(:linked_resource_type, spree_humanize_dropdown_values(@menu_item, {const: 'LINKED_RESOURCE_TYPE'}), {include_blank: false}, class: 'link_switcher') %>+ <%= f.select(:linked_resource_type, spree_humanize_dropdown_values('Spree::MenuItem', {const: 'LINKED_RESOURCE_TYPE'}), {include_blank: false}, class: 'link_switcher') %><%= f.error_message_on :linked_resource_type %><% end %></div>diff --git a/backend/app/views/spree/admin/menus/_form.html.erb b/backend/app/views/spree/admin/menus/_form.html.erbindex 7c3ac898d2..5f3ea5bfda 100644--- a/backend/app/views/spree/admin/menus/_form.html.erb+++ b/backend/app/views/spree/admin/menus/_form.html.erb@@ -8,7 +8,7 @@<%= f.field_container :location, class: ['form-group'] do %><%= f.label :location, Spree.t('location') %>- <%= f.select(:location, spree_humanize_dropdown_values(@menu, { const: 'MENU_LOCATIONS'}), {include_blank: false}, class: 'select2') %>+ <%= f.select(:location, spree_humanize_dropdown_values('Spree::Menu', { const: 'MENU_LOCATIONS'}), {include_blank: false}, class: 'select2') %><%= f.error_message_on :location %><% end %></div>diff --git a/backend/app/views/spree/admin/menus/index.html.erb b/backend/app/views/spree/admin/menus/index.html.erbindex f3ef194efb..5bae51108a 100644--- a/backend/app/views/spree/admin/menus/index.html.erb+++ b/backend/app/views/spree/admin/menus/index.html.erb@@ -20,7 +20,7 @@<div class="col-12 col-lg-6"><div class="form-group"><%= label_tag :q_location_eq, Spree.t(:location) %>- <%= f.select :location_eq, spree_humanize_dropdown_values('Spree::Menu', {const: 'MENU_LOCATIONS', paramterize_value: true}), { include_blank: true }, class: 'select2-clear js-filterable' %>+ <%= f.select :location_eq, spree_humanize_dropdown_values('Spree::Menu', { const: 'MENU_LOCATIONS', paramterize_values: true }), { include_blank: true }, class: 'select2-clear js-filterable' %></div></div></div>@@ -35,7 +35,7 @@<div class="col-12 col-lg-6"><div class="form-group"><%= label_tag :q_store_id_in, Spree.t(:store) %>- <%= f.select :store_id_in, spree_humanize_dropdown_values(@stores, {text: 'unique_name'}), { include_blank: true }, class: 'select2-clear js-filterable' %>+ <%= f.select :store_id_in, spree_custom_dropdown_values(@stores, { text: 'unique_name' }), { include_blank: true }, class: 'select2-clear js-filterable' %></div></div></div>