Skip to content

Commit

Permalink
Add support for other pretty_time formats
Browse files Browse the repository at this point in the history
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)`.
  • Loading branch information
tvdeyen committed Dec 1, 2017
1 parent 678812c commit 40c7ee5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/app/helpers/spree/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down
4 changes: 3 additions & 1 deletion core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions core/spec/helpers/base_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 40c7ee5

Please sign in to comment.