From 678812cc8ac31ca8fe1cd57664defeab173c5dcf Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 30 Nov 2017 22:43:26 +0100 Subject: [PATCH 1/2] Use a date format for pretty_time helper Instead of concatinating strings we should use a i18n date format instead. --- core/app/helpers/spree/base_helper.rb | 3 +-- core/config/locales/en.yml | 4 +++- core/spec/helpers/base_helper_spec.rb | 10 +++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb index a22012dd3a2..e5a670da11d 100644 --- a/core/app/helpers/spree/base_helper.rb +++ b/core/app/helpers/spree/base_helper.rb @@ -128,8 +128,7 @@ def display_price(product_or_variant) end def pretty_time(time) - [I18n.l(time.to_date, format: :long), - time.strftime("%l:%M %p")].join(" ") + I18n.l(time, format: :solidus_long) end def link_to_tracking(shipment, options = {}) diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml index 6d90434bb60..c85d2d6d563 100644 --- a/core/config/locales/en.yml +++ b/core/config/locales/en.yml @@ -699,7 +699,9 @@ en: attributes: payment_source: has_to_be_payment_source_class: "has to be a Spree::PaymentSource" - + time: + formats: + solidus_long: '%B %d, %Y %l:%M %p' devise: confirmations: confirmed: Your account was successfully confirmed. You are now signed in. diff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rb index 3db119fbe84..a2095f36e53 100644 --- a/core/spec/helpers/base_helper_spec.rb +++ b/core/spec/helpers/base_helper_spec.rb @@ -122,9 +122,13 @@ def link_to_tracking_html(options = {}) 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" + describe "#pretty_time" do + subject { pretty_time(date) } + + let(:date) { Time.new(2012, 11, 6, 13, 33) } + + it "pretty prints time in long format" do + is_expected.to eq "November 06, 2012 1:33 PM" end end From 40c7ee56105e4f75a574ed99998e12df89857923 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 30 Nov 2017 23:10:14 +0100 Subject: [PATCH 2/2] Add support for other pretty_time formats Instead of only using a long version we add a short version too. You can add your own solidus time format by adding a format to your-locale: time: formats: solidus: iso: '%F' and use it with `pretty_time(record.created_at, :iso)`. --- core/app/helpers/spree/base_helper.rb | 4 ++-- core/config/locales/en.yml | 4 +++- core/spec/helpers/base_helper_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb index e5a670da11d..41777435c7e 100644 --- a/core/app/helpers/spree/base_helper.rb +++ b/core/app/helpers/spree/base_helper.rb @@ -127,8 +127,8 @@ def display_price(product_or_variant) product_or_variant.price_for(current_pricing_options).to_html end - def pretty_time(time) - I18n.l(time, format: :solidus_long) + def pretty_time(time, format = :long) + I18n.l(time, format: :"solidus.#{format}") end def link_to_tracking(shipment, options = {}) diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml index c85d2d6d563..60c9c3c9481 100644 --- a/core/config/locales/en.yml +++ b/core/config/locales/en.yml @@ -701,7 +701,9 @@ en: has_to_be_payment_source_class: "has to be a Spree::PaymentSource" time: formats: - solidus_long: '%B %d, %Y %l:%M %p' + solidus: + long: '%B %d, %Y %-l:%M %p' + short: "%b %-d '%y %-l:%M%P" devise: confirmations: confirmed: Your account was successfully confirmed. You are now signed in. diff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rb index a2095f36e53..0a9e1608669 100644 --- a/core/spec/helpers/base_helper_spec.rb +++ b/core/spec/helpers/base_helper_spec.rb @@ -130,6 +130,14 @@ def link_to_tracking_html(options = {}) it "pretty prints time in long format" do is_expected.to eq "November 06, 2012 1:33 PM" end + + context 'with format set to short' do + subject { pretty_time(date, :short) } + + it "pretty prints time in short format" do + is_expected.to eq "Nov 6 '12 1:33pm" + end + end end context "plural_resource_name" do