Thanks to visit codestin.com
Credit goes to github.com

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
2 changes: 2 additions & 0 deletions app/controllers/concerns/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def record_not_discarded(exception)
end

def record_invalid(exception)
message = exception.message

respond_to do |format|
format.json {
render json: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

class InternalApi::V1::CompanyUsersController < InternalApi::V1::ApplicationController
class InternalApi::V1::EmploymentsController < InternalApi::V1::ApplicationController
def index
authorize CompanyUser
authorize Employment
render :index, locals: { users: current_company.users.kept }, status: :ok
end
end
2 changes: 1 addition & 1 deletion app/controllers/internal_api/v1/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def current_company_clients
end

def current_company_users
@_current_company_users = current_company.company_users.joins(:user)
@_current_company_users = current_company.employments.joins(:user)
.select("users.id as id, users.first_name as first_name, users.last_name as last_name")
end

Expand Down
8 changes: 4 additions & 4 deletions app/controllers/internal_api/v1/team_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ def index

def destroy
authorize :team
company_user.discard!
employment.discard!
render json: {
user: company_user.user,
user: employment.user,
notice: I18n.t("team.delete.success.message")
}, status: :ok
end

private

def company_user
@company_user ||= current_company.company_users.kept.find_by!(user_id: params[:id])
def employment
@employment ||= current_company.employments.kept.find_by!(user_id: params[:id])
end
end
2 changes: 1 addition & 1 deletion app/javascript/src/apis/company-users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";

const path = "/company_users";
const path = "/employments";

const get = async () => axios.get(`${path}`);

Expand Down
4 changes: 2 additions & 2 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

class Company < ApplicationRecord
# Associations
has_many :company_users, dependent: :destroy
has_many :users, through: :company_users
has_many :employments, dependent: :destroy
has_many :users, through: :employments
has_many :timesheet_entries, through: :users
has_many :clients, dependent: :destroy
has_many :projects, through: :clients, dependent: :destroy
Expand Down
11 changes: 6 additions & 5 deletions app/models/company_user.rb → app/models/employment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# == Schema Information
#
# Table name: company_users
# Table name: employments
#
# id :bigint not null, primary key
# designation :string
Expand All @@ -18,24 +18,25 @@
#
# Indexes
#
# index_company_users_on_company_id (company_id)
# index_company_users_on_discarded_at (discarded_at)
# index_company_users_on_user_id (user_id)
# index_employments_on_company_id (company_id)
# index_employments_on_discarded_at (discarded_at)
# index_employments_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (company_id => companies.id)
# fk_rails_... (user_id => users.id)
#

class CompanyUser < ApplicationRecord
class Employment < ApplicationRecord
include Discard::Model

# Associations
belongs_to :company
belongs_to :user

# Validations
# TODO:- To be uncommented after UI integration is done
# validates :designation, :employment_type, :joined_at, :employee_id, presence: true
# validates :resigned_at, comparison: { greater_than: :joined_at }, unless: -> { resigned_at.nil? }
end
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class User < ApplicationRecord
include Discard::Model

# Associations
has_many :company_users, dependent: :destroy
has_many :companies, through: :company_users
has_many :employments, dependent: :destroy
has_many :companies, through: :employments
has_many :project_members, dependent: :destroy
has_many :timesheet_entries
has_many :identities, dependent: :delete_all
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class CompanyUserPolicy < ApplicationPolicy
class EmploymentPolicy < ApplicationPolicy
def index?
user_owner_role? || user_admin_role?
end
Expand Down
2 changes: 1 addition & 1 deletion config/routes/internal_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
end
resources :generate_invoice, only: [:index, :show]
resources :project_members, only: [:update]
resources :company_users, only: [:index]
resources :employments, only: [:index]
resources :timezones, only: [:index]

resources :companies, only: [:index, :create, :update] do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ChangeCompanyUsersTableNameToEmployment < ActiveRecord::Migration[7.0]
def change
rename_table :company_users, :employments
end
end
41 changes: 19 additions & 22 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions db/seeds/04_company_user.rb

This file was deleted.

6 changes: 6 additions & 0 deletions db/seeds/04_employments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

# Employment Start
@companies.each { |company| @users.each { | user | company.employments.create!(user:) } }
puts "Employment Created"
# Employment End
6 changes: 3 additions & 3 deletions spec/concerns/current_company_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let(:user_2) { create(:user, current_workspace: nil) }

before do
create(:company_user, company:, user:)
create(:employment, company:, user:)
user.add_role :admin, company

@stub_class = Class.new do
Expand Down Expand Up @@ -41,7 +41,7 @@ def initialize(current_user)
describe "when current user doesn't have current workspace id and not associated with any company" do
before do
user.update(current_workspace_id: nil)
user.company_users.destroy
user.employments.destroy
end

it "returns nil" do
Expand All @@ -52,7 +52,7 @@ def initialize(current_user)
describe "when current user is nil" do
before do
user.update(current_workspace_id: nil)
user.company_users.destroy
user.employments.destroy
end

it "returns nil" do
Expand Down
2 changes: 1 addition & 1 deletion spec/concerns/error_handler/test_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def show
describe "#record_not_discarded" do
before do
routes.draw { get "show" => "test#show" }
create(:company_user, company:, user:)
create(:employment, company:, user:)
user.add_role :admin, company
sign_in user
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :company_user do
factory :employment do
company
user
employee_id { Faker::Alphanumeric.alphanumeric(number: 10, min_alpha: 3) }
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/navigation_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:user) { create(:user) }

before do
create(:company_user, user:)
create(:employment, user:)
user.add_role(:owner, user.current_workspace)
allow(controller).to receive(:current_user).and_return(user)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/models/company_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
subject { create(:company) }

describe "Associations" do
it { is_expected.to have_many(:users).through(:company_users) }
it { is_expected.to have_many(:company_users).dependent(:destroy) }
it { is_expected.to have_many(:users).through(:employments) }
it { is_expected.to have_many(:employments).dependent(:destroy) }
it { is_expected.to have_many(:clients).dependent(:destroy) }
it { is_expected.to have_many(:projects).through(:clients).dependent(:destroy) }
it { is_expected.to have_one_attached(:logo) }
Expand Down Expand Up @@ -58,8 +58,8 @@
end

before do
create(:company_user, company_id: company.id, user_id: user_1.id)
create(:company_user, company_id: company.id, user_id: user_2.id)
create(:employment, company_id: company.id, user_id: user_1.id)
create(:employment, company_id: company.id, user_id: user_2.id)
create(:project_member, user_id: user_1.id, project_id: project_1.id)
create(:project_member, user_id: user_1.id, project_id: project_2.id)
create(:project_member, user_id: user_2.id, project_id: project_1.id)
Expand Down
40 changes: 0 additions & 40 deletions spec/models/company_user_spec.rb

This file was deleted.

35 changes: 35 additions & 0 deletions spec/models/employment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Employment, type: :model do
let(:employment) { create(:employment) }
let(:company) { employment.company }
let(:user) { employment.user }

describe "Associations" do
it { is_expected.to belong_to(:company) }
it { is_expected.to belong_to(:user) }
end

describe "Discard" do
it "discards the employments" do
expect { employment.discard! }.to change(company.employments.discarded, :count).by(1)
expect(employment.reload.discarded?).to be_truthy
expect(user.reload.employments.discarded.count).to eq(1)
end

it "does not discard the employments if already discarded" do
employment.discard!
expect { employment.discard! }.to raise_error(Discard::RecordNotDiscarded)
end
end

# TODO:- To be uncommented after UI integration is done
# describe "Validations" do
# it { is_expected.to validate_presence_of(:designation) }
# it { is_expected.to validate_presence_of(:employment_type) }
# it { is_expected.to validate_presence_of(:joined_at) }
# it { is_expected.to validate_presence_of(:employee_id) }
# end
end
Loading