|
6 | 6 | let(:formats) do |
7 | 7 | { |
8 | 8 | date: { |
9 | | - formats: {default: "%m/%d/%Y", short: "%b %d"}, |
| 9 | + formats: { |
| 10 | + default: "%m/%d/%Y", |
| 11 | + short: "%b %d", |
| 12 | + administrate_date_default: "%m/%d, %Y" |
| 13 | + }, |
10 | 14 | abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 }, |
11 | 15 | abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 } |
12 | 16 | }, |
13 | 17 | time: { |
14 | | - formats: {default: "%a, %b %-d, %Y at %r", short: "%d %b %H:%M"} |
| 18 | + formats: { |
| 19 | + default: "%a, %b %-d, %Y at %r", |
| 20 | + short: "%d %b %H:%M", |
| 21 | + administrate_datetime_default: "%a, %b %-d, %Y, %r", |
| 22 | + administrate_time_default: "%I:%M%p" |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + end |
| 27 | + let(:formats_without_administrate_default) do |
| 28 | + { |
| 29 | + date: { |
| 30 | + formats: { |
| 31 | + default: "%m/%d/%Y", |
| 32 | + short: "%b %d" |
| 33 | + }, |
| 34 | + abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 }, |
| 35 | + abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 } |
| 36 | + }, |
| 37 | + time: { |
| 38 | + formats: { |
| 39 | + default: "%a, %b %-d, %Y at %r", |
| 40 | + short: "%d %b %H:%M" |
| 41 | + } |
15 | 42 | } |
16 | 43 | } |
17 | 44 | end |
18 | 45 |
|
19 | 46 | describe "#date" do |
20 | | - it "displays the date" do |
21 | | - with_translations(:en, formats) do |
22 | | - field = Administrate::Field::DateTime |
23 | | - .new(:start_date, start_date, :show) |
24 | | - expect(field.date).to eq("12/25/2015") |
| 47 | + context "without `format` option" do |
| 48 | + it "displays the date in the administrate default format" do |
| 49 | + field = Administrate::Field::DateTime.new(:start_date, start_date, :show) |
| 50 | + with_translations(:en, formats) do |
| 51 | + expect(field.date).to eq("12/25, 2015") |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + it "falls back to default format if administrate_date_default is missing" do |
| 56 | + field = Administrate::Field::DateTime.new(:start_date, start_date, :show) |
| 57 | + with_translations(:en, formats_without_administrate_default) do |
| 58 | + expect(field.date).to eq("12/25/2015") |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + it "falls back to static format (%Y-%m-%d) if locale formats are missing" do |
| 63 | + field = Administrate::Field::DateTime.new(:start_date, start_date, :show) |
| 64 | + |
| 65 | + with_translations(:unformatted_locale, {}) do |
| 66 | + available_locales = I18n.available_locales |
| 67 | + locale = I18n.locale |
| 68 | + begin |
| 69 | + I18n.available_locales = [:unformatted_locale] |
| 70 | + I18n.locale = :unformatted_locale |
| 71 | + expect(field.date).to eq("2015-12-25") |
| 72 | + ensure |
| 73 | + I18n.available_locales = available_locales |
| 74 | + I18n.locale = locale |
| 75 | + end |
| 76 | + end |
25 | 77 | end |
26 | 78 | end |
27 | 79 |
|
28 | | - context "with `prefix` option" do |
| 80 | + context "with `format` option" do |
29 | 81 | it "displays the date in the requested format" do |
30 | 82 | options_field = Administrate::Field::DateTime |
31 | 83 | .with_options(format: :short) |
|
90 | 142 | end |
91 | 143 |
|
92 | 144 | describe "#datetime" do |
93 | | - it "displays the datetime" do |
94 | | - field = Administrate::Field::DateTime.new(:start_date, start_date, :show) |
| 145 | + context "without `format` option" do |
| 146 | + it "displays the datetime in the administrate default format" do |
| 147 | + field = Administrate::Field::DateTime.new(:start_date, start_date, :show) |
| 148 | + with_translations(:en, formats) do |
| 149 | + expect(field.datetime).to eq("Fri, Dec 25, 2015, 10:15:45 AM") |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + it "falls back to default format if administrate_datetime_default is missing" do |
| 154 | + field = Administrate::Field::DateTime.new(:start_date, start_date, :show) |
| 155 | + with_translations(:en, formats_without_administrate_default) do |
| 156 | + expect(field.datetime).to eq("Fri, Dec 25, 2015 at 10:15:45 AM") |
| 157 | + end |
| 158 | + end |
95 | 159 |
|
96 | | - with_translations(:en, formats) do |
97 | | - expect(field.datetime).to eq("Fri, Dec 25, 2015 at 10:15:45 AM") |
| 160 | + it "falls back to static format (%Y-%m-%d %H:%M) if locale formats are missing" do |
| 161 | + field = Administrate::Field::DateTime.new(:start_date, start_date, :show) |
| 162 | + |
| 163 | + with_translations(:unformatted_locale, {}) do |
| 164 | + available_locales = I18n.available_locales |
| 165 | + locale = I18n.locale |
| 166 | + begin |
| 167 | + I18n.available_locales = [:unformatted_locale] |
| 168 | + I18n.locale = :unformatted_locale |
| 169 | + expect(field.datetime).to eq("2015-12-25 10:15") |
| 170 | + ensure |
| 171 | + I18n.available_locales = available_locales |
| 172 | + I18n.locale = locale |
| 173 | + end |
| 174 | + end |
98 | 175 | end |
99 | 176 | end |
100 | 177 |
|
101 | | - context "with `prefix` option" do |
| 178 | + context "with `format` option" do |
102 | 179 | it "displays the datetime in the requested format" do |
103 | 180 | options_field = Administrate::Field::DateTime |
104 | 181 | .with_options(format: :short) |
|
0 commit comments