orbit-basic/app/models/module_app.rb

109 lines
2.8 KiB
Ruby

class ModuleApp
include Mongoid::Document
include Mongoid::Timestamps
include OrbitCoreLib::ObjectTokenUnility
field :key
field :title
has_many :managers,as: :managing_app ,:class_name => "AppManager" ,:dependent => :destroy#,:foreign_key => "managing_app_id",:inverse_of => :managing_app
has_many :sub_managers,as: :sub_managing_app ,:class_name => "AppManager", :dependent => :destroy#,:foreign_key => "sub_managing_app_id",:inverse_of => :sub_managing_app
# has_many :tags
has_many :page_parts
has_many :pages
has_one :app_auth,dependent: :delete
before_save :set_key
def get_registration
OrbitApp::Module::Registration.find_by_key(key)
end
# %w{label_i18n }.each do |field|
# define_method(field){|var| get_registration.send("get_#{m}") }
# end
def label_i18n
reg = get_registration
reg.nil? ? 'Init is not defined completely' : get_registration.get_label_i18n
end
# def method_missing(m, *args, &block)
# # get_registration.send("get_#{m}")
# end
def using_default_widget?
!get_registration.get_default_widget.blank?
end
def get_widget_for_select
widgets = get_registration.get_widgets
ary = widgets.collect{|k,v| [I18n.t(v["label"]),k]}
ary << [I18n.t('widget.default_widget'),'default_widget']if using_default_widget?
end
def is_manager?(user)
managing_users.include?(user)
end
def is_sub_manager?(user)
sub_managing_users.include?(user) || is_manager?(user)
end
def managing_users
self.managers.collect{ |t| t.user }
end
def sub_managing_users
self.sub_managers.collect{ |t| t.user }
end
def assign_manager(user,assigner)
manager = AppManager.first(conditions: {managing_app_id: self.id,user_id: user.id}) rescue nil
if manager.nil?
manager = self.managers.create(:user_id => user.id,:rule_creator_id => (assigner.id rescue nil))
end
manager
end
def assign_sub_manager(user,assigner)
submanager = AppManager.first(conditions: {sub_managing_app_id: self.id,user_id: user.id}) rescue nil
if submanager.nil? && !self.managing_users.include?(user)
submanager = self.sub_managers.create(:user_id => user.id,:rule_creator_id => (assigner.id rescue nil) )
end
submanager
end
def remove_manager(user)
manager = AppManager.first(conditions: {managing_app_id: self.id,user_id: user.id}) rescue nil
if manager
manager.destroy
else
false
end
end
def remove_sub_manager(user)
submanager = AppManager.first(conditions: {sub_managing_app_id: self.id,user_id: user.id}) rescue nil
if submanager
submanager.destroy
else
false
end
end
def label
I18n.t(label_i18n)
end
protected
def set_key
self.key = self.title.underscore.singularize if self.title
end
end