From 3c31483db53c303116964e68d04a0fd8b60b4dad Mon Sep 17 00:00:00 2001 From: Igor Galeta Date: Tue, 6 Jun 2017 15:13:58 +0300 Subject: [PATCH 1/2] Allow id to be string to support not integer ids (postgresql uuid) --- lib/rectify/form.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/rectify/form.rb b/lib/rectify/form.rb index 2c7cda0..914e85e 100644 --- a/lib/rectify/form.rb +++ b/lib/rectify/form.rb @@ -5,7 +5,7 @@ class Form attr_reader :context - attribute :id, Integer + attribute :id, String def self.from_params(params, additional_params = {}) params_hash = hash_from(params) @@ -55,7 +55,11 @@ def self.hash_from(params) end def persisted? - id.present? && id.to_i > 0 + id.present? + end + + def new_record? + id.blank? end def valid?(context = nil) From 8dbbddd553a81b527aaf6a29ccff53fb369b597a Mon Sep 17 00:00:00 2001 From: Igor Galeta Date: Wed, 7 Jun 2017 09:26:46 +0300 Subject: [PATCH 2/2] Update tests --- spec/lib/rectify/form_spec.rb | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/spec/lib/rectify/form_spec.rb b/spec/lib/rectify/form_spec.rb index 4ac6a9c..65ab09e 100644 --- a/spec/lib/rectify/form_spec.rb +++ b/spec/lib/rectify/form_spec.rb @@ -56,7 +56,7 @@ it "populates the id from a params hash" do form = UserForm.from_params(params) - expect(form.id).to eq(1) + expect(form.id).to eq('1') end it "populates other root level values from a params hash" do @@ -271,23 +271,15 @@ end end - context "when the form id is zero" do - it "returns false" do - form = UserForm.new(:id => 0) - - expect(form).not_to be_persisted - end - end - - context "when the form id is less than zero" do + context "when the form id is blank" do it "returns false" do - form = UserForm.new(:id => -1) + form = UserForm.new(:id => "") expect(form).not_to be_persisted end end - context "when the form id is blank" do + context "when the form id is nil" do it "returns false" do form = UserForm.new(:id => nil) @@ -325,7 +317,7 @@ it "returns an array containing the id" do form = UserForm.new(:id => 2) - expect(form.to_key).to eq([2]) + expect(form.to_key).to eq(['2']) end end