Skip to content

Commit

Permalink
Include Date Field (#1530)
Browse files Browse the repository at this point in the history
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
sebaherrera07 authored and pablobm committed Jan 25, 2020
1 parent 19929bd commit d153bcf
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 6 deletions.
@@ -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',
});
});
24 changes: 24 additions & 0 deletions app/views/fields/date/_form.html.erb
@@ -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>
21 changes: 21 additions & 0 deletions app/views/fields/date/_index.html.erb
@@ -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 %>
21 changes: 21 additions & 0 deletions app/views/fields/date/_show.html.erb
@@ -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 %>
1 change: 1 addition & 0 deletions docs/customizing_attribute_partials.md
Expand Up @@ -6,6 +6,7 @@ across all dashboards. You can customize the following built in field types:
- `belongs_to`
- `boolean`
- `date_time`
- `date`
- `email`
- `has_many`
- `has_one`
Expand Down
5 changes: 5 additions & 0 deletions docs/customizing_dashboards.md
Expand Up @@ -190,6 +190,11 @@ objects to display as.
`:timezone` - Specify which timezone `Date` and `DateTime` objects are based
in.

**Field::Date**

`:format` - Specify what format, using `strftime` you would like `Date`
objects to display as.

**Field::Select**

`:collection` - Specify the array or range to select from. Defaults to `[]`.
Expand Down
1 change: 1 addition & 0 deletions lib/administrate/base_dashboard.rb
@@ -1,6 +1,7 @@
require "administrate/field/belongs_to"
require "administrate/field/boolean"
require "administrate/field/date_time"
require "administrate/field/date"
require "administrate/field/email"
require "administrate/field/has_many"
require "administrate/field/has_one"
Expand Down
20 changes: 20 additions & 0 deletions lib/administrate/field/date.rb
@@ -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
Expand Up @@ -5,7 +5,7 @@ module Generators
class DashboardGenerator < Rails::Generators::NamedBase
ATTRIBUTE_TYPE_MAPPING = {
boolean: "Field::Boolean",
date: "Field::DateTime",
date: "Field::Date",
datetime: "Field::DateTime",
enum: "Field::String",
float: "Field::Number",
Expand Down
6 changes: 3 additions & 3 deletions spec/generators/dashboard_generator_spec.rb
Expand Up @@ -174,8 +174,8 @@ class User < ApplicationRecord
end
end

it "assigns dates, times, and datetimes a type of `DateTime` and
`Time`" do
it "assigns dates, times, and datetimes a type of `Date`, `DateTime` and
`Time` respectively" do
begin
ActiveRecord::Schema.define do
create_table :events do |t|
Expand All @@ -193,7 +193,7 @@ class Event < ApplicationRecord
load file("app/dashboards/event_dashboard.rb")
attrs = EventDashboard::ATTRIBUTE_TYPES

expect(attrs[:start_date]).to eq(Administrate::Field::DateTime)
expect(attrs[:start_date]).to eq(Administrate::Field::Date)
expect(attrs[:start_time]).to eq(Administrate::Field::Time)
expect(attrs[:ends_at]).to eq(Administrate::Field::DateTime)
ensure
Expand Down
50 changes: 50 additions & 0 deletions spec/lib/fields/date_spec.rb
@@ -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

0 comments on commit d153bcf

Please sign in to comment.