Permalink
Browse files

Add tumblr_guid to posts

Since I'm porting this blog over from a tumblr blog, I don't want people
to see all the same posts twice. RSS clients should (theoretically) be
using the guid of the RSS item to determine post uniqueness. My hope is
that if I can maintain the guid's that tumblr uses, readers won't get
notified that all posts are unread.

The guids will look like this if a tumblr_guid is present:

'http://blog.edwardloveall.com/posts/12345678'

and if no tumblr_guid is present, it will create this kind of guid using
the posts's slug:

'com.edwardloveall.blog.hello-world'

This hierarchical notation works well for apps, so I thought it may work
well for post guids. Each post's slug is both required and unique.
  • Loading branch information...
edwardloveall committed Aug 24, 2016
1 parent 366b325 commit c1f2de1b86b8802261991ff4b5f067b7c76b217d
Showing with 36 additions and 3 deletions.
  1. +8 −0 app/models/post.rb
  2. +5 −0 db/migrate/20160824190847_add_tumblr_guid_to_posts.rb
  3. +4 −3 db/schema.rb
  4. +19 −0 spec/models/post_spec.rb
View
@@ -4,4 +4,12 @@ class Post < ActiveRecord::Base
validates :slug, presence: true, uniqueness: true
scope :newest_first, lambda { order(created_at: :desc) }
def guid
if tumblr_guid.nil?
"com.edwardloveall.blog.#{slug}"
else
"http://blog.edwardloveall.com/post/#{tumblr_guid}"
end
end
end
@@ -0,0 +1,5 @@
class AddTumblrGuidToPosts < ActiveRecord::Migration
def change
add_column :posts, :tumblr_guid, :string
end
end
View
@@ -11,17 +11,18 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160822180105) do
ActiveRecord::Schema.define(version: 20160824190847) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "posts", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.text "body"
t.string "slug"
t.string "tumblr_guid"
end
create_table "projects", force: :cascade do |t|
View
@@ -19,4 +19,23 @@
expect(posts).to eq([post_a, post_c, post_b])
end
end
describe '#guid' do
context 'when tumblr_guid is present' do
it 'returns a tumblr-style guid' do
guid = '146221172067'
post = create(:post, tumblr_guid: guid)
expect(post.guid).to eq('http://blog.edwardloveall.com/post/146221172067')
end
end
context 'when no tumblr_guid is present' do
it 'returns a created guid' do
post = create(:post)
expect(post.guid).to eq("com.edwardloveall.blog.#{post.slug}")
end
end
end
end

0 comments on commit c1f2de1

Please sign in to comment.