diff --git a/README.md b/README.md index 566ece1a8..409f65a35 100644 --- a/README.md +++ b/README.md @@ -350,7 +350,23 @@ Search ## Search text from messages ```ruby -@alice.messages.search("Search me") @alice seach text "Search me" from all messages +@alice.messages.search("Search me") @alice search text "Search me" from all messages +``` + +## Custom search scope name + +in `config/initializers/acts-as-messageable.rb` + +```ruby +ActsAsMessageable.search_scope_name = :text_search +``` + +or + +```ruby +ActsAsMessageable.config do |config| + config.search_scope_name = :text_search +end ``` Copyright © 2011-2012 Piotr Niełacny (http://ruby-blog.pl), released under the MIT license diff --git a/lib/acts-as-messageable.rb b/lib/acts-as-messageable.rb index 42b162771..3d913c65c 100644 --- a/lib/acts-as-messageable.rb +++ b/lib/acts-as-messageable.rb @@ -6,6 +6,13 @@ module ActsAsMessageable autoload :Rails3, 'acts-as-messageable/rails3' autoload :Rails4, 'acts-as-messageable/rails4' + mattr_accessor :search_scope_name + self.search_scope_name = :search + + def self.config + yield self + end + def self.rails_api if Rails::VERSION::MAJOR >= 4 Rails4 diff --git a/lib/acts-as-messageable/scopes.rb b/lib/acts-as-messageable/scopes.rb index fd0f4ff10..020aa321d 100644 --- a/lib/acts-as-messageable/scopes.rb +++ b/lib/acts-as-messageable/scopes.rb @@ -12,8 +12,9 @@ module ClassMethods def initialize_scopes scope :are_from, lambda { |*args| where(:sent_messageable_id => args.first, :sent_messageable_type => args.first.class.name) } scope :are_to, lambda { |*args| where(:received_messageable_id => args.first, :received_messageable_type => args.first.class.name) } - scope :search, lambda { |*args| where("body like :search_txt or topic like :search_txt",:search_txt => "%#{args.first}%")} - scope :connected_with, lambda { |*args| where("(sent_messageable_type = :sent_type and + scope ActsAsMessageable.search_scope_name, + lambda { |*args| where("body like :search_txt or topic like :search_txt", :search_txt => "%#{args.first}%") } + scope :connected_with, lambda { |*args| where("(sent_messageable_type = :sent_type and sent_messageable_id = :sent_id and sender_delete = :s_delete and sender_permanent_delete = :s_perm_delete) or (received_messageable_type = :received_type and diff --git a/spec/config_spec.rb b/spec/config_spec.rb new file mode 100644 index 000000000..b1fbbb006 --- /dev/null +++ b/spec/config_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe 'configuration' do + share_examples_for :custom_search_scope_name do + before do + @reply_message = @message.reply("Re: Topic", "Body : I am fine") + @reply_reply_message = @reply_message.reply("Re: Re: Topic", "Fine too") + end + + it "bob should be able to search text from messages" do + recordset = @bob.messages.text_search("I am fine") + recordset.count.should == 1 + recordset.should_not be_nil + end + end + + context 'without config block' do + before do + ActsAsMessageable.search_scope_name = :text_search + User.acts_as_messageable + @message = send_message + end + + describe "search text from messages using custom scope name" do + it_behaves_like :custom_search_scope_name + end + end + + context 'with config block' do + before do + ActsAsMessageable.config do |config| + config.search_scope_name = :text_search + end + + User.acts_as_messageable + @message = send_message + end + + describe "search text from messages using custom scope name" do + it_behaves_like :custom_search_scope_name + end + end +end