Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable search scope name #71

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/acts-as-messageable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ module ActsAsMessageable
autoload :Rails3, 'acts-as-messageable/rails3'
autoload :Rails4, 'acts-as-messageable/rails4'

mattr_accessor :search_scope_name
@@search_scope_name = :search

def self.config
yield self
end

def self.rails_api
if Rails::VERSION::MAJOR >= 4
Rails4
Expand Down
5 changes: 3 additions & 2 deletions lib/acts-as-messageable/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}%") }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [143/80]
Align the parameters of a method call if they span more than one line.
Use the new Ruby 1.9 hash syntax.

scope :connected_with, lambda { |*args| where("(sent_messageable_type = :sent_type and

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [97/80]
Block body expression is on the same line as the block start.

sent_messageable_id = :sent_id and
sender_delete = :s_delete and sender_permanent_delete = :s_perm_delete) or
(received_messageable_type = :received_type and
Expand Down
43 changes: 43 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


describe 'configuration' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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