using permalinks to access articles without specifying the date in typo
I recently switched from mephisto to typo, mostly due to lack of activity and features in the mephisto project.
With mephisto, I had to make a modification to allow me to use permalinks without the date to access articles. For example, so I could say http://nopugs.com/permalinks-without-dates instead of http://nopugs.com/2008/09/11/permalinks-without-dates I’m not sure why this isn’t already a feature of such blogging systems. I’m somewhat new to blogging so perhaps there’s a good reason. Maybe there are sometimes different articles with the same permalink (I don’t know why somebody would do this.)
Regardless, here’s how I modified typo to allow me to do this.
In app/controllers/redirect_controller.rb change the redirect method from this:
def redirect
if (params[:from].first == 'articles')
path = request.path.sub('/articles', '')
url_root = request.relative_url_root
path = url_root + path unless url_root.nil?
redirect_to path, :status => 301
return
end
r = Redirect.find_by_from_path(params[:from].join("/"))
if(r)
path = r.to_path
url_root = request.relative_url_root
path = url_root + path unless url_root.nil? or path[0,url_root.length] == url_root
redirect_to path, :status => 301
else
render :text => "Page not found", :status => 404
end
endto this:
def redirect
if (params[:from].first == 'articles')
path = request.path.sub('/articles', '')
url_root = request.relative_url_root
path = url_root + path unless url_root.nil?
redirect_to path, :status => 301
return
end
article = Article.find_by_permalink(params[:from].first)
if article
redirect_to article_path(article)
return
end
r = Redirect.find_by_from_path(params[:from].join("/"))
if(r)
path = r.to_path
url_root = request.relative_url_root
path = url_root + path unless url_root.nil? or path[0,url_root.length] == url_root
redirect_to path, :status => 301
else
render :text => "Page not found", :status => 404
end
end
Then in app/models/article.rb change findbypermalink from this:
def self.find_by_permalink(year, month=nil, day=nil, title=nil)
unless month
case year
when Hash
year, month, day, title = date_from(year)
when Array
year, month, day, title = year
end
end
date_range = self.time_delta(year, month, day)
find_published(:first,
:conditions => { :permalink => title,
:published_at => date_range }) \
or raise ActiveRecord::RecordNotFound
endto this:
def self.find_by_permalink(year, month=nil, day=nil, title=nil)
unless month
case year
when Hash
year, month, day, title = date_from(year)
when Array
year, month, day, title = year
end
end
if year && !month && !day && !title
year, title = title, year
end
published = nil
if year
date_range = self.time_delta(year, month, day)
published = find_published(:first,
:conditions => { :permalink => title,
:published_at => date_range })
end
unless published
published = find_published(:first, :conditions => {:permalink => title})
end
raise ActiveRecord::RecordNotFound unless published
published
endThen you should be able to leave the year/month/day off of your article URLs when sharing them with friends and such.
Enjoy!
Posted in typo | no comments |