Project initialize

This commit is contained in:
Zin Bo Thit
2026-01-28 09:53:14 +06:30
commit e8380c6e23
139 changed files with 4599 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

View File

20
app/models/department.rb Normal file
View File

@@ -0,0 +1,20 @@
class Department < ApplicationRecord
has_many :users
has_many :tasks
validates :name, presence: true, uniqueness: true
scope :ordered, -> { order(:name) }
def user_count
users.count
end
def task_count
tasks.count
end
def incomplete_task_count
tasks.where.not(status: 'completed').count
end
end

105
app/models/task.rb Normal file
View File

@@ -0,0 +1,105 @@
class Task < ApplicationRecord
belongs_to :department, optional: true
belongs_to :assignee, class_name: 'User', optional: true
belongs_to :creator, class_name: 'User'
has_many :task_activities, dependent: :destroy
enum priority: {
planning: 'planning',
review: 'review',
approval: 'approval',
execution: 'execution',
urgent: 'urgent'
}
enum status: {
open: 'open',
in_progress: 'in_progress',
completed: 'completed',
blocked: 'blocked'
}
validates :title, presence: true
validates :priority, presence: true
validates :status, presence: true
validates :creator, presence: true
scope :ordered, -> { order(priority: :asc, created_at: :desc) }
scope :by_priority, ->(priority) { where(priority: priority) if priority.present? }
scope :by_status, ->(status) { where(status: status) if status.present? }
scope :by_department, ->(department) { where(department: department) if department.present? }
scope :by_assignee, ->(assignee) { where(assignee: assignee) if assignee.present? }
scope :incomplete, -> { where.not(status: :completed) }
scope :complete, -> { where(status: :completed) }
scope :urgent, -> { where(priority: :urgent) }
scope :for_user, ->(user) {
tasks = where(creator: user)
tasks = tasks.or(where(assignee: user))
tasks
}
def self.incomplete
where.not(status: :completed)
end
def self.complete
where(status: :completed)
end
def priority_icon
case priority
when 'planning'
'📋'
when 'review'
'🔍'
when 'approval'
'⏳'
when 'execution'
'🚀'
when 'urgent'
'🔴'
end
end
def priority_color
case priority
when 'planning'
'#007bff'
when 'review'
'#ffc107'
when 'approval'
'#fd7e14'
when 'execution'
'#28a745'
when 'urgent'
'#dc3545'
end
end
def status_badge
case status
when 'open'
{ text: 'Open', color: '#6c757d' }
when 'in_progress'
{ text: 'In Progress', color: '#007bff' }
when 'completed'
{ text: 'Completed', color: '#28a745' }
when 'blocked'
{ text: 'Blocked', color: '#dc3545' }
end
end
def assign?(user)
return true if user.admin?
return false unless user.manager?
user.can_manage_department?(department)
end
def updateable_by?(user)
return true if user.admin?
return true if creator == user
return true if assignee == user && status.in?(['open', 'in_progress'])
false
end
end

View File

@@ -0,0 +1,4 @@
class TaskActivity < ApplicationRecord
belongs_to :task
belongs_to :user
end

45
app/models/user.rb Normal file
View File

@@ -0,0 +1,45 @@
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :lockable, :trackable
belongs_to :department, optional: true
has_many :assigned_tasks, class_name: 'Task', foreign_key: 'assignee_id'
has_many :created_tasks, class_name: 'Task', foreign_key: 'creator_id'
enum role: { admin: 'admin', manager: 'manager', employee: 'employee' }
validates :name, presence: true
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :role, presence: true
scope :by_role, ->(role) { where(role: role) }
scope :by_department, ->(department) { where(department: department) }
scope :ordered, -> { order(:name) }
def can_manage_department?(department)
return true if admin?
return false unless manager?
self.department == department
end
def can_view_task?(task)
return true if admin?
return true if department == task.department
assigned_tasks.include?(task) || created_tasks.include?(task)
end
def can_assign_task?(task)
return true if admin?
return false unless manager?
can_manage_department?(task.department)
end
def department_users
return User.all if admin?
return department.users if manager?
return [self] if employee?
User.none
end
end