From 33ca33751cb2df881d3e828e7114756a240adbbb Mon Sep 17 00:00:00 2001 From: Gustavo Molinari Date: Tue, 28 Jan 2025 16:32:43 -0300 Subject: [PATCH] [FS-12] Add agenda app panel --- app/components/app/agenda_component.html.erb | 23 ++++++++++++++++++++ app/components/app/agenda_component.rb | 8 +++++++ app/controllers/app/agendas_controller.rb | 14 ++++++++++++ app/policies/app/agenda_policy.rb | 7 ++++++ app/views/app/agendas/show.html.erb | 16 ++++++++++++++ app/views/app/events/index.html.erb | 2 ++ config/locales/en.yml | 3 +++ config/routes.rb | 1 + spec/components/app/agenda_component_spec.rb | 21 ++++++++++++++++++ spec/policies/app/agenda_policy_spec.rb | 15 +++++++++++++ 10 files changed, 110 insertions(+) create mode 100644 app/components/app/agenda_component.html.erb create mode 100644 app/components/app/agenda_component.rb create mode 100644 app/controllers/app/agendas_controller.rb create mode 100644 app/policies/app/agenda_policy.rb create mode 100644 app/views/app/agendas/show.html.erb create mode 100644 spec/components/app/agenda_component_spec.rb create mode 100644 spec/policies/app/agenda_policy_spec.rb diff --git a/app/components/app/agenda_component.html.erb b/app/components/app/agenda_component.html.erb new file mode 100644 index 0000000..0f55fe3 --- /dev/null +++ b/app/components/app/agenda_component.html.erb @@ -0,0 +1,23 @@ + + +
+ +
+ <% @grouped_events.each_pair do |day, events| %> + <%= tag.div id: day, class: ["tab-pane", ("active" if day.today?)], role: :tabpanel do %> + + <% end %> + <% end %> +
diff --git a/app/components/app/agenda_component.rb b/app/components/app/agenda_component.rb new file mode 100644 index 0000000..4ae6171 --- /dev/null +++ b/app/components/app/agenda_component.rb @@ -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 diff --git a/app/controllers/app/agendas_controller.rb b/app/controllers/app/agendas_controller.rb new file mode 100644 index 0000000..b5d8509 --- /dev/null +++ b/app/controllers/app/agendas_controller.rb @@ -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 diff --git a/app/policies/app/agenda_policy.rb b/app/policies/app/agenda_policy.rb new file mode 100644 index 0000000..848c460 --- /dev/null +++ b/app/policies/app/agenda_policy.rb @@ -0,0 +1,7 @@ +module App + class AgendaPolicy < ApplicationPolicy + def show? + customer? + end + end +end diff --git a/app/views/app/agendas/show.html.erb b/app/views/app/agendas/show.html.erb new file mode 100644 index 0000000..6ba3f48 --- /dev/null +++ b/app/views/app/agendas/show.html.erb @@ -0,0 +1,16 @@ +<%= turbo_frame_tag :remote_modal, target: :_top do %> + +<% end %> diff --git a/app/views/app/events/index.html.erb b/app/views/app/events/index.html.erb index 4d358ed..9d4632a 100644 --- a/app/views/app/events/index.html.erb +++ b/app/views/app/events/index.html.erb @@ -3,6 +3,8 @@
+ <%= 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 %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 31f2e07..754d5b4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -92,6 +92,9 @@ en: schedule_fields: add_rule: Add rule app: + agendas: + show: + title: Agenda events: index: today: Today diff --git a/config/routes.rb b/config/routes.rb index a67717d..e402a58 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/components/app/agenda_component_spec.rb b/spec/components/app/agenda_component_spec.rb new file mode 100644 index 0000000..0709d3b --- /dev/null +++ b/spec/components/app/agenda_component_spec.rb @@ -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 diff --git a/spec/policies/app/agenda_policy_spec.rb b/spec/policies/app/agenda_policy_spec.rb new file mode 100644 index 0000000..ab23dbb --- /dev/null +++ b/spec/policies/app/agenda_policy_spec.rb @@ -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