Skip to content

Commit 054ac83

Browse files
committed
Temp
1 parent 65ffe2f commit 054ac83

19 files changed

Lines changed: 350 additions & 1 deletion

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ gem "sidekiq-scheduler"
116116

117117
gem "paper_trail"
118118

119+
gem "holidays"
120+
119121
gem "phlex-rails", "~> 2.0"
120122

121123
gem "tailwind_merge", "~> 1.2"

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ GEM
138138
raabro (~> 1.4)
139139
globalid (1.2.1)
140140
activesupport (>= 6.1)
141+
holidays (8.8.0)
141142
hotwire-livereload (2.0.0)
142143
actioncable (>= 7.0.0)
143144
listen (>= 3.0.0)
@@ -398,6 +399,7 @@ DEPENDENCIES
398399
devise_invitable (~> 2.0.9)
399400
discard
400401
dotenv-rails (~> 2.1, >= 2.1.1)
402+
holidays
401403
hotwire-livereload
402404
invisible_captcha
403405
jbuilder
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Workspace
2+
class HolidaysController < WorkspaceController
3+
before_action :set_organization
4+
5+
def show
6+
authorize! @organization
7+
@available_countries = Holidays.available_regions.sort
8+
@selected_country = @organization.holiday_country_code
9+
end
10+
11+
def update
12+
authorize! @organization
13+
if @organization.update(organization_params)
14+
redirect_to workspace_holidays_path, notice: t("holidays.country_updated")
15+
else
16+
@available_countries = Holidays.available_regions.sort
17+
@selected_country = @organization.holiday_country_code
18+
render :show, status: :unprocessable_entity
19+
end
20+
end
21+
22+
private
23+
24+
def set_organization
25+
@organization = current_user.current_organization
26+
end
27+
28+
def organization_params
29+
params.require(:organization).permit(:holiday_country_code)
30+
end
31+
32+
def implicit_authorization_target
33+
Organization
34+
end
35+
end
36+
end

app/controllers/workspace/workspace_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Workspace
22
class WorkspaceController < ApplicationController
3+
before_action :authenticate_user!
4+
35
layout -> do
46
"workspace"
57
end

app/helpers/holidays_helper.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module HolidaysHelper
2+
def holidays_for_country(country_code)
3+
return [] unless valid_country_code?(country_code)
4+
5+
start_date = Date.today
6+
end_date = start_date + 1.year
7+
8+
Holidays.between(start_date, end_date, country_code.to_sym)
9+
.sort_by { |holiday| holiday[:date] }
10+
end
11+
12+
def format_holiday_date(date)
13+
date.strftime("%A, %B %d, %Y")
14+
end
15+
16+
def country_name_from_code(code)
17+
countries = Stemplin.config.countries || {}
18+
countries[code.to_sym] || code.to_s.upcase
19+
end
20+
21+
def for_select_countries(country_codes)
22+
country_codes.map { |code| [ country_name_from_code(code), code.to_s ] }
23+
end
24+
25+
private
26+
27+
def valid_country_code?(code)
28+
Holidays.available_regions.include?(code.to_sym)
29+
end
30+
end

app/models/organization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Organization < ApplicationRecord
88
has_many :time_regs, through: :users
99

1010
validates :name, presence: true, uniqueness: true
11+
validates :holiday_country_code, inclusion: { in: proc { Holidays.available_regions.map(&:to_s) }, allow_nil: true }
1112
validate :currency_exists
1213

1314
def currency_exists
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Workspace
2+
class HolidaysPolicy < WorkspacePolicy
3+
def show?
4+
user.current_organization.present?
5+
end
6+
7+
def update?
8+
user.organization_admin?
9+
end
10+
end
11+
end

app/views/workspace/_sidenav.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@
1818
<span><%= t("common.settings") %></span>
1919
<% end %>
2020
</li>
21+
<li>
22+
<%= link_to workspace_holidays_path, class: class_names("group flex items-center gap-x-3 rounded-md p-2 text-sm leading-6 font-semibold", is_page_active?([workspace_holidays_path]) ? "bg-gray-100 text-primary-600": "hover:text-primary-700 text-gray-700") do %>
23+
<i class="uc-icon text-xl">&#xe8df;</i>
24+
<span><%= t("common.holidays") %></span>
25+
<% end %>
26+
</li>
2127
</ul>
2228
</div>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<% content_for :title, t("common.holidays") %>
2+
3+
<div class="flex flex-col gap-y-8">
4+
<!-- Header -->
5+
<div class="flex flex-row justify-between items-center border-b border-gray-100 py-3">
6+
<div class="flex flex-row items-center gap-x-2">
7+
<span class="font-medium text-gray-600 text-lg"><%= t("common.holidays") %></span>
8+
</div>
9+
</div>
10+
11+
<!-- Country Selection Form -->
12+
<%= form_with model: @organization, url: workspace_holidays_path, method: :patch do |form| %>
13+
<div class="flex flex-row gap-4 items-end">
14+
<div class="flex-1 max-w-md">
15+
<%= render RubyUI::FormField.new do %>
16+
<%= render RubyUI::FormFieldLabel.new { t("holidays.select_country") } %>
17+
<%= form.select :holiday_country_code,
18+
for_select_countries(@available_countries),
19+
{ prompt: t("holidays.select_country_prompt") },
20+
class: "border-gray-200 rounded-md w-full" %>
21+
<% end %>
22+
</div>
23+
<div>
24+
<%= render ButtonComponent.new(type: :submit) do %>
25+
<span><%= t("holidays.show_holidays") %></span>
26+
<% end %>
27+
</div>
28+
</div>
29+
<% end %>
30+
31+
<!-- Holidays Table -->
32+
<% if @selected_country.present? %>
33+
<div>
34+
<% holidays = holidays_for_country(@selected_country) %>
35+
<% if holidays.any? %>
36+
<%= render RubyUI::Table.new do %>
37+
<%= render RubyUI::TableHeader.new do %>
38+
<%= render RubyUI::TableRow.new do %>
39+
<%= render RubyUI::TableHead.new { t("holidays.holiday_name") } %>
40+
<%= render RubyUI::TableHead.new { t("holidays.date") } %>
41+
<% end %>
42+
<% end %>
43+
<%= render RubyUI::TableBody.new(class: "text-base") do %>
44+
<% holidays.each do |holiday| %>
45+
<%= render RubyUI::TableRow.new do %>
46+
<%= render RubyUI::TableCell.new(class: "py-4 font-medium") { holiday[:name] } %>
47+
<%= render RubyUI::TableCell.new(class: "py-4") { format_holiday_date(holiday[:date]) } %>
48+
<% end %>
49+
<% end %>
50+
<% end %>
51+
<% end %>
52+
<% else %>
53+
<p class="text-gray-500 text-center py-8"><%= t("holidays.no_holidays_found") %></p>
54+
<% end %>
55+
</div>
56+
<% else %>
57+
<div class="bg-blue-50 border border-blue-200 rounded-md p-4">
58+
<p class="text-blue-800"><%= t("holidays.please_select_country") %></p>
59+
</div>
60+
<% end %>
61+
</div>

config/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Application < Rails::Application
4949

5050
config.emails = config_for(:emails)
5151
config.currencies = config_for(:currencies)
52+
config.countries = config_for(:countries)
5253
end
5354

5455
def self.config

0 commit comments

Comments
 (0)