-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* crud custom leaves * send custom leaves in index action * review comments
- Loading branch information
1 parent
e857d09
commit a518d19
Showing
13 changed files
with
211 additions
and
11 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
app/controllers/internal_api/v1/custom_leaves_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
class InternalApi::V1::CustomLeavesController < InternalApi::V1::ApplicationController | ||
before_action :set_leave, only: [:update] | ||
|
||
def update | ||
authorize current_user, policy_class: LeaveWithLeaveTypesPolicy | ||
CustomLeavesService.new(leave, update_params).process | ||
render json: { notice: "Leaves updated successfully" }, status: :ok | ||
end | ||
|
||
private | ||
|
||
def set_leave | ||
@_leave ||= current_company.leaves.find_or_create_by(year: params[:year]) | ||
end | ||
|
||
def update_params | ||
params.require(:custom_leaves).permit( | ||
add_custom_leaves: [:name, :color, :icon, :allocation_value, | ||
:allocation_period, user_ids: [], | ||
], | ||
update_custom_leaves: [:id, :name, :color, :icon, :allocation_value, | ||
:allocation_period, user_ids: [], | ||
], | ||
remove_custom_leaves: [] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: custom_leaves | ||
# | ||
# id :bigint not null, primary key | ||
# allocation_period :integer not null | ||
# allocation_value :integer not null | ||
# name :string not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# leave_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_custom_leaves_on_leave_id (leave_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (leave_id => leaves.id) | ||
# | ||
class CustomLeave < ApplicationRecord | ||
belongs_to :leave | ||
has_many :custom_leave_users, dependent: :destroy | ||
has_many :users, through: :custom_leave_users | ||
|
||
enum allocation_period: { | ||
days: 0, | ||
weeks: 1, | ||
months: 2 | ||
} | ||
|
||
validates :name, :allocation_value, :allocation_period, presence: true | ||
validates :allocation_value, numericality: { greater_than_or_equal_to: 1 } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: custom_leave_users | ||
# | ||
# id :bigint not null, primary key | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# custom_leave_id :bigint not null | ||
# user_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_custom_leave_users_on_custom_leave_id (custom_leave_id) | ||
# index_custom_leave_users_on_user_id (user_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (custom_leave_id => custom_leaves.id) | ||
# fk_rails_... (user_id => users.id) | ||
# | ||
class CustomLeaveUser < ApplicationRecord | ||
belongs_to :custom_leave | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
class CustomLeavesService | ||
attr_reader :leave, :params | ||
|
||
def initialize(leave, params) | ||
@leave = leave | ||
@params = params | ||
end | ||
|
||
def process | ||
ActiveRecord::Base.transaction do | ||
add_custom_leaves | ||
update_custom_leaves | ||
remove_custom_leaves | ||
end | ||
end | ||
|
||
private | ||
|
||
def add_custom_leaves | ||
return if params[:add_custom_leaves].blank? | ||
|
||
params[:add_custom_leaves].each do |custom_leave| | ||
leave.custom_leaves.create!(custom_leave) | ||
end | ||
end | ||
|
||
def update_custom_leaves | ||
return if params[:update_custom_leaves].blank? | ||
|
||
params[:update_custom_leaves].each do |update_custom_leave| | ||
custom_leave = leave.custom_leaves.find(update_custom_leave[:id]) | ||
next unless custom_leave | ||
|
||
custom_leave.update!(update_custom_leave) | ||
end | ||
end | ||
|
||
def remove_custom_leaves | ||
return if params[:remove_custom_leaves].blank? | ||
|
||
leave.custom_leaves.where(id: params[:remove_custom_leaves]).destroy_all | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateCustomLeaves < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :custom_leaves do |t| | ||
t.string :name, null: false | ||
t.integer :allocation_value, null: false | ||
t.integer :allocation_period, null: false | ||
t.references :leave, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateCustomLeaveUsers < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :custom_leave_users do |t| | ||
t.references :custom_leave, null: false, foreign_key: true | ||
t.references :user, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.