Skip to content

Commit

Permalink
Added searchable DSL 'associate'
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjeno committed Apr 10, 2013
1 parent 3bc8c21 commit 2494eb7
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PATH
specs:
sunspot_association (0.0.1)
activerecord (~> 3.0)
sunspot_solr
sunspot_rails

GEM
remote: http://rubygems.org/
Expand Down Expand Up @@ -85,7 +85,6 @@ GEM
sunspot_rails (1.3.3)
nokogiri
sunspot (= 1.3.3)
sunspot_solr (2.0.0)
sunspot_test (0.4.0)
sunspot_rails (>= 1.2.1)
terminal-table (1.4.5)
Expand Down
1 change: 1 addition & 0 deletions lib/sunspot_association.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'sunspot_association/version'
require 'sunspot_association/model'
require 'sunspot_association/searchable'

module SunspotAssociation
end
29 changes: 29 additions & 0 deletions lib/sunspot_association/searchable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'sunspot/rails'

module SunspotAssociation
module Searchable

extend ActiveSupport::Concern

# Index association fields
#
# Examples:
#
# => associate :text, :company, :name
# => associate :text, :company, :name, :phone
#
def associate(type, association_name, *fields)
options = fields.extract_options!

fields.each do |field|
name = [association_name, field].join('_')
self.send(type, name, options) do
self.send(association_name).send(field)
end
end
end

end
end

Sunspot::DSL::Fields.send(:include, SunspotAssociation::Searchable)
172 changes: 172 additions & 0 deletions spec/sunspot_association/searchable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
require 'spec_helper'

describe SunspotAssociation::Searchable do

## Shared examples

shared_examples_for 'a searchable field' do |searchable_field, type, stored|
stored ||= false

let(:field) { fetch_fields(searchable_field, model) }
let(:data) { field.build }

subject { field }

it { should be_present }
it { data.instance_variable_get(:@stored).should == stored }
it { data.type.class.should == type } if type

end

## Methods

def fetch_fields(name, model)
fields = Sunspot::Setup.for(model).field_factories
fields += Sunspot::Setup.for(model).text_field_factories
fields += Sunspot::Setup.for(model).dynamic_field_factories

fields.flatten!

found_fields = fields.collect do |f|
f if f.name == name
end.compact

found_fields.count > 1 ? found_fields : found_fields.first
end

## Models

with_model :User do
table do |t|
t.belongs_to :company
t.string :name
t.timestamps
end

model do
include Sunspot::Rails::Searchable
belongs_to :company
end
end

with_model :Company do
table do |t|
t.string :name
t.string :phone
t.timestamps
end

model do
has_many :users
end
end

describe :associate do

describe :sunspot_settings do

let(:model) { User }

describe :text do

before(:each) do
User.searchable do
associate :text, :company, :name
end
end

it_should_behave_like 'a searchable field', :company_name, Sunspot::Type::TextType

context 'with multiple' do

describe 'in one call' do

before(:each) do
User.searchable do
associate :text, :company, :name, :phone
end
end

it_should_behave_like 'a searchable field', :company_name, Sunspot::Type::TextType
it_should_behave_like 'a searchable field', :company_phone, Sunspot::Type::TextType

end

describe 'in multiple calls' do

before(:each) do
User.searchable do
associate :text, :company, :name
associate :text, :company, :phone
end
end

it_should_behave_like 'a searchable field', :company_name, Sunspot::Type::TextType
it_should_behave_like 'a searchable field', :company_phone, Sunspot::Type::TextType

end

end

end

describe :string do

before(:each) do
User.searchable do
associate :string, :company, :name
end
end

it_should_behave_like 'a searchable field', :company_name, Sunspot::Type::StringType

context 'with stored' do

before(:each) do
User.searchable do
associate :string, :company, :name, :stored => true
end
end

it_should_behave_like 'a searchable field', :company_name, Sunspot::Type::StringType, true

end

context 'with multiple' do

describe 'in one call' do

before(:each) do
User.searchable do
associate :string, :company, :name, :phone
end
end

it_should_behave_like 'a searchable field', :company_name, Sunspot::Type::StringType
it_should_behave_like 'a searchable field', :company_phone, Sunspot::Type::StringType

end

describe 'in multiple calls' do

before(:each) do
User.searchable do
associate :string, :company, :name
associate :string, :company, :phone
end
end

it_should_behave_like 'a searchable field', :company_name, Sunspot::Type::StringType
it_should_behave_like 'a searchable field', :company_phone, Sunspot::Type::StringType

end

end

end

end

end

end
2 changes: 1 addition & 1 deletion sunspot_association.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "coveralls"

s.add_dependency "activerecord", "~> 3.0"
s.add_dependency "sunspot_solr"
s.add_dependency "sunspot_rails"
end

0 comments on commit 2494eb7

Please sign in to comment.