-
Notifications
You must be signed in to change notification settings - Fork 77
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
Feat: Custom leaves #1823
Merged
Merged
Feat: Custom leaves #1823
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ed0237c
crud custom leaves
apoorv1316 8413e41
Merge branch 'develop' into customised-leaves
apoorv1316 2a55f36
send custom leaves in index action
apoorv1316 fbb562b
Merge branch 'develop' into customised-leaves
apoorv1316 346abe9
Merge branch 'develop' into customised-leaves
apoorv1316 7f8d15e
review comments
apoorv1316 bb18feb
Merge branch 'develop' into customised-leaves
apoorv1316 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 :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 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
find_leave
orset_leave