Skip to content

Commit

Permalink
Merge pull request #96 from LTe/custom-search-scope
Browse files Browse the repository at this point in the history
Add support for custom search scope
  • Loading branch information
LTe committed Apr 19, 2020
2 parents 69b119a + 3ff3bd0 commit 58d2cbe
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ Usage
```ruby
class User < ActiveRecord::Base
acts_as_messageable :table_name => "table_with_messages", # default 'messages'
:required => :body # default [:topic, :body]
:class_name => "CustomMessages" # default "ActsAsMessageable::Message",
:dependent => :destroy # default :nullify
:group_messages => true # default false
:required => :body, # default [:topic, :body]
:class_name => "CustomMessages", # default "ActsAsMessageable::Message",
:dependent => :destroy, # default :nullify
:group_messages => true, # default false
:search_scope => :custom_search # default :search
end
```

Expand Down
6 changes: 4 additions & 2 deletions lib/acts_as_messageable/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ module ClassMethods
# @param [String] :class_name - message class name
# @param [Array, Symbol] :required - required fields in message
# @param [Symbol] :dependent - dependent option from ActiveRecord has_many method
# @param [Symbol] :search_scope - name of a scope for a full text search
def acts_as_messageable(options = {})
default_options = {
table_name: 'messages',
class_name: 'ActsAsMessageable::Message',
required: %i[topic body],
dependent: :nullify,
group_messages: false
group_messages: false,
search_scope: :search
}
options = default_options.merge(options)

Expand All @@ -37,7 +39,7 @@ def acts_as_messageable(options = {})
messages_class_name.has_ancestry

messages_class_name.table_name = options[:table_name]
messages_class_name.initialize_scopes
messages_class_name.initialize_scopes(options[:search_scope])

messages_class_name.required = Array.wrap(options[:required])
messages_class_name.validates_presence_of messages_class_name.required
Expand Down
8 changes: 2 additions & 6 deletions lib/acts_as_messageable/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ module ActsAsMessageable
module Scopes
extend ActiveSupport::Concern

included do
initialize_scopes
end

module ClassMethods
def initialize_scopes
def initialize_scopes(search_scope)
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|
scope search_scope, lambda { |*args|
where('body like :search_txt or topic like :search_txt', search_txt: "%#{args.first}%")
}
scope :connected_with, lambda { |*args|
Expand Down
15 changes: 15 additions & 0 deletions spec/custom_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ class CustomMessage < ActsAsMessageable::Message
def custom_method; end
end

class CustomSearchUser < ActiveRecord::Base
acts_as_messageable search_scope: :custom_search, class_name: 'CustomMessage'
end

describe 'custom class' do
let(:alice) { User.find_by_email('alice@example.com') }
let(:bob) { User.find_by_email('bob@example.com') }
let(:custom_user) { CustomSearchUser.find_by_email('custom@example.com') }

before do
User.acts_as_messageable class_name: 'CustomMessage', table_name: 'custom_messages'
Expand Down Expand Up @@ -38,4 +43,14 @@ def custom_method; end
expect(@reply_message.root).to eq(@message)
expect(@reply_message.root.class).to eq(CustomMessage)
end

context 'with custom search scope' do
before do
send_message(custom_user, bob, 'Test subject', 'Test body')
end

it 'will use custom search scope' do
expect(custom_user.messages.custom_search('Test body').count).to eq(1)
end
end
end
11 changes: 4 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@pat = User.create email: 'pat@example.com'
@admin = Admin.create email: 'admin@example.com'
@men = Men.create email: 'men@example.com'
@custom_search_user = CustomSearchUser.create email: 'custom@example.com'
end

config.after(:all) do
Expand All @@ -47,13 +48,9 @@ def create_database
create_table(:messages, &TABLE_SCHEMA)
create_table(:custom_messages, &TABLE_SCHEMA)

create_table :users do |t|
t.string :email
end

create_table :admins do |t|
t.string :email
end
create_table(:users, &USER_SCHEMA)
create_table(:admins, &USER_SCHEMA)
create_table(:custom_search_users, &USER_SCHEMA)
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/support/table_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
t.string :ancestry
t.timestamps
end

USER_SCHEMA = lambda do |t|
t.string :email
end

0 comments on commit 58d2cbe

Please sign in to comment.