Skip to content

Instantly share code, notes, and snippets.

@rothanachoun
Created June 13, 2019 06:22
Show Gist options
  • Select an option

  • Save rothanachoun/bebeaf85a3f5e7147df1d92f41e75aef to your computer and use it in GitHub Desktop.

Select an option

Save rothanachoun/bebeaf85a3f5e7147df1d92f41e75aef to your computer and use it in GitHub Desktop.
# == Schema Information
#
# Table name: listing_posts
#
# id :integer not null, primary key
# title :string(255)
# content :text(65535)
# published :boolean default(TRUE)
# featured :boolean default(FALSE)
# featured_order :integer
# category_id :integer
# listing_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# format :integer
# account_id :integer
# meta_title :string(255)
# meta_description :string(255)
#
require_dependency 'base_model'
module News
class Post < ::BaseModel
self.table_name = 'posts'
include Notifiable
enum format: [:standard, :image, :video, :audio]
default_scope -> { order(created_at: :desc) }
belongs_to :category, class_name: 'News::Category'
belongs_to :author, class_name: 'Account', foreign_key: :account_id
has_one :cover, class_name: 'News::Image'
has_many :taggings, class_name: 'News::Tagging', dependent: :destroy
has_many :tags, class_name: 'News::Tag', through: :taggings
has_one :instant_article, class_name: 'Facebook::InstantArticle'
scope :featured, -> { where(feature: true).reorder(feature_order: :desc) }
scope :breakings, -> { where(breaking_news: true).reorder(breaking_news_order: :desc) }
scope :published, -> { where(status: 0) }
scope :unpublished, -> { where(status: 2) }
scope :title_like, -> (query) { where('LOWER(title) LIKE LOWER(?)', "%#{query}%") }
scope :by_category, -> (category_id) { where(category_id: category_id) }
scope :by_status, -> (status) { where(status: status) }
def self.search(query, params = {})
relation = all
relation = relation.where("LOWER(posts.title) LIKE LOWER(?)", "%#{query}%") if query.present?
relation = relation.by_category(params[:category_id]) if params[:category_id].present?
relation = relation.by_status(params[:status]) if params[:status].present?
relation
end
def self.related_with(post)
if post.tags.present?
News::Post.joins(:taggings).where(taggings: { tag_id: post.tag_ids}).where('posts.id != ?', post.id).order(created_at: :desc).uniq
elsif post.category.present?
return News::Post.where(category_id: post.category_id).where.not(id: post.id).order(created_at: :desc).uniq
else
[]
end
end
def published?
status == 0
end
def instant_article?
instant_article.present?
end
def instant_article_html_source
doc = Nokogiri::HTML(self.content)
doc.css('img').each do |img|
figure = doc.create_element('figure')
image = doc.create_element('img')
image['src'] = "#{ENV['DOMAIN']}#{img.attributes['src'].value}"
figure.children = image
img.replace figure.to_html
end
doc.css('iframe').each do |frm|
figure = doc.create_element('figure', class: 'op-interactive')
iframe = doc.create_element('iframe')
iframe['src'] = frm.attributes['src'].value
iframe['width'] = frm.attributes['width'].value
iframe['height'] = frm.attributes['height'].value
figure.children = iframe
frm.replace figure.to_html
end
doc.css('p').each do |p|
if p.children.search('img').present?
p.replace p.children.search('figure').to_html
end
if p.children.search('iframe').present?
p.replace p.children.search('figure').to_html
end
end
doc.css('b').find_all{|b| all_children_are_blank?(b) }.each do |b|
b.remove
end
doc.css('p').find_all{|p| all_children_are_blank?(p) }.each do |p|
p.remove
end
doc.search('body').children.to_html
end
def is_blank?(node)
(node.text? && node.content.strip == '') || (node.element? && node.name == 'br')
end
def all_children_are_blank?(node)
node.children.all?{|child| is_blank?(child) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment