Skip to content

Commit

Permalink
Merge pull request #2419 from tvdeyen/prettier-time
Browse files Browse the repository at this point in the history
Use I18n date format for `pretty_time` helper
  • Loading branch information
tvdeyen authored Dec 12, 2017
2 parents 5bf2735 + 40c7ee5 commit b807318
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
5 changes: 2 additions & 3 deletions core/app/helpers/spree/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +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.to_date, format: :long),
time.strftime("%l:%M %p")].join(" ")
def pretty_time(time, format = :long)
I18n.l(time, format: :"solidus.#{format}")
end

def link_to_tracking(shipment, options = {})
Expand Down
6 changes: 5 additions & 1 deletion core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,11 @@ 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'
short: "%b %-d '%y %-l:%M%P"
devise:
confirmations:
confirmed: Your account was successfully confirmed. You are now signed in.
Expand Down
18 changes: 15 additions & 3 deletions core/spec/helpers/base_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,21 @@ 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

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

Expand Down

0 comments on commit b807318

Please sign in to comment.