diff --git a/db/config.yml b/db/config.yml index 19d1e9d..0b93e4f 100644 --- a/db/config.yml +++ b/db/config.yml @@ -2,7 +2,7 @@ default: &default adapter: postgresql database: jsonb_accessor host: <%= ENV.fetch("DATABASE_HOST") { "127.0.0.1" } %> - user: postgres + username: <%= ENV.fetch("DATABASE_USER") { "postgres" } %> test: <<: *default diff --git a/lib/jsonb_accessor/helpers.rb b/lib/jsonb_accessor/helpers.rb index a6f452e..d26a118 100644 --- a/lib/jsonb_accessor/helpers.rb +++ b/lib/jsonb_accessor/helpers.rb @@ -24,14 +24,23 @@ def deserialize_value(value, attribute_type) return value if value.blank? if attribute_type == :datetime - value = if active_record_default_timezone == :utc - Time.find_zone("UTC").parse(value).in_time_zone + value = if value.is_a?(Array) + value.map { |v| parse_date(v) } else - Time.zone.parse(value) + parse_date(value) end end value end + + # Parse datetime based on the configured default_timezone + def parse_date(datetime) + if active_record_default_timezone == :utc + Time.find_zone("UTC").parse(datetime).in_time_zone + else + Time.zone.parse(datetime) + end + end end end diff --git a/spec/jsonb_accessor_spec.rb b/spec/jsonb_accessor_spec.rb index b474a65..7679380 100644 --- a/spec/jsonb_accessor_spec.rb +++ b/spec/jsonb_accessor_spec.rb @@ -19,7 +19,8 @@ def build_class(jsonb_accessor_config, &block) bar: :integer, ban: :integer, baz: [:integer, { array: true }], - bazzle: [:integer, { default: 5 }] + bazzle: [:integer, { default: 5 }], + dates: [:datetime, { array: true }] ) do enum ban: { foo: 1, bar: 2 } end @@ -58,6 +59,17 @@ def build_class(jsonb_accessor_config, &block) expect(instance.baz).to eq([1, 2, 3]) end + it "supports array of date" do + # Write + instance.dates = [Date.new(2017, 1, 1), Date.new(2017, 1, 2)] + expect { instance.save! }.to_not raise_error + # Read + instance.reload + + expect(instance.dates).to be_kind_of Array + expect(instance.dates.first).to be_kind_of Time + end + it "supports defaults" do expect(instance.bazzle).to eq(5) end