#7 Add thumbnail to post, system will auto parse the thumbnail

This commit is contained in:
2015-09-14 20:41:33 +08:00
parent 57a420f119
commit 2a334c201f
6 changed files with 19 additions and 2 deletions
+2
View File
@@ -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
+2
View File
@@ -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
+5 -1
View File
@@ -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
+3
View File
@@ -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
@@ -0,0 +1,5 @@
class AddPostIdToThumbnail < ActiveRecord::Migration
def change
add_column :thumbnails, :post_id, :integer
end
end
+2 -1
View File
@@ -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|