Skip to content

Commit

Permalink
add a little daily articles counter to sources#index
Browse files Browse the repository at this point in the history
  • Loading branch information
pessi-v committed Jun 15, 2024
1 parent 2c1b82a commit 226f897
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/controllers/sources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions app/views/sources/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 226f897

Please sign in to comment.