From 226f897e801bd7f12c4db960212148334d6b9d89 Mon Sep 17 00:00:00 2001 From: pessi-v Date: Sat, 15 Jun 2024 15:18:08 +0200 Subject: [PATCH] add a little daily articles counter to sources#index --- app/controllers/sources_controller.rb | 10 ++++++++++ app/models/article.rb | 4 ++++ app/views/sources/index.html.slim | 11 +++++++++++ 3 files changed, 25 insertions(+) diff --git a/app/controllers/sources_controller.rb b/app/controllers/sources_controller.rb index cc01f49..5b3974c 100644 --- a/app/controllers/sources_controller.rb +++ b/app/controllers/sources_controller.rb @@ -4,6 +4,16 @@ class SourcesController < ApplicationController # GET /sources or /sources.json def index @sources = Source.all.order(name: :asc) + @article_counts_by_day = [ + Article.today.count, + Article.yesterday.count, + Article.days_ago(2).count, + Article.days_ago(3).count, + Article.days_ago(4).count, + Article.days_ago(5).count, + Article.days_ago(6).count, + Article.days_ago(7).count + ] end # GET /sources/1 or /sources/1.json diff --git a/app/models/article.rb b/app/models/article.rb index 4eb8863..61e4f3c 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -8,4 +8,8 @@ class Article < ApplicationRecord against: %i[title source_name readability_output], using: { tsearch: { prefix: true } }, # tsearch = full text search ignoring: :accents + + scope :today, -> { where('DATE(published_at) = CURRENT_DATE') } + scope :yesterday, -> { where('DATE(published_at) = CURRENT_DATE - 1') } + scope :days_ago, ->(days) { where("DATE(published_at) = CURRENT_DATE - #{days}") } end diff --git a/app/views/sources/index.html.slim b/app/views/sources/index.html.slim index 64fe1a0..b97b45a 100644 --- a/app/views/sources/index.html.slim +++ b/app/views/sources/index.html.slim @@ -11,6 +11,17 @@ = link_to "New source", new_source_path + div id="article_counts" + table + - (Date.current - 7.days .. Date.current).to_a.reverse.each_with_index do |date, index| + tr + th + p + = date.strftime("%A %d.") + th + p + = @article_counts_by_day[index] + div id="sources" .list_view_desktop - @sources.each do |source|