Skip to content

Commit

Permalink
Update for 2.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
robyurkowski committed Mar 5, 2012
1 parent d7d0d52 commit 9f718f4
Showing 1 changed file with 16 additions and 20 deletions.
Expand Up @@ -8,46 +8,42 @@ This guide will show you how to:

endprologue.

WARNING: This method is tested with Refinery CMS v. 1.0.8 only, but should be applicable to other versions. Always ensure your code is backed up or under version control before making substantial change.
WARNING: This method is tested with Refinery CMS v. 2.0.0 only. It should be applicable to later versions, but not earlier versions (1.0.9 or earlier). Always ensure your code is backed up or under version control before making substantial change.

h3. Implementing Cached Slugs
h3. Fresh Slugs

FriendlyID requires that you create a string field entitled +cached_slug+ on any model that you wish to convert. It's better to do this as a separate migration after creating the extension, because you will have to remove the administrative text fields Refinery CMS auto-generates.
FriendlyID requires that you create a string field entitled +slug+ on any model that you wish to convert. It's better to do this as a separate migration after creating the extension, because you don't want Refinery to auto-generate a field in the back-end; this attribute is set by code, not by hand.

To do so, create a new migration file in the +db/migrate+ folder of your extension. Since this depends on the previous migration, it's best to rename the other migrations to include a sequence reference in the filename -- for example, +create_staff_members.rb+ becomes +001_create_staff_members.rb+. When you re-run the generator that will move these migrations over to your main +db/migrate+ folder, they will be moved in this order. Don't worry if you've already run these migrations; Rails will auto-detect that it has been run, even though the filename has been changed.
To do so, create a new migration file in the +db/migrate+ folder of your extension. Since this depends on the previous migration, it's best to rename the other migrations to include a sequence reference in the filename -- for example, +create_staff_members.rb+ becomes +001_create_staff_members.rb+. When you re-run the generator that will move these migrations over to your main +db/migrate+ folder, they will be moved in this order. Don't worry if you've already run these migrations; Rails will auto-detect that you've run them, even though the filename has been changed, and it won't re-run them.

Inside your migration, write something like this:
<ruby>
class AddCachedSlugToStaffMembers < ActiveRecord::Migration
def self.up
add_column :staff_members, :cached_slug, :string
end

def self.down
remove_column :staff_members, :cached_slug
class AddSlugToStaffMembers < ActiveRecord::Migration
def change
add_column :staff_members, :slug, :string
add_index :staff_members, :slug
end
end
</ruby>

Run the generator that you ran initially to create your extension, and then migrate.. In the above example, since the extension is named +staff_members+, one would run:
Run the generator that you ran initially to create your extension, and then migrate. In the above example, since the extension is named +staff_members+, one would run:

<shell>
$ rails generate refinerycms_staff_members
$ rails generate refinery:staff_members
$ rake db:migrate
$ rake db:seed
</shell>

h3. Adding Friendly ID

In your model, add the following lines, replacing +staff_member+ where appropriate:
In your model, add the following lines after the opening +class+ line:

<ruby>
has_friendly_id :name, :use_slug => true,
:default_locale => (::Refinery::I18n.default_frontend_locale rescue :en),
:approximate_ascii => RefinerySetting.find_or_set(:approximate_ascii, false, :scoping => 'staff_member'),
:strip_non_ascii => RefinerySetting.find_or_set(:strip_non_ascii, false, :scoping => 'staff_member')
extend FriendlyId
friendly_id :title, :use => [:slugged]
</ruby>

If you want to use a different field than +name+ from which to generate the URL, be sure to change the first symbol after +has_friendly_id+ to be in accordance (for example, many extensions use +title+ instead).
If you want to use a different field than +title+ from which to generate the URL, be sure to change the first symbol after +friendly_id+ to be in accordance (for example, many extensions use +name+ instead).

Also be sure to remove any +to_param+ method that exists, or this will interfere.

Expand All @@ -61,4 +57,4 @@ Each entry must be re-saved to generate its slug. You can do this quickly from t
StaffMember.all.map(&:save)
</ruby>

If all entries in the resulting array return true, everything is complete! If you have an entry return false, you may have some individual issues that prevent saving, and you can update these manually using the Refinery CMS administrative interface.
If all entries in the resulting array return true, everything is complete! If you have an entry return false, you may have some individual issues that prevent saving, and you can update these manually using the Refinery CMS administrative interface.

0 comments on commit 9f718f4

Please sign in to comment.