Class: MessagesController

Inherits:
BaseController show all
Defined in:
app/controllers/messages_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#advertise, #cache_action?, #footer_content, #homepage_features, #plaxo, #rss_site_index, #site_index

Methods included from BaseHelper

#add_friend_link, #ajax_spinner_for, #avatar_for, #block_to_partial, #box, #city_cloud, #clippings_link, #commentable_url, #container_title, #excerpt_with_jump, #flash_class, #forum_page?, #is_current_user_and_featured?, #jumbotron, #last_active, #more_comments_links, #page_title, #paginating_links, #possesive, #profile_completeness, #render_jumbotron, #render_widgets, #rounded, #search_posts_title, #search_user_posts_path, #show_footer_content?, #tag_auto_complete_field, #time_ago_in_words, #time_ago_in_words_or_date, #topnav_tab, #truncate_words, #truncate_words_with_highlight, #widget

Methods included from LocalizedApplication

#get_matching_ui_locale, #get_sorted_langs_from_accept_header, #get_valid_lang_from_accept_header, #set_locale

Methods included from AuthenticatedSystem

#login_by_token, #update_last_seen_at

Instance Method Details

#auto_complete_for_usernameObject



8
9
10
11
12
13
# File 'app/controllers/messages_controller.rb', line 8

def auto_complete_for_username
  @usernames = User.active.where.not(login: @user.).pluck(:login)
  respond_to do |format|
    format.json {render :inline => @usernames.to_json}
  end
end

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/messages_controller.rb', line 37

def create
  messages = []

  if params.require(:message)[:to].blank?
    # If 'to' field is empty, call validations to catch other errors
    @message = Message.new(message_params)
    @message.valid?
    render :action => :new and return
  else
    @message = Message.new(message_params)
    @message.recipient= User.where('lower(login) = ?', params.require(:message)[:to].strip.downcase).first
    @message.sender = @user
    unless @message.valid?
      render :action => :new and return
    else
      @message.save!
    end
    flash[:notice] = :message_sent.l
    redirect_to user_messages_path(@user) and return
  end
end

#delete_message_threadsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/messages_controller.rb', line 72

def delete_message_threads
  if request.post?
    if params[:delete]
      params[:delete].each { |id|
        message_thread = MessageThread.find_by_id_and_recipient_id(id, @user.id)
        message_thread.destroy if message_thread
      }
      flash[:notice] = :messages_deleted.l
    end
    redirect_to user_messages_path(@user)
  end

end

#delete_selectedObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/messages_controller.rb', line 59

def delete_selected
  if request.post?
    if params[:delete]
      params[:delete].each { |id|
        @message = Message.where("messages.id = ? AND (sender_id = ? OR recipient_id = ?)", id, @user, @user).first
        @message.mark_deleted(@user) unless @message.nil?
      }
      flash[:notice] = :messages_deleted.l
    end
    redirect_to user_messages_path(@user)
  end
end

#indexObject



15
16
17
18
19
20
21
# File 'app/controllers/messages_controller.rb', line 15

def index
  if params[:mailbox] == "sent"
    @messages = @user.sent_messages.page(params[:page]).per(20)
  else
    @messages = @user.message_threads_as_recipient.order('updated_at DESC').page(params[:page]).per(20)
  end
end

#newObject



29
30
31
32
33
34
35
# File 'app/controllers/messages_controller.rb', line 29

def new
  if params[:reply_to]
    in_reply_to = Message.find_by_id(params[:reply_to])
    message_thread = MessageThread.for(in_reply_to, current_user)
  end
  @message = Message.new_reply(@user, message_thread, params)
end

#showObject



23
24
25
26
27
# File 'app/controllers/messages_controller.rb', line 23

def show
  @message = Message.read(params[:id], @user)
  @message_thread = MessageThread.for(@message, (admin? ? @message.recipient : current_user ))
  @reply = Message.new_reply(@user, @message_thread, params)
end