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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date and Time fields #931

Closed
wants to merge 14 commits into from
10 changes: 10 additions & 0 deletions app/assets/javascripts/administrate/components/date_time_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@ $(function () {
debug: false,
format: "YYYY-MM-DD HH:mm:ss",
});

$(".datepicker").datetimepicker({
debug: false,
format: "YYYY-MM-DD",
});

$(".timepicker").datetimepicker({
debug: false,
format: "HH:mm:ss",
});
});
24 changes: 24 additions & 0 deletions app/views/fields/date/_form.html.erb
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 [DatePicker].

## 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
[DatePicker]: 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, class: "datepicker" %>
</div>
21 changes: 21 additions & 0 deletions app/views/fields/date/_index.html.erb
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 %>
21 changes: 21 additions & 0 deletions app/views/fields/date/_show.html.erb
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 %>
24 changes: 24 additions & 0 deletions app/views/fields/time/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<%#
# Time Form Partial

This partial renders an input element for a time attribute.
By default, the input is a text field that is augmented with [DatePicker].

## Local variables:

- `f`:
A Rails form generator, used to help create the appropriate input fields.
- `field`:
An instance of [Administrate::Field::Time][1].
A wrapper around the Time value pulled from the database.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Time
[DatePicker]: 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, class: "timepicker" %>
</div>
21 changes: 21 additions & 0 deletions app/views/fields/time/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%#
# Time Index Partial

This partial renders a time attribute,
to be displayed on a resource's index page.

By default, the attribute is rendered
as a localized time string.

## Local variables:

- `field`:
An instance of [Administrate::Field::Time][1].
A wrapper around the Time value pulled from the database.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Time
%>

<% if field.data %>
<%= field.time %>
<% end %>
21 changes: 21 additions & 0 deletions app/views/fields/time/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%#
# Time Show Partial

This partial renders a time attribute,
to be displayed on a resource's show page.

By default, the attribute is rendered
as a localized time string.

## Local variables:

- `field`:
An instance of [Administrate::Field::Time][1].
A wrapper around the Time value pulled from the database.

[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Time
%>

<% if field.data %>
<%= field.time %>
<% end %>
3 changes: 3 additions & 0 deletions config/locales/administrate.ar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ ar:
search:
clear: مسح البحث
label: بحث %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.da.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ da:
search:
clear: Ryd søgning
label: Søg %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ de:
search:
clear: Saubere Suche
label: Suche %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ en:
search:
clear: Clear search
label: Search %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ es:
search:
clear: Borrar búsqueda
label: Buscar %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ fr:
search:
clear: Effacer la recherche
label: Chercher %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ it:
search:
clear: Cancella ricerca
label: Ricerca %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ ja:
search:
clear: 検索をクリアする
label: サーチ %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.ko.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ ko:
search:
clear: 검색 초기화
label: "%{resource} 검색"
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ nl:
search:
clear: CDuidelijke zoek
label: Zoeken %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ pl:
search:
clear: Wyczyść wyszukiwanie
label: Szukanie %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ pt-BR:
search:
clear: Limpar pesquisa
label: Pesquisa %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ pt:
search:
clear: Limpar pesquisa
label: Pesquisa %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ ru:
search:
clear: Очистить поиск
label: Поиск %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.sv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ sv:
search:
clear: Rensa sökningen
label: Sök %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ uk:
search:
clear: Очистити пошук
label: пошук %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ vi:
search:
clear: Tìm kiếm rõ ràng
label: Tìm kiếm %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ zh-CN:
search:
clear: 清除搜索
label: 搜索 %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
3 changes: 3 additions & 0 deletions config/locales/administrate.zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ zh-TW:
search:
clear: 清除搜索
label: 搜索 %{resource}
time:
formats:
time_without_date: "%H:%M:%S"
2 changes: 2 additions & 0 deletions lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "administrate/field/belongs_to"
require "administrate/field/boolean"
require "administrate/field/date"
require "administrate/field/date_time"
require "administrate/field/email"
require "administrate/field/has_many"
Expand All @@ -9,6 +10,7 @@
require "administrate/field/select"
require "administrate/field/string"
require "administrate/field/text"
require "administrate/field/time"

module Administrate
class BaseDashboard
Expand Down
17 changes: 17 additions & 0 deletions lib/administrate/field/date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
17 changes: 17 additions & 0 deletions lib/administrate/field/time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative "base"

module Administrate
module Field
class Time < Base
def time
I18n.localize(data, format: format)
end

private

def format
options.fetch(:format, :time_without_date)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/generators/administrate/dashboard/dashboard_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ 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",
integer: "Field::Number",
time: "Field::DateTime",
time: "Field::Time",
text: "Field::Text",
string: "Field::String",
}
Expand Down
4 changes: 4 additions & 0 deletions spec/example_app/app/dashboards/customer_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class CustomerDashboard < Administrate::BaseDashboard
orders: Field::HasMany.with_options(limit: 2),
updated_at: Field::DateTime,
kind: Field::Select.with_options(collection: Customer::KINDS),
birth_date: Field::Date,
birth_time: Field::Time,
}

COLLECTION_ATTRIBUTES = ATTRIBUTE_TYPES.keys
Expand All @@ -19,6 +21,8 @@ class CustomerDashboard < Administrate::BaseDashboard
:email,
:email_subscriber,
:kind,
:birth_date,
:birth_time,
].freeze

def display_resource(customer)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBirthDateToCustomer < ActiveRecord::Migration[5.1]
def change
add_column :customers, :birth_date, :date
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddBirthTimeToCustomer < ActiveRecord::Migration[5.1]
def change
add_column :customers, :birth_time, :time
end
end
2 changes: 2 additions & 0 deletions spec/example_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
t.datetime "updated_at", null: false
t.boolean "email_subscriber"
t.string "kind", default: "standard", null: false
t.date "birth_date"
t.time "birth_time"
end

create_table "line_items", id: :serial, force: :cascade do |t|
Expand Down
Loading