#33 Add ability to let admin add css selector to grab content from page.

This commit is contained in:
2015-11-26 00:18:05 +08:00
parent 429be8cffa
commit a801b09c41
5 changed files with 33 additions and 2 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ ActiveAdmin.register Post do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
permit_params :blocked, :processed, :good_format, :type
permit_params :blocked, :processed, :good_format, :type, :css_selector, :type, :url
#
# or
#
+10
View File
@@ -1,4 +1,14 @@
class Article < Post
before_update :update_content
private
def update_content
if self.changed? && self.css_selector_changed?
PostContentWorker.perform_async self.id, self.url, self.css_selector
end
end
end
+15
View File
@@ -0,0 +1,15 @@
class PostContentWorker
include Sidekiq::Worker
sidekiq_options queue: :post, retry: 3
def perform id, url, css_selector
post = Post.find id
page = Nokogiri::HTML RestClient.get(url)
content_html = page.css(css_selector)[0].try(:to_html)
post.title = page.css("title").text if post.title.nil?
post.content = content_html
post.good_format = true if post.type != 'WebPage'
post.save!
end
end
@@ -0,0 +1,5 @@
class AddCssSlectorToArticle < ActiveRecord::Migration
def change
add_column :posts, :css_selector, :string, default: nil
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: 20151125145909) do
ActiveRecord::Schema.define(version: 20151125153022) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -146,6 +146,7 @@ ActiveRecord::Schema.define(version: 20151125145909) do
t.integer "bookmarks_count", default: 0
t.datetime "deleted_at"
t.boolean "blocked", default: false
t.string "css_selector"
end
add_index "posts", ["deleted_at"], name: "index_posts_on_deleted_at", using: :btree