From 01ff510bf009b6e7ad573846fdb5fb284fb08eed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ug=CC=A7is=20Ozols?= Date: Mon, 3 Feb 2014 09:50:29 +0200 Subject: Tell friendly_id to regenerate slug if title has changed. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is due to change in friendly_id 5.0.x where it doesn’t regenerate slug when record is updated. --- app/models/refinery/blog/post.rb | 6 ++++++ spec/models/refinery/blog/post_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/app/models/refinery/blog/post.rb b/app/models/refinery/blog/post.rb index 005ec29..f80a939 100644 --- a/app/models/refinery/blog/post.rb +++ b/app/models/refinery/blog/post.rb @@ -32,6 +32,12 @@ module Refinery is_seo_meta end + # If custom_url or title changes tell friendly_id to regenerate slug when + # saving record + def should_generate_new_friendly_id? + custom_url_changed? || title_changed? + end + # Delegate SEO Attributes to globalize translation seo_fields = ::SeoMeta.attributes.keys.map{|a| [a, :"#{a}="]}.flatten delegate(*(seo_fields << {:to => :translation})) diff --git a/spec/models/refinery/blog/post_spec.rb b/spec/models/refinery/blog/post_spec.rb index 8b156cb..99946b8 100644 --- a/spec/models/refinery/blog/post_spec.rb +++ b/spec/models/refinery/blog/post_spec.rb @@ -234,6 +234,30 @@ module Refinery end end + describe "#should_generate_new_friendly_id?" do + context "when custom_url changes" do + it "regenerates slug upon save" do + post = FactoryGirl.create(:blog_post, :custom_url => "Test Url") + + post.custom_url = "Test Url 2" + post.save! + + expect(post.slug).to eq("test-url-2") + end + end + + context "when title changes" do + it "regenerates slug upon save" do + post = FactoryGirl.create(:blog_post, :title => "Test Title") + + post.title = "Test Title 2" + post.save! + + expect(post.slug).to eq("test-title-2") + end + end + end + end end end -- cgit v1.2.3