Skip to content
Merged
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
4 changes: 4 additions & 0 deletions app/assets/stylesheets/pages/backend/leave_times.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
text-align: center;
}
}

.checkbox-inline:first-child {
margin-left: 10px;
}
22 changes: 20 additions & 2 deletions app/controllers/backend/leave_times_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def index
end

def new
@current_object = LeaveTime.ransack(@search_params).result.preload(:user).new
@current_object = LeaveTime.new
end

def create
@current_object = LeaveTime.ransack(@search_params).result.preload(:user).new(resource_params)
@current_object = LeaveTime.new(resource_params)
return render action: :new unless @current_object.save
action_success
end
Expand All @@ -21,6 +21,19 @@ def append_quota
render :new
end

def batch_new
@current_object = LeaveTime.new
end

def batch_create
user_ids = params[:leave_time][:user_id]
user_ids.each do |user_id|
@current_object = LeaveTime.new(batch_leave_time_params.merge(user_id: user_id))
return render action: :batch_new unless @current_object.save
end
action_success
end

private

def leave_time_params_by_leave_application
Expand All @@ -46,6 +59,11 @@ def resource_params
)
end

def batch_leave_time_params
params.require(:leave_time).permit(
:leave_type, :quota, :effective_date, :expiration_date, :usable_hours, :used_hours, :remark)
end

def search_params
params.fetch(:q, {})&.permit(:s, :leave_type_eq, :effective_true, :user_id_eq)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def action_fail(message, action)
end

def set_actions
@actions = [:create, :update, :destroy]
@actions = [:create, :update, :destroy, :batch_create]
end

def action_success(url = nil)
Expand Down
2 changes: 1 addition & 1 deletion app/views/backend/leave_applications/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
%h2= t("captions.backend/leave_applications.#{action_name}")
%p= t("captions.backend/leave_applications.#{action_name}/hint")
= simple_form_for current_object, url: url_for(action: action), method: :post do |f|
= f.association :user, collection: User.all, label_method: :name, value_method: :id, required: true
= f.association :user, collection: User.where.not(role: 'resigned'), label_method: :name, value_method: :id, required: true
= f.error :base, error: sanitize(f.object.errors[:base].join('<br />'), tags: %w(a br), attributes: %w(href target))
= f.input :leave_type, required: true, collection: LeaveApplication.enum_attributes_for_select(:leave_types)
= f.input :start_time, as: :string, input_html: { data: { input: 'datetimepicker', format: 'Y/m/d H:i' } }
Expand Down
2 changes: 1 addition & 1 deletion app/views/backend/leave_times/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%h2= t("captions.backend/leave_times.#{action_name}")

= simple_form_for [:backend, current_object] do |f|
= f.association :user, required: true
= f.association :user, required: true, collection: User.where.not(role: 'resigned')
= f.input :leave_type, required: true, collection: LeaveTime.enum_attributes_for_select(:leave_types)
= f.input :quota
= f.input :effective_date, as: :string, required: true, wrapper_html: { class: 'date-input' },
Expand Down
16 changes: 16 additions & 0 deletions app/views/backend/leave_times/batch_new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
%h2= t("captions.backend/leave_times.#{action_name}")

= simple_form_for current_object, url: batch_create_backend_leave_times_path do |f|
= f.association :user, required: true, collection: User.where.not(role: 'resigned'),as: :check_boxes, include_hidden: false, item_wrapper_class: "checkbox-inline", label: false
= f.input :leave_type, required: true, collection: LeaveTime.enum_attributes_for_select(:leave_types)
= f.input :quota
= f.input :effective_date, as: :string, required: true, wrapper_html: { class: 'date-input' },
input_html: { data: { input: 'datepicker', format: 'Y/m/d' } }
= f.input :expiration_date, as: :string, required: true, wrapper_html: { class: 'date-input' },
input_html: { data: { input: 'datepicker', format: 'Y/m/d' } }
= f.input :remark, as: :text
- unless current_object.new_record?
= f.input :usable_hours
= f.input :used_hours
= link_to t("title.backend/leave_times.delete"), backend_leave_time_path(current_object), method: :delete, class: "btn btn-sm btn-danger", data: {confirm: "Are you sure?"}
= f.button :submit
1 change: 1 addition & 0 deletions app/views/shared/_navigation.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
= dropdown_title t("nav.manage")
%ul.dropdown-menu
%li= link_to t("title.backend/leave_times.new"), new_backend_leave_time_path
%li= link_to t("title.backend/leave_times.batch_append_quota"),batch_new_backend_leave_times_path
%li= link_to t("title.backend/leave_applications.new"), new_backend_leave_application_path
%li= link_to t("title.backend/leave_applications.index"),
backend_leave_applications_path(status: :pending)
Expand Down
3 changes: 3 additions & 0 deletions config/locales/meta_data.zh_TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ zh-TW:
backend/leave_times:
index: "員工休假額度"
new: "新增員工休假額度"
batch_append_quota: "批次新增員工休假額度"
edit: "修改"
delete: "刪除"
change_type: "切換休假種類至"
Expand Down Expand Up @@ -216,6 +217,7 @@ zh-TW:
backend/leave_times:
index: "員工休假額度"
new: "新增員工休假額度"
batch_new: "批次新增員工休假額度"
append_quota: "新增員工休假額度"
edit: "修改員工休假"
tools: "工具"
Expand All @@ -226,6 +228,7 @@ zh-TW:
destroy: "刪除成功"
revise: "已修改假單"
cancel: "已取消假單"
batch_create: '批次新增員工休假額度成功'

warnings:
self_manage: "申請人不得自行審核"
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

resources :leave_times, except: [:edit, :update, :destroy] do
post :append_quota, on: :collection
get :batch_new, on: :collection
post :batch_create, on: :collection
end

resources :bonus_leave_time_logs, only: [:index, :update]
Expand Down
11 changes: 11 additions & 0 deletions spec/controllers/backend/leave_times_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Backend::LeaveTimesController, type: :controller do
describe 'POST #batch_create' do
before do
sign_in create(:manager_eddie)
5.times { FactoryGirl.create(:user) }
end
it 'add leave time to each users' do
expect do
post :batch_create, params: { leave_time: { 'user_id' => User.select(:id).last(4), 'leave_type' => 'personal', 'quota' => '3', 'effective_date' => '2017/12/15', 'expiration_date' => '2017/12/25', 'remark' => 'fd' } }
end.to change(LeaveTime, :count).by(4)
end
end
end