如何为Mailboxer的每个框实现“搜索功能”

时间:2012-07-17 06:16:56

标签: ruby-on-rails ruby-on-rails-3 activerecord

现在,我正在使用名为“mailboxer”的宝石作为邮件系统。https://github.com/ging/mailboxer
我想为收件箱,发件箱和垃圾箱实施“关键字搜索功能”。

如果我不关心搜索关键字,那么通过这样的编码来获取结果没有任何问题

inbox...   @messages = current_user.mailbox.inbox.page(params[:page]).per(10)
sentbox... @messages = current_user.mailbox.sentbox.page(params[:page]).per(10)    
trash...   @messages = current_user.mailbox.trash.page(params[:page]).per(10)

但我有时想用搜索关键字过滤结果 假设此次搜索关键字为'test',结果应该只是通知表中body属性中包含'test'的记录。
我如何为上面的每个内容(如收件箱,发件箱和垃圾箱)执行此操作?

我试过了,但它根本不起作用:(

@messages = current_user.mailbox.inbox.search_messages(@search).page(params[:page]).per(10)
@messages = current_user.mailbox.sentbox.search_messages(@search).page(params[:page]).per(10)
@messages = current_user.mailbox.trash.search_messages(@search).page(params[:page]).per(10)

1 个答案:

答案 0 :(得分:0)

解决方案是修补Mailboxer :: Receipt和Mailboxer :: Models :: Messageable模块以启用按框类型搜索:

class Mailboxer::Receipt < ActiveRecord::Base
  ...
  if Mailboxer.search_enabled
    searchable do
      text :subject, :boost => 5 do
        message.subject if message
      end
      text :body do
        message.body if message
      end
      integer :receiver_id

      # Add mailbox_type to sunspot search field
      string  :mailbox_type
      boolean :trashed
      boolean :deleted
    end
  end
end

在Messageable模块下,我为每种邮箱类型添加了新的search__messages方法:

module Mailboxer
  module Models
    module Messageable

      def search_messages(query)

        # Replaces 'search' with alias 'solr_search' since ransack search clashes with solr search method
        @search = Mailboxer::Receipt.solr_search do
          fulltext query
          with :receiver_id, self.id
        end

        @search.results.map { |r| r.conversation }.uniq
      end

      # Adds additional Mailboxer search functionality to search by box type
      def search_inbox_messages(query)
        @search = Mailboxer::Receipt.solr_search do
          fulltext query
          with :receiver_id, self.id
          with(:mailbox_type).equal_to("inbox")
        end

        @search.results.map { |r| r.conversation }.uniq
      end

      def search_sent_messages(query)
        @search = Mailboxer::Receipt.solr_search do
          fulltext query
          with :receiver_id, self.id
          with(:mailbox_type).equal_to("sentbox")
        end

        @search.results.map { |r| r.conversation }.uniq
      end

      def search_trash_messages(query)
        @search = Mailboxer::Receipt.solr_search do
          fulltext query
          with :receiver_id, self.id
          with :trashed, true
          with :deleted, false
        end

        @search.results.map { |r| r.conversation }.uniq
      end

    end
  end
end

这样,在您的控制器中,您只需使用新定义的方法进行框类型搜索。

current_user.search_inbox_messages(query[:keywords])
current_user.search_sentbox_messages(query[:keywords])
current_user.search_trash_messages(query[:keywords])

重要说明:确保在添加新搜索字段后重新生成Solr索引,否则不会显示任何内容:

bundle exec rake sunspot:solr:reindex

希望这有帮助!