限制rails mailboxer gem中的收件人数量

时间:2014-06-15 02:11:24

标签: ruby-on-rails validation mailboxer

我在我的应用中使用邮箱gem来处理用户之间的消息传递,并且我已经让一切正常运行但是我试图找出一种方法来限制用户的消息数量可以发送到。我不知道如何覆盖或添加gem中处理的验证。

这是我的控制器

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

1 个答案:

答案 0 :(得分:0)

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

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