From 2a334c201fe4ba3e821bf46f6bbc7ed4c6938e89 Mon Sep 17 00:00:00 2001 From: junv Date: Mon, 14 Sep 2015 20:41:33 +0800 Subject: [PATCH] #7 Add thumbnail to post, system will auto parse the thumbnail --- app/models/post.rb | 2 ++ app/models/thumbnail.rb | 2 ++ app/serializers/post_serializer.rb | 6 +++++- app/workers/process_post_worker.rb | 3 +++ db/migrate/20150914122528_add_post_id_to_thumbnail.rb | 5 +++++ db/schema.rb | 3 ++- 6 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20150914122528_add_post_id_to_thumbnail.rb diff --git a/app/models/post.rb b/app/models/post.rb index a02aa14..241adc6 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -5,5 +5,7 @@ class Post < ActiveRecord::Base has_many :favorites, as: :favoritable has_many :share_instances, as: :sharable + has_one :thumbnail + validates_presence_of :url, :type end diff --git a/app/models/thumbnail.rb b/app/models/thumbnail.rb index 2f30e3e..c27acf4 100644 --- a/app/models/thumbnail.rb +++ b/app/models/thumbnail.rb @@ -3,4 +3,6 @@ class Thumbnail < ActiveRecord::Base has_attached_file :file, :path => ":class/:attachment/:id/:basename.:extension" validates :file, :attachment_presence => true validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/ + + belongs_to :post end diff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rb index 99016d0..0fa58b0 100644 --- a/app/serializers/post_serializer.rb +++ b/app/serializers/post_serializer.rb @@ -1,5 +1,9 @@ class PostSerializer < ActiveModel::Serializer - attributes :viewed_times, :url, :title, :type, :processed, :good_format, :created_at, :likes_count, :comments_count, :summary + attributes :viewed_times, :url, :title, :type, :processed, :good_format, :created_at, :likes_count, :comments_count, + :summary, :thumbnail + def thumbnail + object.thumbnail.try(:file).try(:url) + end end diff --git a/app/workers/process_post_worker.rb b/app/workers/process_post_worker.rb index a26737f..ab3aad2 100644 --- a/app/workers/process_post_worker.rb +++ b/app/workers/process_post_worker.rb @@ -9,6 +9,9 @@ class ProcessPostWorker post.title = page.title post.summary = page.description post.processed = true + if page.images.best.present? + post.create_thumbnail file: URI.parse(page.images.best) + end post.save end diff --git a/db/migrate/20150914122528_add_post_id_to_thumbnail.rb b/db/migrate/20150914122528_add_post_id_to_thumbnail.rb new file mode 100644 index 0000000..51f6da0 --- /dev/null +++ b/db/migrate/20150914122528_add_post_id_to_thumbnail.rb @@ -0,0 +1,5 @@ +class AddPostIdToThumbnail < ActiveRecord::Migration + def change + add_column :thumbnails, :post_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 9be8d15..b52c63e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150914121114) do +ActiveRecord::Schema.define(version: 20150914122528) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -141,6 +141,7 @@ ActiveRecord::Schema.define(version: 20150914121114) do t.integer "file_file_size" t.datetime "file_updated_at" t.text "file_meta" + t.integer "post_id" end create_table "users", force: :cascade do |t|