Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

미리보기 링크를 추가합니다 #73

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ end
#gem 'wicked_pdf'
#gem 'wkhtmltopdf-binary'

# open graph
gem 'ogp', '~> 0.4.0'
gem 'faraday', '~> 0.9.2'

# excel
gem 'axlsx', '2.1.0.pre'
gem 'axlsx_rails'
Expand Down
17 changes: 16 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ GEM
sshkit (>= 1.6.1, != 1.7.0)
ancestry (3.0.7)
activerecord (>= 3.2.0)
ansi (1.5.0)
archive-zip (0.12.0)
io-like (~> 0.3.0)
arel (9.0.0)
ast (2.4.2)
autoprefixer-rails (9.7.2)
execjs
aws-eventstream (1.1.0)
Expand Down Expand Up @@ -1042,6 +1044,8 @@ GEM
execjs (2.7.0)
faker (1.7.3)
i18n (~> 0.5)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
ffi (1.11.3)
fugit (1.3.3)
et-orbi (~> 1.1, >= 1.1.8)
Expand Down Expand Up @@ -1118,13 +1122,19 @@ GEM
mini_portile2 (2.5.0)
minitest (5.14.4)
msgpack (1.3.1)
multipart-post (2.1.1)
net-scp (2.0.0)
net-ssh (>= 2.6.5, < 6.0.0)
net-ssh (5.2.0)
nio4r (2.5.7)
nokogiri (1.11.2)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
oga (2.15)
ast
ruby-ll (~> 2.1)
ogp (0.4.0)
oga (~> 2.15)
orm_adapter (0.5.0)
pg (1.1.4)
popper_js (1.14.5)
Expand Down Expand Up @@ -1180,6 +1190,9 @@ GEM
responders (3.0.0)
actionpack (>= 5.0)
railties (>= 5.0)
ruby-ll (2.1.2)
ansi
ast
ruby-vips (2.0.16)
ffi (~> 1.9)
ruby_dep (1.5.0)
Expand Down Expand Up @@ -1297,6 +1310,7 @@ DEPENDENCIES
devise
dotenv-rails
faker (~> 1.7.3)
faraday (~> 0.9.2)
haml-rails (~> 1.0)
health_check
http_accept_language
Expand All @@ -1308,6 +1322,7 @@ DEPENDENCIES
letter_opener_web
listen (>= 3.0.5, < 3.2)
mini_magick (~> 4.8)
ogp (~> 0.4.0)
pg
puma (~> 3.11)
rails (~> 5.2.5)
Expand Down Expand Up @@ -1337,4 +1352,4 @@ RUBY VERSION
ruby 2.6.6p146

BUNDLED WITH
2.2.6
2.2.14
5 changes: 4 additions & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def index
# GET /posts/1
# GET /posts/1.json
def show
@preview_link = PreviewLink.find_by_url(@post.preview_link)
end

# GET /posts/new
Expand Down Expand Up @@ -46,6 +47,8 @@ def create
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
CrawlingJob.perform_later(@post.preview_link) if @post.preview_link

respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
Expand Down Expand Up @@ -75,6 +78,6 @@ def set_post

# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:archive_id, :title, :cover, :body)
params.require(:post).permit(:archive_id, :title, :cover, :body, :preview_link)
end
end
12 changes: 12 additions & 0 deletions app/jobs/crawling_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CrawlingJob < ApplicationJob
queue_as :default

def perform(url)
link = PreviewLink.find_by_url(url)
unless link.present?
ogp = OGP::OpenGraph.new(Faraday.get(url)&.body)
link = PreviewLink.create(url: url, title: ogp.title, description: ogp.description, image: ogp.image.url)
end
logger.info "#{link.inspect}"
end
end
2 changes: 2 additions & 0 deletions app/models/preview_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class PreviewLink < ApplicationRecord
end
1 change: 1 addition & 0 deletions app/views/posts/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
= f.input :title
= f.input :cover
= f.input :body, as: "summernote"
= f.input :preview_link
= f.submit data: { disable_with: "Please wait..." }, class: 'btn btn-primary btn-full py-3'
9 changes: 9 additions & 0 deletions app/views/posts/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
.my-4
.body-content
= raw @post.body
- if @preview_link.present?
= link_to @preview_link.url do
.preview-link-card{ style: "display: block; width: 438px; max-width: 100%; border-radius: .85714em; border: 1px solid #e1e8ed; overflow: hidden; line-height: 1.3em;" }
.card-cover-image{ style: "background-image: url(#{@preview_link.image}); background-size: cover; height: 220px; border-bottom: 1px solid #e1e8ed;" }
.card-meta{ style: "box-sizing: border-box; padding: .75em; text-decoration: none;"}
.card-meta-title{ style: "display: block; border-collapse: separate; text-align: left; word-wrap: break-word;" }
= @preview_link.title
.card-meta-description{ style: "max-height: 2.6em; margin-top: .32333em; overflow: hidden" }
= @preview_link.description

- if @post.archive.present?
%section
Expand Down
8 changes: 1 addition & 7 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
adapter: postgresql
database: datapublic
database: datapublic_development_<%= %x[git rev-parse --abbrev-ref HEAD].strip %>
encoding: unicode
host: <%= ENV.fetch("DATABASE_HOST") {"localhost"} %>
<% if(ENV["DATABASE_USERNAME"] != nil) %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20210630134855_add_preview_link_to_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPreviewLinkToPosts < ActiveRecord::Migration[5.2]
def change
add_column :posts, :preview_link, :string
end
end
12 changes: 12 additions & 0 deletions db/migrate/20210717054410_create_preview_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreatePreviewLinks < ActiveRecord::Migration[5.2]
def change
create_table :preview_links do |t|
t.string :url
t.string :title
t.text :description
t.string :image

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_11_16_105613) do
ActiveRecord::Schema.define(version: 2021_07_17_054410) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -92,10 +92,20 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "archive_id"
t.string "preview_link"
t.index ["archive_id"], name: "index_posts_on_archive_id"
t.index ["user_id"], name: "index_posts_on_user_id"
end

create_table "preview_links", force: :cascade do |t|
t.string "url"
t.string "title"
t.text "description"
t.string "image"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "rumors", force: :cascade do |t|
t.string "title"
t.text "body"
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/preview_links.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
description: MyText

two:
title: MyString
description: MyText
7 changes: 7 additions & 0 deletions test/jobs/crawling_job_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CrawlingJobJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/preview_link_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class PreviewLinkTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end