Skip to content

Commit

Permalink
Hobo models -- have an SEO friendly URL (#to_param) by default on any…
Browse files Browse the repository at this point in the history
… model with a name attribute
  • Loading branch information
tslocke committed Jun 26, 2008
1 parent 0467688 commit 781874d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions hobo/lib/hobo/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,17 @@ def has_many_with_join_record_management(name, options={}, &b)
def to_url_path
"#{self.class.to_url_path}/#{to_param}" unless new_record?
end


def to_param
name_attr = self.class.name_attribute
if name_attr
readable = send(name_attr).to_s.downcase.gsub(/[^a-z0-9]+/, '-').gsub(/-+$/, '').gsub(/^-+$/, '').split('-')[0..5].join('-')
@to_param ||= "#{id}-#{readable}"
else
id
end
end


def with_acting_user(user)
Expand Down

0 comments on commit 781874d

Please sign in to comment.