Class: Abizvn::Cms::CmsService

Inherits:
Object
  • Object
show all
Defined in:
app/services/abizvn/cms/cms_service.rb

Constant Summary collapse

STATUS_PUBLIC =
'public'.freeze
STATUS_TABLE_NAME =
Abizvn::General::GeneralSetting.table_name
ARTICLE_TABLE_NAME =
Abizvn::Cms::Article.table_name

Class Method Summary collapse

Class Method Details

.find_article(id, status_code) ⇒ Object



24
25
26
# File 'app/services/abizvn/cms/cms_service.rb', line 24

def self.find_article(id, status_code)
  Article.joins(:status, :category).includes(:category).where("#{STATUS_TABLE_NAME}.code": status_code).friendly.find(id)
end

.get_articles_by_status(status_codes, filter_options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/abizvn/cms/cms_service.rb', line 9

def self.get_articles_by_status(status_codes, filter_options)
  category_id = filter_options[:category_id]
  article_id = filter_options[:article_id]
    
  query = Article.joins(:status).where("#{STATUS_TABLE_NAME}.code": status_codes).ordered
  
  if category_id.present?
    query = query.where(category_id: category_id)
  elsif article_id.present?
    query = query.where(category_id: Article.select(:category_id).where(slug: article_id))
  end
    
  return query
end

.get_categories_with_latest_articles(category_tag = nil, nb_articles = 5) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/abizvn/cms/cms_service.rb', line 28

def self.get_categories_with_latest_articles(category_tag = nil, nb_articles = 5)
  subquery = Article.joins(:status, :category)
              .select("#{ARTICLE_TABLE_NAME}.*, ROW_NUMBER() OVER (PARTITION BY category_id ORDER BY #{ARTICLE_TABLE_NAME}.updated_at DESC) as row_num")
              .where("#{STATUS_TABLE_NAME}.code": STATUS_PUBLIC)
              .to_sql
    
  articles = Article.select('*')
              .from("(#{subquery}) as articles_with_row_numbers")
              .where('row_num <= ?', nb_articles)
              .includes(:category)

  if category_tag.present?
    articles = articles.where("#{Category.table_name}.tag": category_tag)
  end
    
  categories = articles.map(&:category).uniq.sort { |c| c.order }
  
  return [categories, articles]
end