Commit e11ca6a1 by houdelin

add model

parent fb58d251
module CrmUc
module OrganizationRoleable
extend ActiveSupport::Concern
included do
end
def super_role
@super_role ||= roles.detect{|r| r.settings["internal"]}
end
def super_users
@super_users ||= (super_role ? super_role.users : User.none)
end
def super_user_ids
@super_user_ids ||= super_users.pluck(:id)
end
def normal_manager_role
@normal_manager_role ||= roles.find &:normal_manager_role?
end
def normal_managers
@normal_managers ||= (normal_manager_role ? normal_manager_role.users : User.none)
end
module ClassMethods
end
end
end
module OrganizationUserable
extend ActiveSupport::Concern
included do
end
def all_staffs
if AppSettings.uc_enabled?
users
else
users.where(locked_at: nil)
end
end
def strictly_staffs
users.leaves.with_organization_staff_state
end
def all_active_staffs
if AppSettings.uc_enabled?
all_staffs.joins(:user_extra).where(user_extras: {last_sign_in_at: 6.months.ago..Time.now})
else
all_staffs.where(last_sign_in_at: 6.months.ago..Time.now)
end
end
def all_inactive_staffs
if AppSettings.uc_enabled?
all_staffs.joins(:user_extra).where("user_extras.last_sign_in_at < ?", 6.months.ago)
else
all_staffs.where("last_sign_in_at < ?", 6.months.ago)
end
end
module ClassMethods
end
end
module CrmUc
module UserCenterable
extend ActiveSupport::Concern
included do
end
module ClassMethods
def get_user_by_uid(uid)
User.where(uid: uid).take
end
end
end
end
module CrmUc
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
end
class Department < ActiveRecord::Base
self.table_name = AppSettings.table_name('departments')
include DepartmentOwnerable
include DepartmentUserable
include TreeDescendantable
include CrmUc::UserCenterable
tree_depth_as 20
status_scope_as true
extend ActsAsTree::TreeWalker
include ActsAsTree
enum status: [:hide, :visible]
belongs_to :organization
has_many :users_departments
has_many :users, -> { paranoia_scope }, through: :users_departments
has_many :admins_departments
has_many :admins, through: :admins_departments
after_commit -> { Cache::DepartmentService.update_cache_key(self) }
default_scope { visible }
scope :stateless, -> { unscope(where: :status) }
scope :with_path_deep, -> (path_deep = 1) {
path_deep_calcul_sql = %Q{ROUND ( (LENGTH(path) - LENGTH(REPLACE(path, '/', ''))) / LENGTH('/') )}
where("(#{path_deep_calcul_sql}) = ?", path_deep)
}
scope :descendants_for, -> (department_id) {
where("#{self.table_name}.path like ?", "%/#{department_id}/%")
}
scope :self_and_descendants_for, -> (department_id) {
where("(#{self.table_name}.path like ?) or (#{self.table_name}.path rlike ?)", "%/#{department_id}/%", "\/#{department_id}$")
}
scope :self_and_parents_and_descendants_for, -> (department) {
where("(#{self.table_name}.id in (?)) or (#{self.table_name}.path like ?) or (#{self.table_name}.path rlike ?)", department.path.to_s.split('/').map(&:to_i), "%/#{department.id}/%", "\/#{department.id}$")
}
scope :self_and_ancestors_for, -> (department) {
where(id: department.path.to_s.split('/').map(&:to_i))
}
acts_as_tree order: "position asc"
# acts_as_list scope: [:parent_id, :organization_id, "deleted_at"], add_new_at: :bottom
#TODO change single quote to double quotes
acts_as_list scope: '#{parent_id.present? ? %Q{parent_id = #{parent_id}} : %q{parent_id is null}} and #{organization_id.present? ? %Q{organization_id = #{organization_id}} : %q{organization_id is null}} and deleted_at is null', add_new_at: :bottom
validates_associated :organization
acts_as_paranoid
has_one :ding_department
attr_accessor :position_action
delegate :in_ding_exist?, to: :ding_department, allow_nil: true
def can_destroy_this?
!self.users.exists?
end
def parent
return if parent_id.blank?
self.class.stateless.find_by(id: parent_id)
end
def deep_length
tree_depth
end
def descendant_departments
descendant_entities
end
def descendant_department_ids
descendant_entity_ids
end
def self_and_descendant_departments
self_and_descendant_entities
end
def self_and_descendant_department_ids
self_and_descendant_entity_ids
end
def self_and_parent_departments
self_and_ancestor_entities
end
def self_and_parent_and_descendant_departments
Department.where(organization_id: organization_id).self_and_parents_and_descendants_for(self)
end
def child_department_ids
self_and_descendant_entity_ids
end
def to_s
name || id.to_s
end
def can_be_deleted?
! User.stateless.where(deleted_at: nil).joins(:users_department).exists?(["#{UsersDepartment.table_name}.department_id in(?)", self_and_descendant_entities.stateless.select(:id)])
end
alias :is_delete_department :can_be_deleted?
class << self
# :key [:symbol] every node key was node.send(key)
# :fields [:array] every node value
# :proc if block_given? exec proc.call(children_list)
# else will wrappen node's child list to {children: children_list}
def children_tree_data(child, key = :id, fields = [:id ,:name], descendants = nil, &proc)
tree_hash = HashWithIndifferentAccess.new({})
fields = fields.map(&:to_s)
key_val = child.send(key)
#_tree_hash = {key_val => child.attributes.select{|k,_| fields.include?("#{k}")} }
_fields_data = {}
fields.each do |f|
value = child.send(f) rescue nil
_fields_data.reverse_merge! f => value
end
_tree_hash = HashWithIndifferentAccess.new({key_val => _fields_data })
_children = if descendants.is_a?(Array)
descendants.select{|d| d.parent_id == child.id }
else
child.children
end
if _children.present?
children_tree = HashWithIndifferentAccess.new({})
_children.each do |_child|
children_tree.merge! children_tree_data(_child, key, fields, descendants, &proc)
end
if block_given?
_tree_hash[key_val].merge! proc.call(children_tree)
else
_tree_hash[key_val][:children] = children_tree
end
end
tree_hash.merge! _tree_hash
end
def walk_tree_department(departments, level = 0, parent_id = nil, &block)
departments.select{|department| department.parent_id == parent_id}.each do |department|
block.call(department, level)
walk_tree_department(departments, (level + 1), department.id, &block)
end
end
def self_and_descendant_department_ids_by_departments(departments = [])
return [] if departments.blank?
department_ids = []
departments.compact.each do |department|
_ids = department.self_and_descendant_department_ids
next if _ids.blank?
department_ids.concat(_ids)
end
department_ids = department_ids.compact.uniq
department_ids
rescue
[]
end
end
end
class RolesUser < ActiveRecord::Base
self.table_name = AppSettings.table_name('roles_users')
belongs_to :role
belongs_to :user
after_commit :enqueue_reset_customers_flow_time
def enqueue_reset_customers_flow_time
if Role.super.where(id: previous_changes[:role_id]).present?
CustomerCommons::UserFlowTimeWorker.perform_async(user_id)
end
end
end
class UsersAssistDepartment < ActiveRecord::Base
self.table_name = AppSettings.table_name('users_assist_departments')
belongs_to :user
belongs_to :department
include CrmUc::UserCenterable
end
class UsersDepartment < ActiveRecord::Base
self.table_name = AppSettings.table_name('users_departments')
include CrmUc::UserCenterable
belongs_to :user
belongs_to :department
before_update :exchange_assist_department
after_commit :enqueue_reset_customers_flow_time
def enqueue_reset_customers_flow_time
CustomerCommons::UserFlowTimeWorker.perform_async(user_id)
end
def exchange_assist_department
if department_id_changed? && department_id_was
unscoped_user = User.stateless.find_by(id: user_id)
if unscoped_user
unscoped_user.users_assist_departments.find_or_create_by(department_id: department_id_was)
unscoped_user.users_assist_departments.find_by(department_id: department_id).try(:destroy)
end
end
end
end
...@@ -17,5 +17,5 @@ Gem::Specification.new do |spec| ...@@ -17,5 +17,5 @@ Gem::Specification.new do |spec|
spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
spec.add_dependency "rails" spec.add_dependency "rails"
s.add_development_dependency "mysql2" spec.add_development_dependency "mysql2"
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment