2011-11-19 06:33:26 +00:00
|
|
|
class ModuleApp
|
2011-11-16 07:21:31 +00:00
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
|
2012-01-30 10:12:18 +00:00
|
|
|
field :key
|
2011-12-23 10:34:21 +00:00
|
|
|
field :title
|
2011-11-16 07:21:31 +00:00
|
|
|
field :version
|
|
|
|
field :organization
|
|
|
|
field :author
|
|
|
|
field :intro
|
|
|
|
field :update_info
|
|
|
|
field :create_date
|
2012-01-05 08:20:51 +00:00
|
|
|
field :enable_frontend,type: Boolean
|
2011-11-16 07:21:31 +00:00
|
|
|
|
2011-12-20 08:47:17 +00:00
|
|
|
field :app_pages ,type: Array
|
2012-01-05 08:20:51 +00:00
|
|
|
field :widgets ,type: Array
|
2011-12-20 08:47:17 +00:00
|
|
|
|
2012-01-13 10:20:04 +00:00
|
|
|
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
|
|
|
|
|
2011-12-20 08:47:17 +00:00
|
|
|
has_one :app_auth,dependent: :delete
|
|
|
|
|
2012-01-30 10:12:18 +00:00
|
|
|
before_save :set_key
|
|
|
|
|
2012-02-15 10:20:44 +00:00
|
|
|
def is_manager?(user)
|
|
|
|
managing_users.include?(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_sub_manager?(user)
|
|
|
|
sub_managing_users.include?(user) || is_manager?(user)
|
|
|
|
end
|
|
|
|
|
2012-01-17 08:20:03 +00:00
|
|
|
def managing_users
|
|
|
|
self.managers.collect{ |t| t.user }
|
|
|
|
end
|
|
|
|
|
|
|
|
def sub_managing_users
|
|
|
|
self.sub_managers.collect{ |t| t.user }
|
|
|
|
end
|
2012-01-13 10:20:04 +00:00
|
|
|
|
|
|
|
def assign_manager(user,assigner)
|
|
|
|
manager = AppManager.first(conditions: {managing_app_id: self.id,user_id: user.id}) rescue nil
|
2012-01-17 08:20:03 +00:00
|
|
|
if manager.nil?
|
2012-01-13 10:20:04 +00:00
|
|
|
manager = self.managers.create(:user => user,:rule_creator => assigner)
|
|
|
|
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
|
2012-01-17 08:20:03 +00:00
|
|
|
if submanager.nil? && !self.managing_users.include?(user)
|
2012-01-13 10:20:04 +00:00
|
|
|
submanager = self.sub_managers.create(:user => user,:rule_creator => assigner)
|
|
|
|
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
|
2012-01-05 08:20:51 +00:00
|
|
|
|
2012-01-13 10:20:04 +00:00
|
|
|
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
|
2011-11-16 07:21:31 +00:00
|
|
|
|
2012-01-30 10:12:18 +00:00
|
|
|
protected
|
2011-12-23 10:34:21 +00:00
|
|
|
|
2012-01-30 10:12:18 +00:00
|
|
|
def set_key
|
|
|
|
self.key = self.title.underscore if self.title
|
|
|
|
end
|
2011-12-23 10:34:21 +00:00
|
|
|
|
2011-11-16 07:21:31 +00:00
|
|
|
end
|