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 "    \ndef index\n  @mailbox = params[:mailbox].blank? ? \"inbox\" : params[:mailbox]\n  @messages = \#{mapping[:user_object_name]}.send(@mailbox).paginate(:page => params[:page], :per_page => 10)\n  if @mailbox == \"inbox\"\n    @options = [\"Read\",\"Unread\",\"Delete\"]\n  elsif @mailbox == \"outbox\"\n    @options = [\"Delete\"]\n  elsif @mailbox == \"trash\"\n    @options = [\"Read\",\"Unread\",\"Delete\",\"Undelete\"]  \n  end\nend\n\ndef show\n  unless params[:mailbox].blank?\n    @message = \#{mapping[:user_object_name]}.send(params[:mailbox]).find(params[:id])\n    message_from = @message.from.\#{mapping[:user_display_attribute]}\n    message_created_at = @message.created_at.strftime('%A, %B %d, %Y at %I:%M %p')\n    unless params[:mailbox] == \"outbox\"\n      read_unread_messages(true,@message)\n      @message_description = \"On \" + message_created_at +\" <span class='recipient_name'>\" + message_from + \"</span> wrote :\"\n      @user_tokens = @message.from.id\n    else\n      @message_description = \"You wrote to <span class='recipient_name'>\" + message_from + \"</span> at \" + message_created_at + \" :\"\n    end\n  end\nend\n\ndef new\nend\n\ndef create\n  unless params[:user_tokens].blank? or params[:subject].blank? or params[:body].blank?\n    recipients = \#{user_class_name}.find(params[:user_tokens].split(\",\").collect { |id| id.to_i })\n    if \#{mapping[:user_object_name]}.send_message?(params[:subject],params[:body],*recipients)\n      redirect_to mailboxes_url, :notice => 'Successfully send message.'\n    else\n      flash[:alert] = \"Unable to send message.\"\n      render :action => \"new\"\n    end\n  else\n    flash[:alert] = \"Invalid input for sending message.\"\n    render :action => \"new\"\n  end  \nend\n\ndef update\n  unless params[:messages].nil? \n    messages = \#{mapping[:user_object_name]}.send(params[:mailbox]).find(params[:messages])\n    if params[:option].downcase == \"read\"\n      read_unread_messages(true,*messages) \n    elsif params[:option].downcase == \"unread\"\n      read_unread_messages(false,*messages)\n    elsif params[:option].downcase == \"delete\"\n      delete_messages(true,*messages)\n    elsif params[:option].downcase == \"undelete\"\n      delete_messages(false,*messages)\n    end\n    redirect_to box_mailboxes_url(params[:mailbox])\n  else\n    redirect_to box_mailboxes_url(params[:mailbox])\n  end  \nend  \n\ndef token\n  query = \"%\" + params[:q] + \"%\"\n  recipients = \#{user_class_name}.select(\"id,\#{mapping[:user_display_attribute]}\").where(\"\#{mapping[:user_display_attribute]} like ?\", query)\n  respond_to do |format|\n    format.json { render :json => recipients.map { |u| { \"id\" => u.id, \"name\" => u.\#{mapping[:user_display_attribute]}} } }\n  end\nend\n\ndef read_unread_messages(isRead, *messages)\n  messages.each do |msg|\n    if isRead\n      msg.mark_as_read unless msg.read?\n    else\n      msg.mark_as_unread if msg.read?\n    end \n  end\nend\n\ndef delete_messages(isDelete, *messages)\n  messages.each do |msg|  \n    if isDelete\n      msg.delete \n    else\n      msg.undelete \n    end \n  end\nend\n  \n   METHODS\n      \nend\n", __FILE__, __LINE__ + 1