Skip to content

Commit

Permalink
Merge pull request #3141 from projectblacklight/backport-time-type
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored Feb 6, 2024
2 parents bdf125a + a73094b commit 9756184
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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

0 comments on commit 9756184

Please sign in to comment.