Class: Post

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
PgSearch, TranslationCms::Taggable, TranslationCms::WhiteList
Defined in:
app/models/post.rb

Overview

Schema Information

Table name: posts

id                       :integer          not null, primary key
slug                     :string(255)
tags                     :string(255)      default([])
published_at             :datetime
import_id                :integer
created_at               :datetime
updated_at               :datetime
title_translations       :hstore           default({})
content_translations     :hstore           default({})
description_translations :hstore           default({})
notes_translations       :hstore           default({})

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TranslationCms::Taggable

#cached_meta_tags, #meta_tag

Class Method Details

.filter(params) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'app/models/post.rb', line 69

def filter(params)
  query = all.includes(:picture)

  posts = query.past.page(params[:page]).per(10)
  posts = posts.with_tag(params[:tag]) if params[:tag].present?
  posts = posts.search_by_all(params[:q]) if params[:q].present?

  posts
end

.sunrise_search(params) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'app/models/post.rb', line 59

def sunrise_search(params)
  query = scoped

  title = params[:title] || params[:q]
  query = query.search_by_all(title) if title.present?
  query = query.by_date(Time.zone.parse(params[:published_at])) if params[:published_at].present?

  query
end

Instance Method Details

#joined_tagsObject



88
89
90
# File 'app/models/post.rb', line 88

def joined_tags
  (tags || []).join(',')
end

#joined_tags=(values) ⇒ Object



84
85
86
# File 'app/models/post.rb', line 84

def joined_tags=(values)
  self.tags = values.split(',').map(&:strip)
end

#published?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'app/models/post.rb', line 92

def published?
  published_at <= Time.zone.now
end


102
103
104
105
106
# File 'app/models/post.rb', line 102

def related_by_ids
  return [] if related_ids.blank?

  Post.includes(:cover).past.where(id: related_ids).limit(3)
end


96
97
98
99
100
# File 'app/models/post.rb', line 96

def related_by_tags
  posts = Post.includes(:cover).past.with_any_tags(tags).unless_id(id)
  posts.sort_by { |post| (tags & post.tags).size }
  posts.limit(3)
end

#to_paramObject



80
81
82
# File 'app/models/post.rb', line 80

def to_param
  slug
end