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
open sourced more admin helpers
Damian Legawiec787707060e1e
diff --git a/admin/app/helpers/spree/admin/base_helper.rb b/admin/app/helpers/spree/admin/base_helper.rbindex e02879b5dc..8e81947a65 100644--- a/admin/app/helpers/spree/admin/base_helper.rb+++ b/admin/app/helpers/spree/admin/base_helper.rb@@ -200,10 +200,6 @@ module Spree)end- def current_currency_money- @current_currency_money ||= ::Money::Currency.find(current_currency)- end-def path_from_url(url)url.to_s.gsub('https://', '').gsub('http://', '')enddiff --git a/admin/app/helpers/spree/admin/currencies_helper.rb b/admin/app/helpers/spree/admin/currencies_helper.rbnew file mode 100644index 0000000000..0bcd43ce08--- /dev/null+++ b/admin/app/helpers/spree/admin/currencies_helper.rb@@ -0,0 +1,17 @@+module Spree+ module Admin+ module CurrenciesHelper+ def preferred_currencies+ @preferred_currencies ||= ([current_store.default_currency] + current_store.supported_currencies_list).uniq+ end++ def currency_money(currency = current_currency)+ ::Money::Currency.find(currency)+ end++ def currency_symbol(currency = current_currency)+ currency_money(currency).symbol+ end+ end+ end+enddiff --git a/admin/app/helpers/spree/admin/flash_helper.rb b/admin/app/helpers/spree/admin/flash_helper.rbnew file mode 100644index 0000000000..31efec0e3e--- /dev/null+++ b/admin/app/helpers/spree/admin/flash_helper.rb@@ -0,0 +1,15 @@+module Spree+ module Admin+ module FlashHelper+ def admin_flash_class_for(flash_type)+ {+ success: 'alert-success',+ error: 'alert-danger',+ warning: 'alert-warning',+ notice: 'alert-success',+ alert: 'alert-danger'+ }[flash_type.to_sym]+ end+ end+ end+enddiff --git a/admin/app/helpers/spree/admin/navigation_helper.rb b/admin/app/helpers/spree/admin/navigation_helper.rbindex ec55c51010..ca1d867649 100644--- a/admin/app/helpers/spree/admin/navigation_helper.rb+++ b/admin/app/helpers/spree/admin/navigation_helper.rb@@ -196,6 +196,21 @@ module Spreelink_to label, url, role: 'tab', 'aria-controls': 'pills-general', class: link_class, 'aria-selected': activeendend++ def external_link_to(label, url, opts = {})+ opts[:target] ||= :blank+ opts[:rel] ||= :nofollow+ opts[:class] = "d-inline-flex align-items-center #{opts[:class]}"++ link_to url, opts do+ (label + icon('external-link', class: 'ml-1 mr-0 small')).html_safe+ end+ end++ def help_bubble(text = '', placement = 'bottom', css: nil)+ css ||= 'text-muted font-size-base'+ content_tag :small, icon('help', class: css), data: { placement: placement }, class: "with-tip #{css}", title: text+ endendendenddiff --git a/admin/app/helpers/spree/admin/payments_helper.rb b/admin/app/helpers/spree/admin/payments_helper.rbnew file mode 100644index 0000000000..60bb9e4ec9--- /dev/null+++ b/admin/app/helpers/spree/admin/payments_helper.rb@@ -0,0 +1,31 @@+module Spree+ module Admin+ module PaymentsHelper+ def payment_method_name(payment)+ payment_method = payment.payment_method++ if can?(:update, payment_method)+ link_to payment_method.name, spree.edit_admin_payment_method_path(payment_method)+ else+ payment_method.name+ end+ end++ def payment_method_icon_tag(payment_method, opts = {})+ image_tag "payment_icons/#{payment_method}.svg", opts+ rescue Sprockets::Rails::Helper::AssetNotFound+ end++ def payment_source_name(payment)+ return if payment.source.blank?++ source_class = payment.source.class+ if source_class.respond_to?(:display_name)+ source_class.display_name+ else+ source_class.name.demodulize.split(/(?=[A-Z])/).join(' ')+ end+ end+ end+ end+enddiff --git a/admin/app/helpers/spree/admin/sortable_tree_helper.rb b/admin/app/helpers/spree/admin/sortable_tree_helper.rbnew file mode 100644index 0000000000..3f2e77c19c--- /dev/null+++ b/admin/app/helpers/spree/admin/sortable_tree_helper.rb@@ -0,0 +1,33 @@+module Spree+ module Admin+ module SortableTreeHelper+ def sortable_tree_bar(parent_resource, child_resource)+ partial_name = parent_resource.class.name.demodulize.underscore+ render "spree/admin/shared/sortable_tree/#{partial_name}", parent_resource: parent_resource, child_resource: child_resource+ end++ def build_sortable_tree(parent_resource, child_resource)+ child_resource_name = child_resource.class.name.demodulize.underscore++ descendants = []++ unless child_resource.leaf?+ child_resource.children.includes(image_attachment: :blob, square_image_attachment: :blob).each do |child_item|+ descendants << build_sortable_tree(parent_resource, child_item) unless child_resource.leaf?+ end+ end++ row = sortable_tree_bar(parent_resource, child_resource)+ container = content_tag(:div, raw(descendants.join), data: { sortable_tree_parent_id_value: child_resource.id })++ content_tag(:div, row + container,+ id: spree_dom_id(child_resource),+ class: 'sortable-tree-item draggable',+ data: {+ sortable_tree_resource_name_value: child_resource_name.singularize,+ sortable_tree_update_url_value: spree.reposition_admin_taxonomy_taxon_path(child_resource.taxonomy_id, child_resource.id)+ })+ end+ end+ end+enddiff --git a/admin/app/helpers/spree/admin/turbo_helper.rb b/admin/app/helpers/spree/admin/turbo_helper.rbnew file mode 100644index 0000000000..791f615905--- /dev/null+++ b/admin/app/helpers/spree/admin/turbo_helper.rb@@ -0,0 +1,41 @@+module Spree+ module Admin+ module TurboHelper+ def turbo_close_modal(modal_id = nil)+ modal_id ||= 'modal'++ turbo_stream.replace :modal_scripts do+ turbo_frame_tag :modal_scripts do+ javascript_tag do+ raw <<~JS+ if (document.querySelector('##{modal_id}')) {+ window.bootstrapJQuery("##{modal_id}").modal('hide');+ }+ JS+ end+ end+ end+ end++ def turbo_render_alerts+ turbo_stream.replace :alerts do+ render 'spree/admin/shared/alerts'+ end+ end++ def turbo_save_button_tag(label = Spree.t('actions.save'), opts = {}, &block)+ opts[:class] ||= 'btn btn-primary text-center'+ opts[:class] << ' d-flex align-items-center justify-content-center' if opts[:class].exclude?('d-block') && opts[:class].exclude?('d-flex')++ opts['data-turbo-submits-with'] ||= "#{content_tag(:span, '', class: 'spinner-border spinner-border-sm mr-2', role: 'status')}Saving..."+ opts['data-enable-button-target'] = 'button'++ if block_given?+ button_tag opts, &block+ else+ button_tag label, opts+ end+ end+ end+ end+enddiff --git a/admin/app/helpers/spree/admin/users_helper.rb b/admin/app/helpers/spree/admin/users_helper.rbnew file mode 100644index 0000000000..9a44a69b0f--- /dev/null+++ b/admin/app/helpers/spree/admin/users_helper.rb@@ -0,0 +1,33 @@+module Spree+ module Admin+ module UsersHelper+ def customer_location(user)+ address = user.billing_address || user.shipping_address || user.addresses.first++ return if address.nil?++ "#{address.city}, #{address&.state_name_text || address&.country&.to_s}"+ end++ def customer_location_flag(user)+ address = user.billing_address || user.shipping_address || user.addresses.first++ return if address.nil?++ country = address.country++ return if country.nil?++ ::Country.new(country.iso).emoji_flag+ end++ def customer_full_name(user)+ user.name&.full+ end++ def users_for_select_options+ @users_for_select_options ||= Spree.user_class.accessible_by(current_ability).pluck(:id, :email).map { |id, email| { id: id, name: email } }.as_json+ end+ end+ end+enddiff --git a/admin/spec/helpers/spree/admin/payment_helper_spec.rb b/admin/spec/helpers/spree/admin/payment_helper_spec.rbnew file mode 100644index 0000000000..1860556c1b--- /dev/null+++ b/admin/spec/helpers/spree/admin/payment_helper_spec.rb@@ -0,0 +1,34 @@+require 'spec_helper'++describe Spree::Admin::PaymentsHelper, type: :helper do+ let!(:store) { create(:store) }++ before do+ allow(helper).to receive(:current_store).and_return(store)+ end++ describe '#payment_source_name' do+ let(:payment_method) { create(:credit_card_payment_method, stores: [store]) }+ let(:payment) { build(:payment, payment_method: payment_method) }++ before do+ allow(payment).to receive(:source).and_return(source)+ end++ context 'for source with display_name' do+ let(:source) { double('Payment Source', class: double('Payment Source Class', display_name: 'Display 554')) }++ it 'returns the display name of the source class' do+ expect(helper.payment_source_name(payment)).to eq('Display 554')+ end+ end++ context 'for source without display_name' do+ let(:source) { double('Payment Source', class: double('Payment Source Class', name: 'MyPaymentMethod')) }++ it 'returns the display name of the source class' do+ expect(helper.payment_source_name(payment)).to eq('My Payment Method')+ end+ end+ end+enddiff --git a/admin/spec/spec_helper.rb b/admin/spec/spec_helper.rbnew file mode 100644index 0000000000..cf30bb8800--- /dev/null+++ b/admin/spec/spec_helper.rb@@ -0,0 +1,123 @@+if ENV['COVERAGE']+ # Run Coverage report+ require 'simplecov'+ SimpleCov.start 'rails' do+ add_group 'Libraries', 'lib/spree'++ add_filter '/bin/'+ add_filter '/db/'+ add_filter '/script/'+ add_filter '/spec/'+ add_filter '/lib/generators/'++ coverage_dir "#{ENV['COVERAGE_DIR']}/admin" if ENV['COVERAGE_DIR']+ end+end++# This file is copied to ~/spec when you run 'ruby script/generate rspec'+# from the project root directory.+ENV['RAILS_ENV'] ||= 'test'+++begin+ require File.expand_path('../dummy/config/environment', __FILE__)+rescue LoadError+ puts 'Could not load dummy application. Please ensure you have run `bundle exec rake test_app`'+ exit+end++require 'rspec/rails'++# Requires supporting files with custom matchers and macros, etc,+# in ./support/ and its subdirectories.+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }++require 'database_cleaner/active_record'+require 'ffaker'++require 'spree/testing_support/authorization_helpers'+require 'spree/testing_support/factories'+require 'spree/testing_support/preferences'+require 'spree/testing_support/controller_requests'+require 'spree/testing_support/url_helpers'+require 'spree/testing_support/order_walkthrough'+require 'spree/admin/testing_support/capybara_utils'+require 'spree/testing_support/capybara_config'+require 'spree/testing_support/rspec_retry_config'+require 'spree/testing_support/image_helpers'++require 'spree/core/controller_helpers/strong_parameters'++RSpec.configure do |config|+ config.color = true+ config.default_formatter = 'doc'+ config.fail_fast = ENV['FAIL_FAST'] || false+ config.infer_spec_type_from_file_location!+ config.mock_with :rspec+ config.raise_errors_for_deprecations!++ # If you're not using ActiveRecord, or you'd prefer not to run each of your+ # examples within a transaction, comment the following line or assign false+ # instead of true.+ config.use_transactional_fixtures = false++ config.before :suite do+ Capybara.match = :smart+ Capybara.javascript_driver = :selenium_chrome+ Capybara.default_max_wait_time = 10+ Capybara.raise_server_errors = false+ DatabaseCleaner.clean_with :truncation+ # Force jobs to be executed in a synchronous way+ ActiveJob::Base.queue_adapter = :inline+ end++ config.before do+ Rails.cache.clear+ WebMock.disable!+ DatabaseCleaner.strategy = if RSpec.current_example.metadata[:js]+ :truncation+ else+ :transaction+ end+ # TODO: Find out why open_transactions ever gets below 0+ # See issue #3428+ ApplicationRecord.connection.increment_open_transactions if ApplicationRecord.connection.open_transactions < 0++ DatabaseCleaner.start+ reset_spree_preferences++ country = create(:country, name: 'United States of America', iso_name: 'UNITED STATES', iso: 'US', states_required: true)++ create(:store, default_country: country, default: true)+ end++ config.after(:each, type: :feature) do |example|+ if page&.body&.present?+ missing_translations = page.body.scan(/translation missing: #{I18n.locale}\.(.*?)[\s<\"&]/)+ if missing_translations.any?+ puts "Found missing translations: #{missing_translations.inspect}"+ puts "In spec: #{example.location}"+ end+ end+ end++ config.append_after do+ DatabaseCleaner.clean+ end++ config.include FactoryBot::Syntax::Methods++ config.include Spree::Admin::TestingSupport::CapybaraUtils+ config.include Spree::TestingSupport::Preferences+ config.include Spree::TestingSupport::UrlHelpers+ config.include Spree::TestingSupport::ControllerRequests, type: :controller+ config.include Spree::TestingSupport::ImageHelpers++ config.include Spree::Core::ControllerHelpers::StrongParameters, type: :controller++ config.order = :random+ Kernel.srand config.seed++ config.filter_run_including focus: true unless ENV['CI']+ config.run_all_when_everything_filtered = true+end