From 899c0bcb46f7485e0674d0b091ebea1f7c9a9e81 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 3 Nov 2018 13:54:02 -0400 Subject: [PATCH] Update for compatibility with Crystal 0.27.0 Because a few methods on `Time` have been renamed, this is a breaking change, and Crecto will not be compatible with versions before 0.27.0 after this commit. --- .crystal-version | 2 +- .tool-versions | 1 + spec/repo_spec.cr | 8 ++++---- spec/schema_spec.cr | 6 +++--- src/crecto/changeset/changeset.cr | 14 +++++++------- 5 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 .tool-versions diff --git a/.crystal-version b/.crystal-version index a67ceba..1b58cc1 100644 --- a/.crystal-version +++ b/.crystal-version @@ -1 +1 @@ -0.21.1 +0.27.0 diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..7841a7d --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +crystal 0.27.0 diff --git a/spec/repo_spec.cr b/spec/repo_spec.cr index 242c863..b15acfb 100644 --- a/spec/repo_spec.cr +++ b/spec/repo_spec.cr @@ -447,7 +447,7 @@ describe Crecto do user = Repo.get(User, id) user.is_a?(User).should eq(true) user.not_nil!.id.should eq(id) - user.not_nil!.some_date.as(Time).to_local.epoch_ms.should be_close(now.epoch_ms, 2000) + user.not_nil!.some_date.as(Time).to_local.to_unix_ms.should be_close(now.to_unix_ms, 2000) end it "should return nil if not in db" do @@ -782,7 +782,7 @@ describe Crecto do u.created_at.should eq(created_at) changeset.instance.name.should eq("new name") changeset.valid?.should eq(true) - changeset.instance.updated_at.as(Time).to_local.epoch_ms.should be_close(Time.now.epoch_ms, 2000) + changeset.instance.updated_at.as(Time).to_local.to_unix_ms.should be_close(Time.now.to_unix_ms, 2000) end it "should return a changeset and set the changeset action" do @@ -822,7 +822,7 @@ describe Crecto do u.created_at.should eq(created_at) changeset.instance.name.should eq("new name") changeset.valid?.should eq(true) - changeset.instance.updated_at.as(Time).to_local.epoch_ms.should be_close(Time.now.epoch_ms, 2000) + changeset.instance.updated_at.as(Time).to_local.to_unix_ms.should be_close(Time.now.to_unix_ms, 2000) end it "should raise if changeset is invalid (name is nil)" do @@ -1087,7 +1087,7 @@ describe Crecto do user.user_projects.size.should eq 1 user.projects.size.should eq 1 end - + it "should preload the has_many through association with a query" do user = User.new user.name = "tester" diff --git a/spec/schema_spec.cr b/spec/schema_spec.cr index d171699..3582820 100644 --- a/spec/schema_spec.cr +++ b/spec/schema_spec.cr @@ -208,7 +208,7 @@ describe Crecto do end it "allows the primary key to be a string" do user = UserUUIDCustom.new - user.name = "whatever" + user.name = "whatever" # Need to set this because of MySQL and SQLite # MySQL actually inserts the uuid because of the trigger, # but the `instance` method below seems to return the object before the trigger is fired. @@ -255,7 +255,7 @@ describe Crecto do it "should set the updated at value to now" do u = User.new u.updated_at_to_now - u.updated_at.as(Time).epoch_ms.should be_close(Time.now.epoch_ms, 100) + u.updated_at.as(Time).to_unix_ms.should be_close(Time.now.to_unix_ms, 100) end end @@ -263,7 +263,7 @@ describe Crecto do it "should set the created at value to now" do u = UserDifferentDefaults.new u.created_at_to_now - u.xyz.as(Time).epoch_ms.should be_close(Time.now.epoch_ms, 2000) + u.xyz.as(Time).to_unix_ms.should be_close(Time.now.to_unix_ms, 2000) end end diff --git a/src/crecto/changeset/changeset.cr b/src/crecto/changeset/changeset.cr index ae8798b..0b77254 100644 --- a/src/crecto/changeset/changeset.cr +++ b/src/crecto/changeset/changeset.cr @@ -112,8 +112,8 @@ module Crecto return unless REQUIRED_FORMATS.has_key?(@class_key) REQUIRED_FORMATS[@class_key].each do |format| next unless @instance_hash[format[:field]]? - raise Crecto::InvalidType.new("Format validator can only validate strings") unless @instance_hash.fetch(format[:field]).is_a?(String) - val = @instance_hash.fetch(format[:field]).as(String) + raise Crecto::InvalidType.new("Format validator can only validate strings") unless @instance_hash[format[:field]].is_a?(String) + val = @instance_hash[format[:field]].as(String) add_error(format[:field].to_s, "is invalid") if format[:pattern].match(val).nil? end end @@ -122,7 +122,7 @@ module Crecto return unless REQUIRED_ARRAY_INCLUSIONS.has_key?(@class_key) REQUIRED_ARRAY_INCLUSIONS[@class_key].each do |inclusion| next unless @instance_hash[inclusion[:field]]? - val = @instance_hash.fetch(inclusion[:field]) + val = @instance_hash[inclusion[:field]] add_error(inclusion[:field].to_s, "is invalid") unless inclusion[:in].includes?(val) end end @@ -131,7 +131,7 @@ module Crecto return unless REQUIRED_RANGE_INCLUSIONS.has_key?(@class_key) REQUIRED_RANGE_INCLUSIONS[@class_key].each do |inclusion| next unless @instance_hash[inclusion[:field]]? - val = @instance_hash.fetch(inclusion[:field]) + val = @instance_hash[inclusion[:field]] if inclusion[:in].is_a?(Range(Float64, Float64)) && val.is_a?(Float64) add_error(inclusion[:field].to_s, "is invalid") unless inclusion[:in].as(Range(Float64, Float64)).includes?(val.as(Float64)) elsif inclusion[:in].is_a?(Range(Int32, Int32)) && val.is_a?(Int32) @@ -148,7 +148,7 @@ module Crecto return unless REQUIRED_ARRAY_EXCLUSIONS.has_key?(@class_key) REQUIRED_ARRAY_EXCLUSIONS[@class_key].each do |exclusion| next unless @instance_hash[exclusion[:field]]? - val = @instance_hash.fetch(exclusion[:field]) + val = @instance_hash[exclusion[:field]] add_error(exclusion[:field].to_s, "is invalid") if exclusion[:in].includes?(val) end end @@ -157,7 +157,7 @@ module Crecto return unless REQUIRED_RANGE_EXCLUSIONS.has_key?(@class_key) REQUIRED_RANGE_EXCLUSIONS[@class_key].each do |exclusion| next unless @instance_hash[exclusion[:field]]? - val = @instance_hash.fetch(exclusion[:field]) + val = @instance_hash[exclusion[:field]] if exclusion[:in].is_a?(Range(Float64, Float64)) && val.is_a?(Float64) add_error(exclusion[:field].to_s, "is invalid") if exclusion[:in].as(Range(Float64, Float64)).includes?(val.as(Float64)) elsif exclusion[:in].is_a?(Range(Int32, Int32)) && val.is_a?(Int32) @@ -174,7 +174,7 @@ module Crecto return unless REQUIRED_LENGTHS.has_key?(@class_key) REQUIRED_LENGTHS[@class_key].each do |length| next unless @instance_hash[length[:field]]? - val = @instance_hash.fetch(length[:field]).as(String) + val = @instance_hash[length[:field]].as(String) add_error(length[:field].to_s, "is invalid") if !length[:is].nil? && val.size != length[:is].as(Int32) add_error(length[:field].to_s, "is invalid") if !length[:min].nil? && val.size < length[:min].as(Int32) add_error(length[:field].to_s, "is invalid") if !length[:max].nil? && val.size > length[:max].as(Int32)