Various changes since last time, such as adding user login authentication, with the following in the User model, which seems to mimic the WordPress authentication (at least for my password):
def check_password(plaintext_password)
require 'phpass'
return Phpass.new(8).check(plaintext_password, self.user_pass)
end
Thanks to Aaron Hill for a helpful post on that topic.
Biggest change has been the introduction of permalinks, and making it possible to view each blog post on a separate page. Within the Post model, I added the following:
def self.find_by_permalink(permalink)
year, month, day, name = permalink.split('/')
name ||= ''
find(:first, :conditions => ["YEAR(post_date) = ? AND MONTH(post_date) = ? AND DAYOFMONTH(post_date) = ? AND post_name = ?", year.to_i, month.to_i, day.to_i, name])
end
def to_param
permalink
end
def permalink
post_date.strftime("%Y/%m/%d") + (post_name.empty? ? '' : "/#{post_name}")
end
Thanks to the author of this snippet for some of that.
Then in routes.rb
, I have:
resources :posts, :only => [:index, :new, :create]
resources :posts, :only => [:show, :update, :destroy, :edit], :path => '', :id => /\d{4}\/\d{2}\/\d{2}(\/[0-9a-z\-_]+)?/
This preserves mydomain.com/posts
and mydomain.com/posts/new
URLs for listing and creating posts, but allows mydomain.com/2013/09/04/my-post
as a permalink for the post itself (allowing lower-case letters, numbers, hyphens and underscores).
(I’ll leave the smileys in, just to show how clever WordPress is…)
Finally, within PostsController
, replace all the Post.find
occurrences with Post.find_by_permalink
, and I think that’s all!