Module: HasMailbox::Controllers::MethodHelpers

Extended by:
ActiveSupport::Concern
Included in:
MailboxesController
Defined in:
lib/has_mailbox/controllers/method_helpers.rb

Class Method Summary collapse

Class Method Details

.define_methods(user_class_name, mapping = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/has_mailbox/controllers/method_helpers.rb', line 6

def self.define_methods(user_class_name,mapping = {}) 
   
if mapping.empty?
	mapping[:user_object_name] = "current_user"
	mapping[:user_display_attribute] = "email"
end
			
   class_eval <<-METHODS, __FILE__, __LINE__ + 1
   	
def index
	@mailbox = params[:mailbox].blank? ? "inbox" : params[:mailbox]
	@messages = #{mapping[:user_object_name]}.send(@mailbox).paginate(:page => params[:page], :per_page => 10)
	if @mailbox == "inbox"
		@options = ["Read","Unread","Delete"]
	elsif @mailbox == "outbox"
		@options = ["Delete"]
	elsif @mailbox == "trash"
		@options = ["Read","Unread","Delete","Undelete"]	
	end
end

def show
	unless params[:mailbox].blank?
		@message = #{mapping[:user_object_name]}.send(params[:mailbox]).find(params[:id])
		message_from = @message.from.#{mapping[:user_display_attribute]}
		message_created_at = @message.created_at.strftime('%A, %B %d, %Y at %I:%M %p')
		unless params[:mailbox] == "outbox"
			read_unread_messages(true,@message)
			@message_description = "On " + message_created_at +" <span class='recipient_name'>" + message_from + "</span> wrote :"
			@user_tokens = @message.from.id
		else
			@message_description = "You wrote to <span class='recipient_name'>" + message_from + "</span> at " + message_created_at + " :"
		end
	end
end

def new
end

def create
	unless params[:user_tokens].blank? or params[:subject].blank? or params[:body].blank?
		recipients = #{user_class_name}.find(params[:user_tokens].split(",").collect { |id| id.to_i })
		if #{mapping[:user_object_name]}.send_message?(params[:subject],params[:body],*recipients)
			redirect_to mailboxes_url, :notice => 'Successfully send message.'
		else
			flash[:alert] = "Unable to send message."
			render :action => "new"
		end
	else
		flash[:alert] = "Invalid input for sending message."
		render :action => "new"
	end	
end

def update
	unless params[:messages].nil? 
		messages = #{mapping[:user_object_name]}.send(params[:mailbox]).find(params[:messages])
		if params[:option].downcase == "read"
			read_unread_messages(true,*messages)	
		elsif params[:option].downcase == "unread"
			read_unread_messages(false,*messages)
		elsif params[:option].downcase == "delete"
			delete_messages(true,*messages)
		elsif params[:option].downcase == "undelete"
			delete_messages(false,*messages)
		end
		redirect_to box_mailboxes_url(params[:mailbox])
	else
		redirect_to box_mailboxes_url(params[:mailbox])
	end	
end  

def token
	query = "%" + params[:q] + "%"
	recipients = #{user_class_name}.select("id,#{mapping[:user_display_attribute]}").where("#{mapping[:user_display_attribute]} like ?", query)
	respond_to do |format|
		format.json { render :json => recipients.map { |u| { "id" => u.id, "name" => u.#{mapping[:user_display_attribute]}} } }
	end
end

def read_unread_messages(isRead, *messages)
	messages.each do |msg|
		if isRead
			msg.mark_as_read unless msg.read?
		else
			msg.mark_as_unread if msg.read?
		end	
	end
end

def delete_messages(isDelete, *messages)
	messages.each do |msg|  
		if isDelete
			msg.delete	
		else
			msg.undelete	
		end	
	end
end
	
   METHODS
			
end