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

Allow id to be string to support not integer ids (postgresql uuid) #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions lib/rectify/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 5 additions & 13 deletions spec/lib/rectify/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down