Skip to content

Commit 54de5d2

Browse files
committed
url data can be reinitialized
1 parent a637b6b commit 54de5d2

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.rdoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ In order to use the generated url attribute, you will probably want to override
4646

4747
Routing called via named routes like <tt>foo_path(@foo)</tt> will automatically use the url. In your controllers you will need to call <tt>Foo.find_by_url(params[:id])</tt> instead of the regular find. Don't look for <tt>params[:url]</tt> unless you set it explicitly in the routing, <tt>to_param</tt> will generate <tt>params[:id]</tt>.
4848

49-
Note that if you add <tt>acts_as_url</tt> to an existing model, the <tt>url</tt> database column will initially be blank. To set this column for your existing instances, you can use the <tt>initialize_urls</tt> method. So if your class is <tt>Post</tt>, just say <tt>Post.initialize_urls</tt>.
49+
Note that if you add <tt>acts_as_url</tt> to an existing model, the <tt>url</tt> database column will initially be blank.
50+
To set this column for your existing instances, you can use the <tt>initialize_urls</tt> method. So if your class is <tt>Post</tt>, just say <tt>Post.initialize_urls</tt>.
51+
52+
To update the existing <tt>url</tt> database column with data, you can use <tt>reinitialize_urls</tt> method. So if your class is <tt>Post</tt>, just say <tt>Post.reinitialize_urls</tt>.
5053

5154
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:
5255

lib/stringex/acts_as_url.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ def included(base = nil, &block)
9696
def initialize_urls
9797
acts_as_url_configuration.adapter.initialize_urls! self
9898
end
99+
100+
# Renitialize the url fields for the all records. Designed for people who want to update
101+
# <tt>acts_as_url</tt> support once there's already development/production data they'd
102+
# like to keep around.
103+
def reinitialize_urls
104+
acts_as_url_configuration.adapter.reinitialize_urls! self
105+
end
99106
end
100107

101108
module ActsAsUrlInstanceMethods

lib/stringex/acts_as_url/adapter/base.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def initialize_urls!(klass)
3333
end
3434
end
3535

36+
def reinitialize_urls!(klass)
37+
self.klass = klass
38+
klass.update_all(settings.url_attribute => nil)
39+
initialize_urls!(klass)
40+
end
41+
3642
def url_attribute(instance)
3743
# Retrieve from database record if there are errors on attribute_to_urlify
3844
if !is_new?(instance) && is_present?(instance.errors[settings.attribute_to_urlify])

test/unit/acts_as_url_integration_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,21 @@ def test_should_mass_initialize_urls
224224
assert_equal "subsequent", @other_doc.url
225225
end
226226

227+
def test_should_mass_reinitialize_urls
228+
@doc = Document.create(title: "Initial")
229+
@other_doc = Document.create(title: "Subsequent")
230+
# Just making sure this got set before the reinitialize urls test
231+
assert_equal "initial", @doc.url
232+
assert_equal "subsequent", @other_doc.url
233+
234+
Document.reinitialize_urls
235+
236+
@doc.reload
237+
@other_doc.reload
238+
assert_equal "initial", @doc.url
239+
assert_equal "subsequent", @other_doc.url
240+
end
241+
227242
def test_should_mass_initialize_urls_with_custom_url_attribute
228243
Document.class_eval do
229244
# Manually undefining the url method on Document which, in a real class not reused for tests,

0 commit comments

Comments
 (0)