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

fix for UUID set on invalid object #1013

Open
wants to merge 1 commit 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
10 changes: 7 additions & 3 deletions lib/friendly_id/slugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,13 @@ def slug_generator
private :slug_generator

def unset_slug_if_invalid
if errors.key?(friendly_id_config.query_field) && attribute_changed?(friendly_id_config.query_field.to_s)
diff = changes[friendly_id_config.query_field]
send "#{friendly_id_config.slug_column}=", diff.first
if (
errors.key?(friendly_id_config.base) ||
errors.key?(friendly_id_config.query_field) # slug_column
) && attribute_changed?(friendly_id_config.query_field)

prior_slug = changes[friendly_id_config.query_field]&.first
send "#{friendly_id_config.query_field}=", prior_slug
end
end
private :unset_slug_if_invalid
Expand Down
16 changes: 16 additions & 0 deletions test/slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ def self.name
refute instance.valid?
end

test "should allow validations on the base" do
model_class = Class.new(ActiveRecord::Base) do
self.table_name = "articles"
extend FriendlyId
friendly_id :name, use: :slugged
validates :name, presence: true
end
instance = model_class.new name: ""
refute instance.valid? # run Slugged validation hooks

# setting slug to UUID, e.g. "2d28c5c1-4eee-49ef-aee8-4842fddbe895",
# at this point is a problem. This will be sent back to Rails form_with
# and inserted into the form action= url value.
assert(instance.slug.nil?)
end

test "should allow nil slugs" do
transaction do
m1 = model_class.create!
Expand Down