Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a Date Field, where the datepicker lets the admin user select just a date without time. ![Screen Shot 2020-01-16 at 12 49 51 PM](https://user-images.githubusercontent.com/20058206/72539880-c5830200-385e-11ea-922a-58f1d8038373.png)
- Loading branch information
1 parent
19929bd
commit d153bcf
Showing
11 changed files
with
153 additions
and
6 deletions.
There are no files selected for viewing
8 changes: 6 additions & 2 deletions
8
app/assets/javascripts/administrate/components/date_time_picker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
$(function () { | ||
$('[data-type="time"]').datetimepicker({ | ||
debug: false, | ||
format: "HH:mm:ss", | ||
format: 'HH:mm:ss', | ||
}); | ||
$('[data-type="datetime"]').datetimepicker({ | ||
debug: false, | ||
format: "YYYY-MM-DD HH:mm:ss", | ||
format: 'YYYY-MM-DD HH:mm:ss', | ||
}); | ||
$('[data-type="date"]').datetimepicker({ | ||
debug: false, | ||
format: 'YYYY-MM-DD', | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<%# | ||
# Date Form Partial | ||
This partial renders an input element for a date attribute. | ||
By default, the input is a text field that is augmented with [DateTimePicker]. | ||
## Local variables: | ||
- `f`: | ||
A Rails form generator, used to help create the appropriate input fields. | ||
- `field`: | ||
An instance of [Administrate::Field::Date][1]. | ||
A wrapper around the Date value pulled from the database. | ||
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Date | ||
[DateTimePicker]: https://github.com/Eonasdan/bootstrap-datetimepicker | ||
%> | ||
|
||
<div class="field-unit__label"> | ||
<%= f.label field.attribute %> | ||
</div> | ||
<div class="field-unit__field"> | ||
<%= f.text_field field.attribute, data: { type: 'date' } %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<%# | ||
# Date Index Partial | ||
This partial renders a date attribute, | ||
to be displayed on a resource's index page. | ||
By default, the attribute is rendered | ||
as a localized date string. | ||
## Local variables: | ||
- `field`: | ||
An instance of [Administrate::Field::Date][1]. | ||
A wrapper around the Date value pulled from the database. | ||
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Date | ||
%> | ||
<% if field.data %> | ||
<%= field.date %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<%# | ||
# Date Show Partial | ||
This partial renders a date attribute, | ||
to be displayed on a resource's show page. | ||
By default, the attribute is rendered | ||
as a localized date string. | ||
## Local variables: | ||
- `field`: | ||
An instance of [Administrate::Field::Date][1]. | ||
A wrapper around the Date value pulled from the database. | ||
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Date | ||
%> | ||
<% if field.data %> | ||
<%= field.date %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require_relative "base" | ||
|
||
module Administrate | ||
module Field | ||
class Date < Base | ||
def date | ||
I18n.localize( | ||
data.to_date, | ||
format: format, | ||
) | ||
end | ||
|
||
private | ||
|
||
def format | ||
options.fetch(:format, :default) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require "rails_helper" | ||
require "administrate/field/date" | ||
|
||
describe Administrate::Field::Date do | ||
let(:start_date) { Date.parse("2015-12-25") } | ||
let(:formats) do | ||
{ | ||
date: { | ||
formats: { default: "%m/%d/%Y", short: "%b %d" }, | ||
abbr_month_names: Array.new(13) { |i| "Dec" if i == 12 }, | ||
abbr_day_names: Array.new(7) { |i| "Fri" if i == 5 }, | ||
}, | ||
time: { | ||
formats: { default: "%a, %b %-d, %Y", short: "%d %b" }, | ||
}, | ||
} | ||
end | ||
|
||
describe "#date" do | ||
it "displays the date" do | ||
with_translations(:en, formats) do | ||
field = Administrate::Field::Date. | ||
new(:start_date, start_date, :show) | ||
expect(field.date).to eq("12/25/2015") | ||
end | ||
end | ||
|
||
context "with `prefix` option" do | ||
it "displays the date in the requested format" do | ||
options_field = Administrate::Field::Date. | ||
with_options(format: :short) | ||
field = options_field.new(:start_date, start_date, :show) | ||
|
||
with_translations(:en, formats) do | ||
expect(field.date).to eq("Dec 25") | ||
end | ||
end | ||
|
||
it "displays the date using a format string" do | ||
options_field = Administrate::Field::Date. | ||
with_options(format: "%Y") | ||
field = options_field.new(:start_date, start_date, :show) | ||
|
||
with_translations(:en, formats) do | ||
expect(field.date).to eq("2015") | ||
end | ||
end | ||
end | ||
end | ||
end |