Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Date Field #1530

Merged
merged 2 commits into from Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -7,4 +7,8 @@ $(function () {
debug: false,
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