From a390c49d3472d688f71ec94582dbba6f5e9293a8 Mon Sep 17 00:00:00 2001 From: Jamie Davidson Date: Tue, 29 Apr 2014 19:14:02 -0400 Subject: [PATCH 1/3] Make UUID regex match only exact UUIDs (currently, it'll match strings with UUIDs anywhere in the string) --- lib/friendly_id/finder_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/friendly_id/finder_methods.rb b/lib/friendly_id/finder_methods.rb index f24921aa5..9bbb58c45 100644 --- a/lib/friendly_id/finder_methods.rb +++ b/lib/friendly_id/finder_methods.rb @@ -47,7 +47,7 @@ def potential_primary_key?(id) when :integer Integer(id, 10) rescue false when :uuid - id.match /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ + id.match /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/ else true end From 78be486c2841faf893b03ab02a8074eb6f1baf78 Mon Sep 17 00:00:00 2001 From: Jamie Davidson Date: Tue, 29 Apr 2014 22:13:54 -0400 Subject: [PATCH 2/3] Add test to verify a friendly find with a string that contains a UUID is handled correctly --- test/schema.rb | 8 ++++---- test/slugged_test.rb | 28 +++++++++++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/test/schema.rb b/test/schema.rb index cbf2a0e6a..2c4dc5cd3 100644 --- a/test/schema.rb +++ b/test/schema.rb @@ -23,10 +23,10 @@ def up end end - tables_with_string_primary_key.each do |table_name| - create_table table_name, primary_key: :string_key, id: false do |t| + tables_with_uuid_primary_key.each do |table_name| + create_table table_name, primary_key: :uuid_key, id: false do |t| t.string :name - t.string :string_key, null: false + t.string :uuid_key, null: false t.string :slug end add_index table_name, :slug, unique: true @@ -73,7 +73,7 @@ def slugged_tables %w[journalists articles novelists novels manuals] end - def tables_with_string_primary_key + def tables_with_uuid_primary_key ["menu_items"] end diff --git a/test/slugged_test.rb b/test/slugged_test.rb index ad50ff17e..411d53fcd 100644 --- a/test/slugged_test.rb +++ b/test/slugged_test.rb @@ -241,7 +241,7 @@ class Journalist < ActiveRecord::Base end end -class StringAsPrimaryKeyFindTest < MiniTest::Unit::TestCase +class UuidAsPrimaryKeyFindTest < MiniTest::Unit::TestCase include FriendlyId::Test class MenuItem < ActiveRecord::Base @@ -250,12 +250,17 @@ class MenuItem < ActiveRecord::Base before_create :init_primary_key def self.primary_key - "string_key" + "uuid_key" + end + + # Overwrite the method added by FriendlyId + def self.primary_key_type + :uuid end private def init_primary_key - self.string_key = SecureRandom.uuid + self.uuid_key = SecureRandom.uuid end end @@ -263,16 +268,25 @@ def model_class MenuItem end - test "should have a string as a primary key" do - assert_equal model_class.primary_key, "string_key" - assert_equal model_class.columns.find(&:primary).name, "string_key" + test "should have a uuid_key as a primary key" do + assert_equal model_class.primary_key, "uuid_key" + assert_equal model_class.columns.find(&:primary).name, "uuid_key" + assert_equal model_class.primary_key_type, :uuid end - test "should be findable by the string primary key" do + test "should be findable by the UUID primary key" do with_instance_of(model_class) do |record| assert model_class.friendly.find record.id end end + + test "should handle a string that simply contains a UUID correctly" do + with_instance_of(model_class) do |record| + assert_raises(ActiveRecord::RecordNotFound) do + model_class.friendly.find "test-#{SecureRandom.uuid}" + end + end + end end class UnderscoreAsSequenceSeparatorRegressionTest < MiniTest::Unit::TestCase From 1220959ae9849ae6b7d8b213dc22a3f8a48ab33e Mon Sep 17 00:00:00 2001 From: Jamie Davidson Date: Mon, 26 May 2014 11:11:32 -0400 Subject: [PATCH 3/3] Update UUID regex --- lib/friendly_id/finder_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/friendly_id/finder_methods.rb b/lib/friendly_id/finder_methods.rb index 9bbb58c45..cf6de804e 100644 --- a/lib/friendly_id/finder_methods.rb +++ b/lib/friendly_id/finder_methods.rb @@ -47,7 +47,7 @@ def potential_primary_key?(id) when :integer Integer(id, 10) rescue false when :uuid - id.match /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/ + id.match /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/ else true end