Browse Source

Filtering on dates for existing diffs to show

master
Norore 4 years ago
parent
commit
4e23a17bcc
  1. 12
      app/controllers/diffs_controller.rb
  2. 10
      app/models/diff.rb
  3. 15
      app/views/diffs/show.html.erb

12
app/controllers/diffs_controller.rb

@ -4,13 +4,15 @@ class DiffsController < ApplicationController
return redirect_to action: :show, id: last.created_at.to_date if last
end
DATE_DELTA = 3
def show
@dates = Diff.select(:created_at).distinct
@all_dates = []
@dates.each do |d|
@all_dates.push(d.created_at.to_date)
end
@date = Date.parse params[:id]
dates = Diff.dates
current = dates.index @date
min = [0, current - DATE_DELTA].max
max = current + DATE_DELTA
@dates = dates[min..max]
@diffs = Diff.where created_at: @date..@date+1
end
end

10
app/models/diff.rb

@ -1,3 +1,13 @@
class Diff < ApplicationRecord
belongs_to :site
def self.dates
sql = <<-SQL
SELECT DISTINCT date_trunc('day', created_at) AS date
FROM #{self.table_name}
ORDER BY date ASC
SQL
ActiveRecord::Base.connection.execute(sql)
.collect { |r| Date.parse r['date'] }
end
end

15
app/views/diffs/show.html.erb

@ -1,15 +1,8 @@
<div class="autogrid txtcenter">
<% (-3..3).each do |n|
date = @date + n %>
<% if @all_dates.include?(date) %>
<%= content_tag :div, class: (:current if @date == date) do %>
<%= link_to l(date), diff_path(date) %>
<% end %>
<% else %>
<%= content_tag :div, class: (:nodata) do %>
<%= link_to l(date), diff_path(date) %>
<%end %>
<% end %>
<% @dates.each do |date| %>
<%= content_tag :div, class: (:current if @date == date) do
link_to l(date), diff_path(date)
end %>
<% end %>
</div>

Loading…
Cancel
Save