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

Feature/ud field types #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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: 1 addition & 1 deletion app/controllers/admin/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ApplicationController < ApplicationController
private

def is_admin
return if current_user&.admin?
return if current_user.admin?

flash[:danger] = t("application.user.is_not_admin")
redirect_to root_path
Expand Down
49 changes: 48 additions & 1 deletion app/controllers/admin/field_types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
module Admin
class FieldTypesController < ApplicationController
before_action :load_field, only: %i(show create)
before_action :load_field_type, only: %i(destroy update)
before_action :has_booking, only: :destroy

def index; end

Expand All @@ -26,6 +28,27 @@ def create
end
end

def destroy
if @field_type.destroy
manhhnee marked this conversation as resolved.
Show resolved Hide resolved
flash[:success] = t("admin.field.delete_success")
redirect_to admin_fields_path
else
flash[:danger] = t("admin.field.delete_fail")
redirect_to root_path
end
end

def update
if @field_type.update(field_type_params)
update_prices(params)
Copy link
Contributor

Choose a reason for hiding this comment

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

Đặt xử lý này vào callback của model nha. Để khi update field_type thì chạy luôn

flash[:success] = t("admin.field_type.update_success")
redirect_to admin_field_type_path(@field_type.field_id)
else
flash[:danger] = t("admin.field_type.update_fail")
render :edit
end
end

private

def load_field
Expand All @@ -37,8 +60,16 @@ def load_field
redirect_to admin_fields_path
end

def load_field_type
@field_type = FieldType.find_by(id: params[:id])
Copy link
Contributor

Choose a reason for hiding this comment

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

Có cần load field_type từ field không. Và check thêm điều kiện user có phải chủ sân không nữa

return if @field_type

flash[:warning] = t("admin.field.field_type_not_found")
redirect_to admin_fields_path
end

def field_type_params
params.permit(:field_type_name)
params.require(:field_type).permit(:field_type_name)
end

def build_prices(params)
Expand All @@ -47,5 +78,21 @@ def build_prices(params)
@field_type.prices.build(name: period[0][0], price: price_value)
end
end

def update_prices(prices_params)
return unless prices_params && prices_params[:field_type]

%w(Morning Afternoon Evening).each do |period|
price = prices_params[:field_type]["#{period.downcase}_price"]
@field_type.prices.find_or_initialize_by(name: period[0]).update(price:)
end
end

def has_booking
return if @field_type.bookings.pending.empty?

flash[:danger] = t("admin.field.can_destroy")
redirect_to admin_fields_path
end
end
end
4 changes: 2 additions & 2 deletions app/models/field_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
class FieldType < ApplicationRecord
belongs_to :field

has_many :bookings
has_many :prices
has_many :bookings, dependent: :destroy
has_many :prices, dependent: :destroy

delegate :description, :address, :phone_number, to: :field, prefix: true
end
10 changes: 7 additions & 3 deletions app/views/admin/field_types/_table.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<table class="table table-hover table-dark table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col"><%= t("admin.title_name_type") %></th>
<th scope="col"><%= t("admin.title_name") %></th>
<th scope="col"><%= t("admin.title_description") %></th>
Expand All @@ -13,16 +12,21 @@
<tbody>
<% @field_types.each do |field_type| %>
<tr>
<th scope="row"><%= field_type.id %></th>
<td><%= field_type.field_type_name %></td>
<td><%= field_type.field.name %></td>
<td><%= field_type.field.description %></td>
<td><%= field_type.field.phone_number %></td>
<td><%= field_type.field.address %></td>
<td>
<%= button_to t("admin.delete") , admin_field_type_path(field_type), method: :delete, class: "btn btn-danger" %>
<%= button_to t("admin.delete"), admin_field_type_path(field_type), method: :delete, class: "btn btn-danger" %>
<!-- Button trigger modal for updating -->
<button type="button" class="btn btn-warning" data-toggle="modal" data-target="#updateModal<%= field_type.id %>">
<%= t("admin.update") %>
</button>
</td>
</tr>
<!-- Update Modal -->
<%= render "update", field_type: field_type%>
<% end %>
</tbody>
</table>
30 changes: 30 additions & 0 deletions app/views/admin/field_types/_update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="modal fade" id="updateModal<%= field_type.id %>" tabindex="-1" role="dialog" aria-labelledby="updateModalLabel<%= field_type.id %>" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="updateModalLabel<%= field_type.id %>"><%= t("admin.update_field") %></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<%= form_with(model: field_type, url: admin_field_type_path(field_type), method: :put) do |form| %>
<div class="form-group">
<%= form.label :field_type_name %>
<%= form.text_field :field_type_name, class: "form-control" %>
</div>
<% %w(Morning_Price Afternoon_Price Evening_Price).each do |period| %>
<div class="form-group">
<%= form.label "#{period.downcase}", period %>
<%= form.text_field "#{period.downcase}", class: "form-control" %>
</div>
<% end %>
<%= form.submit t("admin.update"), class: "btn btn-primary" %>
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><%= t("admin.close") %></button>
</div>
</div>
</div>
</div>
40 changes: 19 additions & 21 deletions app/views/admin/fields/_field.html.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
<table class="table table-hover table-dark table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col"><%= t("admin.title_name") %></th>
<th scope="col"><%= t("admin.title_description") %></th>
<th scope="col"><%= t("admin.title_phone_number") %></th>
<th scope="col"><%= t("admin.title_address") %></th>
<th scope="col"><%= t("admin.title_actions") %></th>
</tr>
</thead>
<tbody>
<% @fields.each do |field| %>
<tr>
<th scope="row"><%= field.id %></th>
<td><%= field.name %></td>
<td><%= field.description %></td>
<td><%= field.phone_number %></td>
<td><%= field.address %></td>
<td>
<%= button_to t("admin.delete"), admin_field_path(field), method: :delete, class: "btn btn-danger" %>
<%= link_to t("admin.view_detail") , admin_field_type_path(field), class: "btn btn-danger" %>
</td>
<th scope="col"><%= t("admin.title_name") %></th>
<th scope="col"><%= t("admin.title_description") %></th>
<th scope="col"><%= t("admin.title_phone_number") %></th>
<th scope="col"><%= t("admin.title_address") %></th>
<th scope="col"><%= t("admin.title_actions") %></th>
</tr>
<% end %>
</thead>
<tbody>
<% @fields.each do |field| %>
<tr>
<td><%= field.name %></td>
<td><%= field.description %></td>
<td><%= field.phone_number %></td>
<td><%= field.address %></td>
<td>
<%= button_to t("admin.delete"), admin_field_path(field), method: :delete, class: "btn btn-danger" %>
<%= link_to t("admin.view_detail") , admin_field_type_path(field), class: "btn btn-danger" %>
</td>
</tr>
<% end %>
</tbody>
</table>
15 changes: 15 additions & 0 deletions app/views/admin/fields/_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<%= render "form" %>
</div>
<button type="button" class="btn btn-secondary" data-dismiss="modal"><%= t("admin.close") %></button>
</div>
</div>
</div>
16 changes: 1 addition & 15 deletions app/views/admin/fields/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,4 @@
<%= t("admin.add_field") %>
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<%= render "form" %>
</div>
<button type="button" class="btn btn-secondary" data-dismiss="modal"><%= t("admin.close") %></button>
</div>
</div>
</div>
<%= render "table"%>
6 changes: 1 addition & 5 deletions config/locales/controllers/vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,4 @@ vi:
fail: "Hủy đặt chỗ thất bại"

booking:
not_found: "Không tìm thấy đặt chỗ"

field_type:
create_success: "Tạo loại sân thành công"
create_fail: "Tạo loại sân thất bại"
not_found: "Không tìm thấy đơn đặt này"
Copy link
Contributor

Choose a reason for hiding this comment

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

Không thấy thay đổi ở file en nhỉ

4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ en:
field_type: "Field Type"
start_time: "Start time"
end_time: "End time"

user:
is_not_admin: "You are not admin"

admin:
introduce: "This is the admin page"
Comment on lines +30 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

Thêm i18n ở file vi nữa

42 changes: 23 additions & 19 deletions config/locales/views/en.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
en:
sessions:
sign_in_title: "Sign in"
sign_in_title: "Sign In"

registrations:
sign_up_title: "Sign up"
remember_me: "Remember me"
sign_up_title: "Sign Up"
remember_me: "Remember Me"

header:
home: "Home"
Expand Down Expand Up @@ -44,15 +44,19 @@ en:
profile: "Profile"
information: "Personal information"
email: "Email"
phone: "Phone number"
phone: "Phone Number"

booking:
delete_success: "Booking has been deleted"
delete_fail: "Failed to delete booking"

admin:
introduce: "This is the admin page"
dashboard: "Dashboard"
field_manager: "Field manager"
field_manager: "Field Manager"
title_name: "Name"
title_description: "Description"
title_phone_number: "Phone number"
title_phone_number: "Phone Number"
title_address: "Address"
title_actions: "Actions"
delete: "Delete"
Expand Down Expand Up @@ -85,23 +89,23 @@ en:
welcome: "Welcome to Dashboard"
hello: "Hello %{full_name}, welcome to your awesome dashboard!"
settings: "Settings"
admin_page: "Admin page"
admin_page: "Admin Page"

email:
booking_accepted:
subject: "Your booking has been accepted"
subject: "Your booking request has been accepted"
greeting: "Hello %{user_name},"
message: "We are pleased to inform you that your booking has been accepted. Below are the details of your booking:"
field_name: "Field name"
booked_date: "Booked date"
start_time: "Start time"
end_time: "End time"
message: "We are pleased to inform you that your booking request has been accepted. Below are the details of your booking:"
field_name: "Field Name"
booked_date: "Booked Date"
start_time: "Start Time"
end_time: "End Time"

booking_rejected:
subject: "Your booking has been rejected"
subject: "Your booking request has been rejected"
greeting: "Hello %{user_name},"
message: "We regret to inform you that your booking has been rejected. Below are the details:"
field_name: "Field name"
booked_date: "Booked date"
start_time: "Start time"
end_time: "End time"
message: "We regret to inform you that your booking request has been rejected. Below are the details:"
field_name: "Field Name"
booked_date: "Booked Date"
start_time: "Start Time"
end_time: "End Time"
10 changes: 10 additions & 0 deletions config/locales/views/vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ vi:
close: "Đóng"
field:
create: "Tạo mới"

booking:
delete_success: "Lịch đặt sân đã bị xóa"
delete_fail: "Đặt sân thành công"
field:
create: "Tạo mới"
add_field: "Thêm sân bóng"
close: "Đóng"
field:
create: "Tạo mới"
Comment on lines +64 to +72
Copy link
Contributor

Choose a reason for hiding this comment

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

Check luôn i18n chỗ này

morning_price: "Giá buổi sáng"
afternoon_price: "Giá buổi chiều"
evening_price: "Giá buổi tối"
Expand Down