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 column support #1567

Merged
merged 2 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/resources/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
label_method: relation[:attr_method],
include_blank: Alchemy.t(:blank, scope: 'resources.relation_select'),
input_html: {class: 'alchemy_selectbox'} %>
<% elsif attribute[:type] == :datetime || attribute[:type] == :time %>
<% elsif attribute[:type].to_s =~ /(date|time)/ %>
<div class="input <%= attribute[:type] %>">
<label class="control-label">
<%= f.object.class.human_attribute_name(attribute[:name]) %>
Expand Down
6 changes: 6 additions & 0 deletions config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,12 @@ en:
error_notification:
default_message: "Please review the problems below:"

# Alchemy date formats
date:
formats:
alchemy:
default: "%Y-%m-%d"

# Alchemy time formats
time:
formats:
Expand Down
4 changes: 3 additions & 1 deletion lib/alchemy/resources_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ def render_attribute(resource, attribute, options = {})
if attribute[:relation]
record = resource.send(attribute[:relation][:name])
value = record.present? ? record.send(attribute[:relation][:attr_method]) : Alchemy.t(:not_found)
elsif attribute_value && (attribute[:type] == :datetime || attribute[:type] == :time)
elsif attribute_value && attribute[:type].to_s =~ /(date|time)/
localization_format = if attribute[:type] == :datetime
options[:datetime_format] || :'alchemy.default'
elsif attribute[:type] == :date
options[:date_format] || :'alchemy.default'
else
options[:time_format] || :'alchemy.time'
end
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/controllers/admin/bookings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Admin::BookingsController < Alchemy::Admin::ResourcesController
end
2 changes: 2 additions & 0 deletions spec/dummy/app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ def initialize(*)
can :index, :admin_locations
can :manage, Series
can :index, :admin_series
can :manage, Booking
can :index, :admin_bookings
end
end
4 changes: 4 additions & 0 deletions spec/dummy/app/models/booking.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Booking < ActiveRecord::Base
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
end
6 changes: 5 additions & 1 deletion spec/dummy/config/initializers/alchemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
}, {
name: 'Series',
controller: '/admin/series',
action: 'index'
action: 'index'
}, {
name: 'Bookings',
controller: '/admin/bookings',
action: 'index'
}]
}
)
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
resources :events
resources :locations
resources :series
resources :bookings
end

mount Alchemy::Engine => "/"
Expand Down
10 changes: 10 additions & 0 deletions spec/dummy/db/migrate/20190417070726_create_bookings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateBookings < ActiveRecord::Migration[5.2]
def change
create_table :bookings do |t|
t.date :from
t.date :until

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_05_19_204655) do
ActiveRecord::Schema.define(version: 2019_04_17_070726) do

create_table "alchemy_attachments", force: :cascade do |t|
t.string "name"
Expand Down Expand Up @@ -261,6 +261,13 @@
t.index ["host"], name: "index_alchemy_sites_on_host"
end

create_table "bookings", force: :cascade do |t|
t.date "from"
t.date "until"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "dummy_models", force: :cascade do |t|
t.string "data"
end
Expand Down
22 changes: 22 additions & 0 deletions spec/features/admin/resources_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
visit '/admin/events'
expect(page).to have_selector('div#archive_all table.list')
end

describe "date fields" do
let(:yesterday) { Date.yesterday }
let(:tomorrow) { Date.tomorrow }

before do
Booking.create(from: yesterday, until: tomorrow)
end

it "displays date values" do
visit '/admin/bookings'
expect(page).to have_content(yesterday)
expect(page).to have_content(tomorrow)
end
end
end

describe "form for creating and updating items" do
Expand All @@ -48,6 +63,13 @@
expect(page).to have_selector('select')
end
end

describe "date fields" do
it "have date picker" do
visit '/admin/bookings/new'
expect(page).to have_selector('input#booking_from[data-datepicker-type="date"]')
end
end
end

describe "create resource item" do
Expand Down