Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pagy calendar #2

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ group :test do
end

gem "tailwindcss-rails", "~> 2.1"

gem "pagy", github: "ddnexus/pagy", branch: "master"
26 changes: 17 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
GIT
remote: https://github.com/ddnexus/pagy.git
revision: 2d313e8156c209aff9ef3177c54c477e4468e195
branch: master
specs:
pagy (6.2.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -106,7 +113,7 @@ GEM
activesupport (>= 6.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
importmap-rails (1.2.3)
importmap-rails (2.0.1)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
railties (>= 6.0.0)
Expand All @@ -131,14 +138,14 @@ GEM
minitest (5.20.0)
msgpack (1.7.2)
mutex_m (0.2.0)
net-imap (0.4.9)
net-imap (0.4.9.1)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.2)
timeout
net-smtp (0.4.0)
net-smtp (0.4.0.1)
net-protocol
nio4r (2.7.0)
nokogiri (1.16.0-aarch64-linux)
Expand All @@ -150,7 +157,7 @@ GEM
psych (5.1.2)
stringio
public_suffix (5.0.4)
puma (6.4.0)
puma (6.4.1)
nio4r (~> 2.0)
racc (1.7.3)
rack (3.0.8)
Expand Down Expand Up @@ -198,7 +205,7 @@ GEM
redis-client (0.19.1)
connection_pool
regexp_parser (2.8.3)
reline (0.4.1)
reline (0.4.2)
io-console (~> 0.5)
rexml (3.2.6)
ruby2_keywords (0.0.5)
Expand All @@ -217,14 +224,14 @@ GEM
sqlite3 (1.7.0-aarch64-linux)
sqlite3 (1.7.0-arm64-darwin)
sqlite3 (1.7.0-x86_64-linux)
stimulus-rails (1.3.0)
stimulus-rails (1.3.3)
railties (>= 6.0.0)
stringio (3.1.0)
tailwindcss-rails (2.1.0-aarch64-linux)
tailwindcss-rails (2.2.0-aarch64-linux)
railties (>= 6.0.0)
tailwindcss-rails (2.1.0-arm64-darwin)
tailwindcss-rails (2.2.0-arm64-darwin)
railties (>= 6.0.0)
tailwindcss-rails (2.1.0-x86_64-linux)
tailwindcss-rails (2.2.0-x86_64-linux)
railties (>= 6.0.0)
thor (1.3.0)
timeout (0.4.1)
Expand Down Expand Up @@ -259,6 +266,7 @@ DEPENDENCIES
debug
importmap-rails
jbuilder
pagy!
puma (>= 5.0)
rails (~> 7.1.2)
redis (>= 4.0.1)
Expand Down
76 changes: 76 additions & 0 deletions app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,79 @@
}

*/

@import "~tailwindcss/base";
/*
Add the following markup AFTER your import statements
Notice: this style contains all the rules for pagy-nav, pagy-nav-js and pagy-combo-nav-js
*/

.pagy-nav,
.pagy-nav-js {
@apply flex space-x-2;
}

.pagy-nav .page a,
.pagy-nav .page.active,
.pagy-nav .page.prev.disabled,
.pagy-nav .page.next.disabled,
.pagy-nav-js .page a,
.pagy-nav-js .page.active,
.pagy-nav-js .page.prev.disabled,
.pagy-nav-js .page.next.disabled {
@apply block rounded-lg px-3 py-1 text-sm text-gray-500 font-semibold bg-gray-200 shadow-md;
&:hover{
@apply bg-gray-300;
}
&:active{
@apply bg-gray-400 text-white;
}
}

.pagy-nav .page.prev.disabled,
.pagy-nav .page.next.disabled,
.pagy-nav-js .page.prev.disabled,
.pagy-nav-js .page.next.disabled {
@apply text-gray-400 cursor-default;
&:hover {
@apply text-gray-400 bg-gray-200;
}
&:active {
@apply text-gray-400 bg-gray-200;
}
}

.pagy-nav .page.active,
.pagy-nav-js .page.active {
@apply text-white cursor-default bg-gray-400;
&:hover {
@apply text-white bg-gray-400;
}
&:active {
@apply bg-gray-400 text-white;
}
}


.pagy-combo-nav-js {
@apply flex max-w-max rounded-full px-3 py-1 text-sm text-gray-500 font-semibold bg-gray-200 shadow-md;
}

.pagy-combo-nav-js .pagy-combo-input {
@apply bg-white px-2 rounded-sm
}

.pagy-combo-nav-js .page.prev,
.pagy-combo-nav-js .page.next {
&:hover {
@apply text-gray-800;
}
&:active {
@apply text-gray-800;
}
}

.pagy-combo-nav-js .page.prev.disabled,
.pagy-combo-nav-js .page.next.disabled {
@apply text-gray-400 cursor-default;
}
15 changes: 15 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
class ApplicationController < ActionController::Base
# enable pagy backend helpers globally
include Pagy::Backend

# start and end of calendar (first and last record in the list)
def pagy_calendar_period(collection)
# collection.minmax.map(&:start_date)
start_date = collection.min_by(&:start_date).start_date
end_date = Time.zone.now
[start_date, end_date]
end

# query to paginate within start_date
def pagy_calendar_filter(collection, from, to)
collection.where(start_date: from..to)
end
end
17 changes: 15 additions & 2 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ class EventsController < ApplicationController
before_action :set_event, only: %i[ show edit update destroy ]

def index
@events = Event.all
# @events = Event.all
collection = Event.all.order(start_date: :asc)
@calendar, @pagy, @events = pagy_calendar(collection,
year: { size: [1, 1, 1, 1] },
month: { size: [0, 12, 12, 0], format: '%b' },
week: { size: [0, 53, 53, 0], format: '%W' },
day: { size: [0, 31, 31, 0], format: '%d' },
pagy: { items: 10 }, # items per page
active: !params[:skip]
)
end

def show
Expand All @@ -20,7 +29,11 @@ def create

respond_to do |format|
if @event.save
format.html { redirect_to event_url(@event), notice: "Event was successfully created." }
format.html do
flash[:notice] = "Event was successfully created."
redirect_to event_url(@event)
# redirect_to events_path(pagy_calendar_url_at(@calendar, @event.start_date))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that to make this redirect line work, we need to define @calendar and all the collection here.

if the url params were fixed

# bad
http://localhost:3000/events?year_page=10&month_page=10&day_page=5
# good
http://localhost:3000/events?year_page=2023&month_page=10&day_page=5

the calendar pagination would be perfect

Copy link

@ddnexus ddnexus Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that to make this redirect line work, we need to define @Calendar and all the collection here.

Yes, the @calendar should be created in the controller

if the url params were fixed ... the calendar pagination would be perfect

IIUC you would like to use params to get to any page by time, instead by number.

To me that looks actually more complicated. If you use pagy_calendar_url_at(@calendar, time) you get directly the path/url at that time instead of tinkering with 3 params separately.

Am I missing something?

end
else
format.html { render :new, status: :unprocessable_entity }
end
Expand Down
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module ApplicationHelper
include Pagy::Frontend
end
3 changes: 2 additions & 1 deletion app/views/events/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

<div>
<%= form.label :start_date, style: "display: block" %>
<%= form.datetime_field :start_date %>
<%#= form.datetime_field :start_date %>
<%= form.datetime_field :start_date, value: params[:start_date].to_date.strftime('%Y-%m-%dT%H:%M:%S') || @object.start_date %>
</div>

<div>
Expand Down
39 changes: 35 additions & 4 deletions app/views/events/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
<p style="color: green"><%= notice %></p>

<h1>Events</h1>
<%= link_to "New event", new_event_path %>
<%#= link_to "New event", new_event_path %>
<%= link_to "New event", new_event_path(start_date: [@calendar[:day].label, @calendar[:month].label(format: '%m-%Y')].join('-')) %>
yshmarov marked this conversation as resolved.
Show resolved Hide resolved

<div id="events" class="space-y-4">
<% @events.each do |event| %>
<%= render event %>
<div>
<% if params[:skip] %>
<%= link_to 'Show Calendar', events_path %>
<% else %>
<%= link_to 'Hide Calendar', events_path(skip: true) %>
<%= link_to 'Today', pagy_url_for(@calendar.send(:last_object_at, Time.zone.now), 1) %>
<%#= link_to 'Today (does not work)', pagy_calendar_url_at(@calendar, Time.zone.now) %>
yshmarov marked this conversation as resolved.
Show resolved Hide resolved
<% end %>
</div>

<h1>Events</h1>

<% if @calendar %>
<%== pagy_info(@pagy) %>
for
<%#== @calendar[:year].label %>
<%== @calendar[:day].label %>
<%== @calendar[:month].label(format: '%B %Y') %>
<%#== @calendar[:week].label %>
yshmarov marked this conversation as resolved.
Show resolved Hide resolved
<%== pagy_nav(@calendar[:year]) %>
<%== pagy_nav(@calendar[:month]) %>
<%== pagy_nav(@calendar[:week]) %>
<%== pagy_nav(@calendar[:day]) %>
<% end %>

<%== pagy_nav(@pagy) %>

<hr>
<% if @events.any? %>
<% @events.each do |event| %>
<%= render 'event', event: event %>
<% end %>
<% elsif @events.empty? %>
No events found
<% end %>
1 change: 1 addition & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'pagy/extras/calendar'