Skip to content

Commit

Permalink
Merge branch 'main' of github.com:work-design/rails_com
Browse files Browse the repository at this point in the history
  • Loading branch information
qinmingyuan committed Jan 17, 2025
2 parents 5c09f7a + 88f2b62 commit 1a47bb7
Show file tree
Hide file tree
Showing 71 changed files with 273 additions and 389 deletions.
5 changes: 0 additions & 5 deletions app/controllers/roled/admin/who_roles_controller.rb

This file was deleted.

18 changes: 0 additions & 18 deletions app/controllers/roled/panel/role_whos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@ class Panel::RoleWhosController < Panel::BaseController
before_action :set_role
before_action :set_who_role, only: [:destroy]



def new
@who_role = @role.who_roles.build
end

def create
@who_role = @role.who_roles.build(who_role_params)

unless @who_role.save
render :new, locals: { model: @who_role }, status: :unprocessable_entity
end
end

def update
#@who_role.assign_attributes
end

def destroy
@who_role.destroy
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/roled/panel/roles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def role_params
:description,
:visible,
:default,
:type
role_types_attributes: [:who_type, :id, :_destroy]
)
end

Expand Down
35 changes: 0 additions & 35 deletions app/controllers/roled/panel/who_roles_controller.rb

This file was deleted.

26 changes: 0 additions & 26 deletions app/controllers/roled/panel/whos_controller.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/role/roled/job_title_role.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/role/roled/member_role.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/role/roled/organ_role.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/role/roled/user_role.rb

This file was deleted.

5 changes: 5 additions & 0 deletions app/models/roled/cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Roled
class Cache < ApplicationRecord
include Model::Cache
end
end
5 changes: 5 additions & 0 deletions app/models/roled/cache_role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Roled
class CacheRole < ApplicationRecord
include Model::CacheRole
end
end
35 changes: 31 additions & 4 deletions app/models/roled/ext/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,46 @@ module Ext::Base
extend ActiveSupport::Concern

included do
has_many :roles, class_name: 'Roled::Role', through: :who_roles
belongs_to :cache, class_name: 'Roled::Cache', optional: true

has_many :role_whos, class_name: 'Roled::RoleWho', as: :who
has_many :roles, class_name: 'Roled::Role', through: :role_whos

accepts_nested_attributes_for :role_whos, allow_destroy: true, reject_if: ->(attributes){ attributes.slice('role_id').blank? }

has_many :role_rules, class_name: 'Roled::RoleRule', through: :roles
has_many :tabs, class_name: 'Roled::Tab', through: :roles
has_many :meta_actions, class_name: 'Roled::MetaAction', through: :role_rules
end

def role_hash
all_roles.each_with_object({}) do |role, h|
h.deep_merge! role.role_hash
def compute_role_cache!
p_ids = all_roles.pluck(:id)
p_ids.sort!

cache = Cache.find_or_create_by!(str_role_ids: p_ids.join(','))
self.update cache_id: cache.id
end

def visible_roles
Role.visible
end

def all_roles
member_roles = Role.where(default: true)

roles.where.not(id: member_roles.map(&:id)) + member_roles
end

def role_whos_hash
role_whos.each_with_object({}) do |role_who, h|
h.merge! role_who.role_id => role_who
end
end

def role_hash
cache&.role_hash || {}
end

def has_role?(**options)
if admin?
logger.debug "\e[35m #{base_class_name}_#{id} is admin! \e[0m" if Rails.configuration.x.role_debug
Expand Down
7 changes: 2 additions & 5 deletions app/models/roled/ext/job_title.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ module Ext::JobTitle
extend ActiveSupport::Concern

included do
has_many :who_roles, class_name: 'Roled::WhoJobTitleRole', foreign_key: :who_id, dependent: :destroy_async
include Ext::Base
end

def default_role_hash
Rails.cache.fetch('job_title_role_hash') do
JobTitleRole.find_by(default: true)&.role_hash || {}
end
def visible_roles
Role.joins(:role_types).where(role_types: { who_type: 'Org::JobTitle' }).visible
end

end
Expand Down
5 changes: 2 additions & 3 deletions app/models/roled/ext/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ module Ext::Member
extend ActiveSupport::Concern

included do
has_many :who_roles, class_name: 'Roled::WhoMemberRole', foreign_key: :who_id, dependent: :destroy_async
include Ext::Base
end

def all_roles
roles.or(MemberRole.where(default: true))
def visible_roles
Role.joins(:role_types).where(role_types: { who_type: 'Org::Member' }).visible
end

end
Expand Down
6 changes: 2 additions & 4 deletions app/models/roled/ext/organ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ module Ext::Organ
extend ActiveSupport::Concern

included do
has_many :who_roles, class_name: 'Roled::WhoOrganRole', foreign_key: :who_id, inverse_of: :who, dependent: :destroy_async
accepts_nested_attributes_for :who_roles
include Ext::Base
end

def all_roles
roles.or(OrganRole.where(default: true))
def visible_roles
Role.joins(:role_types).where(role_types: { who_type: 'Org::Organ' }).visible
end

end
Expand Down
5 changes: 2 additions & 3 deletions app/models/roled/ext/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ module Ext::User
extend ActiveSupport::Concern

included do
has_many :who_roles, class_name: 'Roled::WhoUserRole', foreign_key: :who_id, dependent: :destroy_async
include Ext::Base
end

def all_roles
roles.or(UserRole.where(default: true))
def visible_roles
Role.joins(:role_types).where(role_types: { who_type: 'Auth::User' }).visible
end

def admin?
Expand Down
32 changes: 32 additions & 0 deletions app/models/roled/model/cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Roled
module Model::Cache
extend ActiveSupport::Concern

included do
attribute :str_role_ids, :string, index: true
attribute :role_hash, :json, default: {}

has_many :cache_roles, dependent: :delete_all
has_many :roles, through: :cache_roles

after_save :sync_role_caches, if: -> { saved_change_to_str_role_ids? }
end

def sync_role_caches
self.role_ids = str_role_ids.split(',')
reset_role_hash!
end

def reset_role_hash!
self.role_hash = compute_role_hash
self.save
end

def compute_role_hash
roles.each_with_object({}) do |role, h|
h.deep_merge! role.role_hash
end
end

end
end
11 changes: 11 additions & 0 deletions app/models/roled/model/cache_role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Roled
module Model::CacheRole
extend ActiveSupport::Concern

included do
belongs_to :role
belongs_to :cache
end

end
end
24 changes: 17 additions & 7 deletions app/models/roled/model/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Model::Role
extend ActiveSupport::Concern

included do
attribute :type, :string
attribute :name, :string
attribute :tip, :string
attribute :description, :string
Expand All @@ -13,20 +12,29 @@ module Model::Role

belongs_to :organ, class_name: 'Org::Organ', optional: true

has_many :who_roles, dependent: :destroy_async
has_many :role_whos, dependent: :destroy_async
has_many :who_roles
has_many :tabs, dependent: :delete_all
has_many :role_types
has_many :cache_roles, dependent: :destroy_async
has_many :caches, through: :cache_roles, source: :cache
has_many :role_rules, dependent: :destroy_async, autosave: true, inverse_of: :role
has_many :controllers, ->{ distinct }, through: :role_rules
has_many :busynesses, -> { distinct }, through: :role_rules
has_many :role_types, dependent: :delete_all
has_many :tabs, dependent: :delete_all

accepts_nested_attributes_for :role_types, allow_destroy: true

scope :visible, -> { where(visible: true) }

validates :name, presence: true

#before_save :sync_who_types
after_update :set_default, if: -> { default? && (saved_change_to_default? || saved_change_to_type?) }
after_save :sync, if: -> { saved_change_to_role_hash? }
after_save :reset_cache!, if: -> { saved_change_to_role_hash? }
end

def reset_cache!
caches.find_each { |i| i.reset_role_hash! }
end

def has_role?(business: nil, namespace: nil, controller: nil, action: nil, **params)
Expand All @@ -49,8 +57,10 @@ def set_default
self.class.where.not(id: self.id).where(type: self.type).update_all(default: false)
end

def sync_who_types
who_types.exists?(who)
def role_types_hash
role_types.each_with_object({}) do |role_type, h|
h.merge! role_type.who_type => role_type
end
end

def business_on(meta_business)
Expand Down
5 changes: 0 additions & 5 deletions app/models/roled/model/role/job_title_role.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/roled/model/role/member_role.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/roled/model/role/organ_role.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/roled/model/role/user_role.rb

This file was deleted.

10 changes: 10 additions & 0 deletions app/models/roled/model/role_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@ module Model::RoleType
belongs_to :role
end

class_methods do

def who_types
Roled::RoleType.options_i18n(:who_type).each_with_object([]) do |(value, type), arr|
arr << OpenStruct.new(type: type, value: value)
end
end

end

end
end
Loading

0 comments on commit 1a47bb7

Please sign in to comment.