简体   繁体   中英

Limit number of recipients in rails mailboxer gem

I'm using the mailboxer gem in my app to handle messaging between users and I've got everything working properly but I'm trying to figure out a way to limit the number of users a message can be sent to. I don't know how to override or add to the validations handled in the gem.

Here's my controller :

class ConversationsController < ApplicationController
    before_filter :authenticate_member!
    helper_method :mailbox, :conversation

def index
    @messages_count = current_member.mailbox.inbox({:read => false}).count
    @conversations ||= current_member.mailbox.inbox.all
    @sent ||= current_member.mailbox.sentbox.all
    @trash ||= current_member.mailbox.trash.all
end

def show
    @receipts = conversation.receipts_for(current_member).order('created_at desc').page(params[:page]).per_page(15)

    render :action => :show
    @receipts.mark_as_read
end

def create
    recipient_emails = conversation_params(:recipients).split(',')
    recipients = Member.where(user_name: recipient_emails).all

    conversation = current_member.
      send_message(recipients, *conversation_params(:body, :subject)).conversation

    redirect_to :conversations
end

def reply
  current_member.reply_to_conversation(conversation, *message_params(:body, :subject))
  redirect_to conversation_path
end

def trash  
    conversation.move_to_trash(current_member)  
    redirect_to :conversations 
end

def untrash  
    conversation.untrash(current_member)  
    redirect_to :back 
end

def empty_trash   
    current_member.mailbox.trash.each do |conversation|    
        conversation.receipts_for(current_member).update_all(:deleted => true)
    end
    redirect_to :conversations
end

private

    def mailbox
     @mailbox ||= current_member.mailbox
    end

    def conversation
     @conversation ||= mailbox.conversations.find(params[:id])
    end

    def conversation_params(*keys)
     fetch_params(:conversation, *keys)
    end

    def message_params(*keys)
     fetch_params(:message, *keys)
    end

    def fetch_params(key, *subkeys)
     params[key].instance_eval do
       case subkeys.size
       when 0 then self
       when 1 then self[subkeys.first]
       else subkeys.map{|k| self[k] }
       end
     end
    end

end

由于它是一个数组,因此我发现可以在控制器中的create动作上调用take():

recipient_emails = conversation_params(:recipients).split(',').take(14)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM