mailr/app/models/folder.rb

144 lines
3.5 KiB
Ruby
Raw Permalink Normal View History

2011-07-27 20:34:30 +02:00
class Folder < ActiveRecord::Base
belongs_to :user
validates_presence_of :name, :on => :create
before_save :check_fill_params, :on => :create
2011-08-16 20:05:58 +02:00
has_many :messages, :dependent => :destroy
2011-07-27 20:34:30 +02:00
2011-09-03 13:07:40 +02:00
SYS_NONE = 0
SYS_TRASH = 1
SYS_INBOX = 2
SYS_SENT = 3
SYS_DRAFTS = 4
default_scope :order => 'name asc'
scope :shown, where(['shown = ?',true])
scope :inbox, where(['sys = ?',SYS_INBOX])
scope :sent, where(['sys = ?',SYS_SENT])
scope :drafts, where(['sys = ?',SYS_DRAFTS])
scope :trash, where(['sys = ?',SYS_TRASH])
scope :sys, where(['sys > ?',SYS_NONE])
2011-07-29 20:05:47 +02:00
def full_name
if parent.empty?
name
else
parent + delim + name
end
end
def depth
parent.split('.').size
end
2011-07-31 22:45:29 +02:00
def selected?(session_folder)
fields = session_folder.split("#")
fields[1].nil? ? fields.insert(0,"") : fields
(fields[1].downcase == name.downcase) && (fields[0].downcase == parent.downcase)
end
2011-08-16 20:05:58 +02:00
def update_stats
unseen = messages.where(:unseen => true).count
total = messages.count
update_attributes(:unseen => unseen, :total => total)
end
def hasFullName?(folder_name)
full_name.downcase == folder_name.downcase
end
2011-09-03 13:07:40 +02:00
def isSystem?
sys > SYS_NONE
end
def isTrash?
sys == SYS_TRASH
end
def isSent?
sys == SYS_SENT
end
def isInbox?
sys == SYS_INBOX
end
def isDrafts?
sys == SYS_DRAFTS
end
def setNone
update_attributes(:sys => SYS_NONE)
end
def setTrash
update_attributes(:sys => SYS_TRASH)
end
def setSent
update_attributes(:sys => SYS_SENT)
end
def setInbox
update_attributes(:sys => SYS_INBOX)
end
def setDrafts
update_attributes(:sys => SYS_DRAFTS)
end
2011-08-16 20:05:58 +02:00
############################################## private section #####################################
2011-07-27 20:34:30 +02:00
private
def check_fill_params
2011-08-16 20:05:58 +02:00
self.total.nil? ? self.total = 0 : self.total
2011-07-27 20:34:30 +02:00
self.unseen.nil? ? self.unseen = 0 : self.unseen
2011-08-24 19:20:13 +02:00
self.parent.nil? ? self.parent = "" : self.parent
self.haschildren.nil? ? self.haschildren = false : self.haschildren
self.delim.nil? ? self.delim = "." : self.delim
2011-09-03 13:07:40 +02:00
self.sys.nil? ? self.sys = SYS_NONE : self.sys
2011-07-27 20:34:30 +02:00
end
def self.createBulk(user,imapFolders)
imapFolders.each do |name,data|
data.attribs.find_index(:Haschildren).nil? ? has_children = 0 : has_children = 1
name_fields = name.split(data.delim)
if name_fields.count > 1
name = name_fields.delete_at(name_fields.size - 1)
parent = name_fields.join(data.delim)
else
name = name_fields[0]
parent = ""
end
2011-08-16 20:05:58 +02:00
user.folders.create(
:name => name,
:parent => parent,
:haschildren => has_children,
:delim => data.delim,
:total => data.messages,
2011-09-03 13:07:40 +02:00
:unseen => data.unseen,
:sys => SYS_NONE)
2011-07-27 20:34:30 +02:00
end
end
2011-08-16 20:05:58 +02:00
def self.find_by_full_name(data)
folder = data.gsub(/\./,'#')
fields = folder.split("#")
nam = fields.delete_at(fields.size - 1)
fields.size.zero? == true ? par = "" : par = fields.join(".")
2011-07-31 22:45:29 +02:00
where(['name = ? and parent = ?',nam,par]).first
2011-07-27 20:34:30 +02:00
end
2011-07-31 22:45:29 +02:00
def self.refresh(mailbox,user)
user.folders.destroy_all
folders=mailbox.folders
Folder.createBulk(user,folders)
end
2011-07-29 20:05:47 +02:00
2011-07-27 20:34:30 +02:00
end