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

url data can be reinitialized #202

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ In order to use the generated url attribute, you will probably want to override

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>.

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>.
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>.

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>.

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:

Expand Down
7 changes: 7 additions & 0 deletions lib/stringex/acts_as_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [94/80]

# <tt>acts_as_url</tt> support once there's already development/production data they'd
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [92/80]

# like to keep around.
def reinitialize_urls
acts_as_url_configuration.adapter.reinitialize_urls! self
end
end

module ActsAsUrlInstanceMethods
Expand Down
6 changes: 6 additions & 0 deletions lib/stringex/acts_as_url/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
15 changes: 15 additions & 0 deletions test/unit/acts_as_url_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down