From 54de5d2d73ed49061d764596c28ee63f23468140 Mon Sep 17 00:00:00 2001 From: Aashish Kiran Date: Wed, 14 Mar 2018 20:24:16 +0530 Subject: [PATCH 1/6] url data can be reinitialized --- README.rdoc | 5 ++++- lib/stringex/acts_as_url.rb | 7 +++++++ lib/stringex/acts_as_url/adapter/base.rb | 6 ++++++ test/unit/acts_as_url_integration_test.rb | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index ef7b173c..81739ec5 100644 --- a/README.rdoc +++ b/README.rdoc @@ -46,7 +46,10 @@ In order to use the generated url attribute, you will probably want to override Routing called via named routes like foo_path(@foo) will automatically use the url. In your controllers you will need to call Foo.find_by_url(params[:id]) instead of the regular find. Don't look for params[:url] unless you set it explicitly in the routing, to_param will generate params[:id]. -Note that if you add acts_as_url to an existing model, the url database column will initially be blank. To set this column for your existing instances, you can use the initialize_urls method. So if your class is Post, just say Post.initialize_urls. +Note that if you add acts_as_url to an existing model, the url database column will initially be blank. +To set this column for your existing instances, you can use the initialize_urls method. So if your class is Post, just say Post.initialize_urls. + +To update the existing url database column with data, you can use reinitialize_urls method. So if your class is Post, just say Post.reinitialize_urls. Unlike other permalink solutions, ActsAsUrl doesn't rely on Iconv (which is inconsistent across platforms and doesn't provide great transliteration as is) but instead uses a transliteration scheme (see the code for Unidecoder) which produces much better results for Unicode characters. It also mixes in some custom helpers to translate common characters into a more URI-friendly format rather than just dump them completely. Examples: diff --git a/lib/stringex/acts_as_url.rb b/lib/stringex/acts_as_url.rb index 67fdcdd2..e2ac8cee 100644 --- a/lib/stringex/acts_as_url.rb +++ b/lib/stringex/acts_as_url.rb @@ -96,6 +96,13 @@ def included(base = nil, &block) def initialize_urls acts_as_url_configuration.adapter.initialize_urls! self end + + # Renitialize the url fields for the all records. Designed for people who want to update + # acts_as_url support once there's already development/production data they'd + # like to keep around. + def reinitialize_urls + acts_as_url_configuration.adapter.reinitialize_urls! self + end end module ActsAsUrlInstanceMethods diff --git a/lib/stringex/acts_as_url/adapter/base.rb b/lib/stringex/acts_as_url/adapter/base.rb index 0c51c7a6..b3f56b6c 100644 --- a/lib/stringex/acts_as_url/adapter/base.rb +++ b/lib/stringex/acts_as_url/adapter/base.rb @@ -33,6 +33,12 @@ def initialize_urls!(klass) end end + def reinitialize_urls!(klass) + self.klass = klass + klass.update_all(settings.url_attribute => nil) + initialize_urls!(klass) + end + def url_attribute(instance) # Retrieve from database record if there are errors on attribute_to_urlify if !is_new?(instance) && is_present?(instance.errors[settings.attribute_to_urlify]) diff --git a/test/unit/acts_as_url_integration_test.rb b/test/unit/acts_as_url_integration_test.rb index 20ae2220..a09cf4ba 100644 --- a/test/unit/acts_as_url_integration_test.rb +++ b/test/unit/acts_as_url_integration_test.rb @@ -224,6 +224,21 @@ def test_should_mass_initialize_urls assert_equal "subsequent", @other_doc.url end + def test_should_mass_reinitialize_urls + @doc = Document.create(title: "Initial") + @other_doc = Document.create(title: "Subsequent") + # Just making sure this got set before the reinitialize urls test + assert_equal "initial", @doc.url + assert_equal "subsequent", @other_doc.url + + Document.reinitialize_urls + + @doc.reload + @other_doc.reload + assert_equal "initial", @doc.url + assert_equal "subsequent", @other_doc.url + end + def test_should_mass_initialize_urls_with_custom_url_attribute Document.class_eval do # Manually undefining the url method on Document which, in a real class not reused for tests, From c4de900eae48aa0c87f8686288a979a45dbdb3d8 Mon Sep 17 00:00:00 2001 From: Aashish Kiran Date: Wed, 14 Mar 2018 20:37:31 +0530 Subject: [PATCH 2/6] updated readme --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 81739ec5..1ac809aa 100644 --- a/README.rdoc +++ b/README.rdoc @@ -49,7 +49,7 @@ Routing called via named routes like foo_path(@foo) will automatically Note that if you add acts_as_url to an existing model, the url database column will initially be blank. To set this column for your existing instances, you can use the initialize_urls method. So if your class is Post, just say Post.initialize_urls. -To update the existing url database column with data, you can use reinitialize_urls method. So if your class is Post, just say Post.reinitialize_urls. +To update the existing url database column which has data, you can use reinitialize_urls method. So if your class is Post, just say Post.reinitialize_urls. Unlike other permalink solutions, ActsAsUrl doesn't rely on Iconv (which is inconsistent across platforms and doesn't provide great transliteration as is) but instead uses a transliteration scheme (see the code for Unidecoder) which produces much better results for Unicode characters. It also mixes in some custom helpers to translate common characters into a more URI-friendly format rather than just dump them completely. Examples: From 8fbc5a89515a9b5dbb63f190b4c96972e4f0f0b7 Mon Sep 17 00:00:00 2001 From: Aashish Kiran Date: Wed, 14 Mar 2018 20:57:18 +0530 Subject: [PATCH 3/6] reupdated readme --- README.rdoc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.rdoc b/README.rdoc index 1ac809aa..09e0b5c2 100644 --- a/README.rdoc +++ b/README.rdoc @@ -46,10 +46,14 @@ In order to use the generated url attribute, you will probably want to override Routing called via named routes like foo_path(@foo) will automatically use the url. In your controllers you will need to call Foo.find_by_url(params[:id]) instead of the regular find. Don't look for params[:url] unless you set it explicitly in the routing, to_param will generate params[:id]. -Note that if you add acts_as_url to an existing model, the url database column will initially be blank. -To set this column for your existing instances, you can use the initialize_urls method. So if your class is Post, just say Post.initialize_urls. +Note that if you add acts_as_url to an existing model, the url +database column will initially be blank. To set this column for your existing +instances, you can use the initialize_urls method. So if your class is +Post, just say Post.initialize_urls. -To update the existing url database column which has data, you can use reinitialize_urls method. So if your class is Post, just say Post.reinitialize_urls. +To update the existing url database column which has data, you can use +reinitialize_urls method. So if your class is Post, just say < +tt>Post.reinitialize_urls. Unlike other permalink solutions, ActsAsUrl doesn't rely on Iconv (which is inconsistent across platforms and doesn't provide great transliteration as is) but instead uses a transliteration scheme (see the code for Unidecoder) which produces much better results for Unicode characters. It also mixes in some custom helpers to translate common characters into a more URI-friendly format rather than just dump them completely. Examples: @@ -133,7 +137,7 @@ If you don't want to use the Stringex built-in translations, you can force Strin You'll need to add a :find_by => :url to your load_and_authorize_resource. Here's an example: load_and_authorize_resource :class => "Whatever", :message => "Not authorized", :find_by => :url - + == Semantic Versioning This project conforms to [semver](http://semver.org/). As a result of this policy, you can (and should) specify a dependency on this gem using the [Pessimistic Version Constraint](http://guides.rubygems.org/patterns/) with two digits of precision. For example: From 0574f92211787702e0c902afb1bc097acf4a8257 Mon Sep 17 00:00:00 2001 From: Aashish Kiran Date: Wed, 14 Mar 2018 21:01:08 +0530 Subject: [PATCH 4/6] formatted comments --- README.rdoc | 12 ++++-------- lib/stringex/acts_as_url.rb | 15 ++++++--------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/README.rdoc b/README.rdoc index 09e0b5c2..a4fe5410 100644 --- a/README.rdoc +++ b/README.rdoc @@ -46,14 +46,10 @@ In order to use the generated url attribute, you will probably want to override Routing called via named routes like foo_path(@foo) will automatically use the url. In your controllers you will need to call Foo.find_by_url(params[:id]) instead of the regular find. Don't look for params[:url] unless you set it explicitly in the routing, to_param will generate params[:id]. -Note that if you add acts_as_url to an existing model, the url -database column will initially be blank. To set this column for your existing -instances, you can use the initialize_urls method. So if your class is -Post, just say Post.initialize_urls. - -To update the existing url database column which has data, you can use -reinitialize_urls method. So if your class is Post, just say < -tt>Post.reinitialize_urls. +Note that if you add acts_as_url to an existing model, the url database column will initially be blank. +To set this column for your existing instances, you can use the initialize_urls method. So if your class is Post, just say Post.initialize_urls. + +To update the existing url database column which has data, you can use reinitialize_urls method. So if your class is Post, just say Post.reinitialize_urls. Unlike other permalink solutions, ActsAsUrl doesn't rely on Iconv (which is inconsistent across platforms and doesn't provide great transliteration as is) but instead uses a transliteration scheme (see the code for Unidecoder) which produces much better results for Unicode characters. It also mixes in some custom helpers to translate common characters into a more URI-friendly format rather than just dump them completely. Examples: diff --git a/lib/stringex/acts_as_url.rb b/lib/stringex/acts_as_url.rb index e2ac8cee..173bebea 100644 --- a/lib/stringex/acts_as_url.rb +++ b/lib/stringex/acts_as_url.rb @@ -1,5 +1,5 @@ -# encoding: UTF-8 -require "stringex/acts_as_url/adapter" + +require 'stringex/acts_as_url/adapter' module Stringex module ActsAsUrl # :nodoc: @@ -61,9 +61,7 @@ class << self define_method :acts_as_url_configuration do klass = self.class - while klass.acts_as_url_configuration.nil? - klass = klass.superclass - end + klass = klass.superclass while klass.acts_as_url_configuration.nil? klass.acts_as_url_configuration end end @@ -74,7 +72,6 @@ class << self acts_as_url_configuration.adapter.create_callbacks! self end - # Some ORMs function as mixins not base classes and need to have a hook to reinclude # and re-extend ActsAsUrl methods def included(base = nil, &block) @@ -97,9 +94,9 @@ def initialize_urls acts_as_url_configuration.adapter.initialize_urls! self end - # Renitialize the url fields for the all records. Designed for people who want to update - # acts_as_url support once there's already development/production data they'd - # like to keep around. + # Renitialize the url fields for the all records. Designed for people who + # want to update acts_as_url support once there's already + # development/production data they'd like to keep around. def reinitialize_urls acts_as_url_configuration.adapter.reinitialize_urls! self end From 16dca024ad7549f6cbd802d38dc09f953e3e74a3 Mon Sep 17 00:00:00 2001 From: Aashish Kiran Date: Wed, 14 Mar 2018 21:06:01 +0530 Subject: [PATCH 5/6] reverted commit --- lib/stringex/acts_as_url.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/stringex/acts_as_url.rb b/lib/stringex/acts_as_url.rb index 173bebea..6f4f7e6a 100644 --- a/lib/stringex/acts_as_url.rb +++ b/lib/stringex/acts_as_url.rb @@ -94,9 +94,9 @@ def initialize_urls acts_as_url_configuration.adapter.initialize_urls! self end - # Renitialize the url fields for the all records. Designed for people who - # want to update acts_as_url support once there's already - # development/production data they'd like to keep around. + # Renitialize the url fields for the all records. Designed for people who want to update + # acts_as_url support once there's already development/production data they'd + # like to keep around. def reinitialize_urls acts_as_url_configuration.adapter.reinitialize_urls! self end From c11da39d683b3ddfef3c0e8fece02eb70d518fb8 Mon Sep 17 00:00:00 2001 From: Aashish Kiran Date: Wed, 14 Mar 2018 21:10:22 +0530 Subject: [PATCH 6/6] updated commit --- lib/stringex/acts_as_url.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/stringex/acts_as_url.rb b/lib/stringex/acts_as_url.rb index 6f4f7e6a..2b51e201 100644 --- a/lib/stringex/acts_as_url.rb +++ b/lib/stringex/acts_as_url.rb @@ -1,5 +1,5 @@ - -require 'stringex/acts_as_url/adapter' +# encoding: UTF-8 +require "stringex/acts_as_url/adapter" module Stringex module ActsAsUrl # :nodoc: @@ -61,7 +61,9 @@ class << self define_method :acts_as_url_configuration do klass = self.class - klass = klass.superclass while klass.acts_as_url_configuration.nil? + while klass.acts_as_url_configuration.nil? + klass = klass.superclass + end klass.acts_as_url_configuration end end @@ -72,6 +74,7 @@ class << self acts_as_url_configuration.adapter.create_callbacks! self end + # Some ORMs function as mixins not base classes and need to have a hook to reinclude # and re-extend ActsAsUrl methods def included(base = nil, &block) @@ -94,9 +97,9 @@ def initialize_urls acts_as_url_configuration.adapter.initialize_urls! self end - # Renitialize the url fields for the all records. Designed for people who want to update - # acts_as_url support once there's already development/production data they'd - # like to keep around. + # Renitialize the url fields for the all records. Designed for people who + # want to update acts_as_url support once there's already + # development/production data they'd like to keep around. def reinitialize_urls acts_as_url_configuration.adapter.reinitialize_urls! self end