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

[backport] Add a time type attribute #3141

Merged
merged 1 commit into from
Feb 6, 2024
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
14 changes: 14 additions & 0 deletions app/values/blacklight/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ def cast(input)
end
end

class Time < Value
def cast(input)
value = super
return if value.blank?

begin
::Time.parse(value.to_s) # rubocop:disable Rails/TimeZone
rescue ArgumentError
Rails.logger&.info "Unable to parse time: #{value.inspect}"
end
end
end

class Boolean < Value
def cast(input)
ActiveModel::Type::Boolean.new.cast(super)
Expand Down Expand Up @@ -110,6 +123,7 @@ def cast(input)
register :boolean, Boolean
register :string, String
register :date, Date
register :time, Time
register :array, Array
register :json, JsonValue
register :html, Html
Expand Down
8 changes: 6 additions & 2 deletions spec/models/solr_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,25 @@
attribute :author, :array, 'author_tesim', of: :string
attribute :first_author, :select, 'author_tesim', by: :min
attribute :date, :date, field: 'date_dtsi'
attribute :time, :time, field: 'date_dtsi'
attribute :whatever, :string, default: ->(*) { 'default_value' }
end
end
let(:document) do
doc_class.new(id: '123',
title_tesim: ['Good Omens'],
author_tesim: ['Neil Gaiman', 'Terry Pratchett'],
date_dtsi: '1990-01-01T00:00:00Z')
date_dtsi: '1990-01-01T17:23:13Z')
end

it "casts the attributes" do
expect(document.title).to eq 'Good Omens'
expect(document.author).to eq ['Neil Gaiman', 'Terry Pratchett']
expect(document.first_author).to eq 'Neil Gaiman'
expect(document.date).to eq Date.new(1990)
expect(document.date).to be_a Date
expect(document.date.to_s).to eq '1990-01-01'
expect(document.time).to be_a Time
expect(document.time.to_s).to eq '1990-01-01 17:23:13 UTC'
expect(document.whatever).to eq 'default_value'
end

Expand Down
Loading