Skip to content

Commit

Permalink
[FS-12] Create agenda app panel (#34)
Browse files Browse the repository at this point in the history
* [FS-12] Bugfix: flaky reservation test fix

* [FS-12] Add agenda app panel
  • Loading branch information
beetlegius-jt authored Jan 28, 2025
1 parent 81591fa commit 986f3df
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 2 deletions.
23 changes: 23 additions & 0 deletions app/components/app/agenda_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<nav class="nav nav-pills nav-fill" role="tablist">
<% @weekdays.each do |day| %>
<%= button_tag day.strftime("%A"), class: ["nav-link", ("active" if day.today?)], data: { bs_toggle: :tab, bs_target: "##{day}" } %>
<% end %>
</nav>

<hr>

<div class="tab-content">
<% @grouped_events.each_pair do |day, events| %>
<%= tag.div id: day, class: ["tab-pane", ("active" if day.today?)], role: :tabpanel do %>
<ul class="list-group">
<% events.each do |event| %>
<li class="list-group-item d-flex justify-content-between lh-lg align-items-center">
<%= event.activity_name %>

<small><%= event.start_time %> - <%= event.end_time %></small>
</li>
<% end %>
</ul>
<% end %>
<% end %>
</div>
8 changes: 8 additions & 0 deletions app/components/app/agenda_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module App
class AgendaComponent < ViewComponent::Base
def initialize(events:)
@grouped_events = events.group_by { |event| event.starts_at.to_date }
@weekdays = Time.zone.today.all_week
end
end
end
14 changes: 14 additions & 0 deletions app/controllers/app/agendas_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module App
class AgendasController < BaseController
def show
authorize :agenda, :show?

activities = policy_scope(Current.company.activities).includes(:schedule)
week = Time.zone.today.all_week

@events = EventDecorator.decorate_collection(
Event.from_activities(activities, from: week.begin, to: week.end)
)
end
end
end
2 changes: 1 addition & 1 deletion app/models/reservation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Reservation < ApplicationRecord
belongs_to :activity

scope :from_company, ->(company_id) { joins(:activity).where(activity: { company_id: }) }
scope :future, -> { where(starts_at: 1.hour.ago.round..) }
scope :future, -> { where(starts_at: 1.hour.ago.floor..) }

after_commit :broadcast_event

Expand Down
7 changes: 7 additions & 0 deletions app/policies/app/agenda_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module App
class AgendaPolicy < ApplicationPolicy
def show?
customer?
end
end
end
16 changes: 16 additions & 0 deletions app/views/app/agendas/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= turbo_frame_tag :remote_modal, target: :_top do %>
<div class="modal" data-controller="modal" data-bs-backdrop="static">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><%= t '.title' %></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>

<div class="modal-body">
<%= render App::AgendaComponent.new(events: @events) %>
</div>
</div>
</div>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/app/events/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

<div class="card mb-3">
<div class="card-header">
<%= link_to t(".agenda"), app_agenda_path, class: "text-sm float-end", data: { turbo_frame: :remote_modal } if policy([:app, :agenda]).show? %>

<%= Event.model_name.human.pluralize %>
</div>

Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ en:
schedule_fields:
add_rule: Add rule
app:
agendas:
show:
title: Agenda
events:
index:
agenda: Show agenda
today: Today
event:
date: Date
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

constraints CompanyConstraint.new do
namespace :app do
resource :agenda, only: :show
resources :activities, only: [] do
resources :reservations, only: [ :new, :create ]
end
Expand Down
21 changes: 21 additions & 0 deletions spec/components/app/agenda_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe App::AgendaComponent, type: :component do
let(:activity) { build(:activity) }
let(:event1) { build(:event, activity:, starts_at: Time.current.beginning_of_week).decorate }
let(:event2) { build(:event, activity:, starts_at: Time.current.end_of_week).decorate }

subject { render_inline(described_class.new(events: [ event1, event2 ])).to_html }

Time.zone.today.all_week.each do |date|
it { is_expected.to include(date.strftime("%A")) }
end

it { is_expected.to include(activity.name) }
it { is_expected.to include(event1.start_time) }
it { is_expected.to include(event1.end_time) }
it { is_expected.to include(event2.start_time) }
it { is_expected.to include(event2.end_time) }
end
1 change: 1 addition & 0 deletions spec/lib/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
let(:event) { build(:event, activity:, starts_at:, hours_for_reservation:) }

before { Time.zone = company.utc_offset }
after { Time.zone = nil }

context 'delegations' do
subject { event }
Expand Down
2 changes: 1 addition & 1 deletion spec/models/reservation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@

subject { described_class.future }

it { is_expected.to match_array([ reservation1, reservation2 ]) }
it { travel_to(starts_at) { is_expected.to match_array([ reservation1, reservation2 ]) } }
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/policies/app/agenda_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe App::AgendaPolicy, type: :policy do
let(:customer) { create(:customer) }

subject { described_class }

before { Current.customer = customer }

permissions :show? do
it_behaves_like "a customer resource" do
let(:resource) { :agenda }
end
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require 'support/shared_examples'
require 'support/shoulda'
require 'support/system_tests'
require 'support/time_helpers'
require 'support/turbo'
require 'support/view_component'
require 'capybara/rspec'
Expand Down
3 changes: 3 additions & 0 deletions spec/support/time_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.include ActiveSupport::Testing::TimeHelpers
end

0 comments on commit 986f3df

Please sign in to comment.