diff --git a/app/helpers/spree/base_helper_decorator.rb b/app/helpers/spree/base_helper.rb similarity index 73% rename from app/helpers/spree/base_helper_decorator.rb rename to app/helpers/spree/base_helper.rb index 4b38cb7bb5..11320424b4 100644 --- a/app/helpers/spree/base_helper_decorator.rb +++ b/app/helpers/spree/base_helper.rb @@ -1,12 +1,7 @@ +# frozen_string_literal: true + module Spree module BaseHelper - # human readable list of variant options - # Override: Do not show out of stock text - def variant_options(variant, _options = {}) - variant.options_text - end - - # Overriden to eager-load :states def available_countries checkout_zone = Zone.find_by(name: Spree::Config[:checkout_zone]) @@ -21,5 +16,10 @@ module Spree country end.sort { |a, b| a.name <=> b.name } end + + def pretty_time(time) + [I18n.l(time.to_date, format: :long), + time.strftime("%l:%M %p")].join(" ") + end end end diff --git a/app/models/spree/log_entry.rb b/app/models/spree/log_entry.rb new file mode 100644 index 0000000000..85b65fe8e7 --- /dev/null +++ b/app/models/spree/log_entry.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Spree + class LogEntry < ActiveRecord::Base + belongs_to :source, polymorphic: true + + # Fix for Spree #1767 + # If a payment fails, we want to make sure we keep the record of it failing + after_rollback :save_anyway + + def save_anyway + log = Spree::LogEntry.new + log.source = source + log.details = details + log.save! + end + end +end diff --git a/spec/helpers/spree/base_helper_spec.rb b/spec/helpers/spree/base_helper_spec.rb new file mode 100644 index 0000000000..22f716a3e4 --- /dev/null +++ b/spec/helpers/spree/base_helper_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Spree::BaseHelper do + include Spree::BaseHelper + + context "available_countries" do + let(:country) { create(:country) } + + before do + 3.times { create(:country) } + end + + context "with no checkout zone defined" do + before do + Spree::Config[:checkout_zone] = nil + end + + it "return complete list of countries" do + expect(available_countries.count).to eq Spree::Country.count + end + end + + context "with a checkout zone defined" do + context "checkout zone is of type country" do + before do + @country_zone = create(:zone, name: "CountryZone") + @country_zone.members.create(zoneable: country) + Spree::Config[:checkout_zone] = @country_zone.name + end + + it "return only the countries defined by the checkout zone" do + expect(available_countries).to eq [country] + end + end + + context "checkout zone is of type state" do + before do + state_zone = create(:zone, name: "StateZone") + state = create(:state, country: country) + state_zone.members.create(zoneable: state) + Spree::Config[:checkout_zone] = state_zone.name + end + + it "return complete list of countries" do + expect(available_countries.count).to eq Spree::Country.count + end + end + end + end + + context "pretty_time" do + it "prints in a format" do + expect(pretty_time(DateTime.new(2012, 5, 6, 13, 33))).to eq "May 06, 2012 1:33 PM" + end + end +end