activesearch 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjhhY2NlMzY1NDJmZGU1YjdlNzk0NGIyOGFhOTM5M2RkNjA3YTM1MA==
4
+ MTU2NGJjNGExNDVjNjY5OTk4Njk0OTQwM2FhZjExZmM0Y2E2MmFkNg==
5
5
  data.tar.gz: !binary |-
6
- YTdhYzQwM2NiNGI5NWFiYmY5ZjA3YzYyOGU4ZTJhMjU0ZTY5YTRjMQ==
6
+ Njg2Yzk2OTIxMzNkZjY4OTFlZjhmYWQ1OWU2NDVhN2M4NGNiN2Y4Mg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YzAxNTg0M2NkYzRiYmE2MzMzOThhZTI4MzkyMWU5N2Y2YjZmMGMyOGJhZmQy
10
- ODk5MTBiNTczNTkwNjYwODdkM2JmM2Q0ODViNmE4NWYwNzg1OTU5NGZkMWUz
11
- NGZmYzI4YjM0NzA5YTFlYjFiNWE1Y2IzOGQ5NDMwNDc3NTc3NmQ=
9
+ YzliZTAwNDgwMjQ5MGMxNTM0ZDM4Njc4MDE5NjI2MDFmNTBlMDQwZDY0ZGZk
10
+ NzE4ZTZlYWQyNGNiNDc1NGE0ZTNiYTE0MzcwNjMxODU2ODI4YTY5OGRmY2Jk
11
+ ZTVmNjFhYTE0YjY0MmU5ODYwNTdlYWU0YmQ2MjM5MzMzYmMyZjQ=
12
12
  data.tar.gz: !binary |-
13
- YWM5Mzg0YjA5MDllZjhhZDllMzBkZDlkODU2YWRlZDQ0NjU2YTk0ZWQ4Yzg2
14
- MGZlYmViMWNmNmY0MGYxYTdlYjNiYTY3NzNkNDA3MjlmZDY2YWRhYmQ4NTky
15
- YmIzMzg5MzYyOGMwM2NlYzFhOWIyZDQxNTMzMWZmNTI5NzEyNmQ=
13
+ YTgzMTI3ZDAwMDJlYjJkZjRlY2VlMTE2ODdhOWEwZWY5NWIyMDE3M2RiNTUy
14
+ NmU2ZjUwNzQ2ODJkYjUyN2E2Mzg2NWZkOGJjZjU1ZmQ1MjFhODljZTlhYWRi
15
+ MzM5MDkyNWNjMmExZmUyMjczMjc1ZGJjNGQzZDk1NjlkMGE3OGY=
@@ -27,8 +27,8 @@ module ActiveSearch::Algolia
27
27
  self.class.put("/#{id}", body: object.to_json)
28
28
  end
29
29
 
30
- def query(text)
31
- self.class.get("", query: {query: text})
30
+ def query(text, extras = {})
31
+ self.class.get("", query: extras.merge!(query: text))
32
32
  end
33
33
  end
34
34
  end
@@ -3,9 +3,10 @@ require "activesearch/base"
3
3
  require "activesearch/proxy"
4
4
 
5
5
  module ActiveSearch
6
- def self.search(text)
7
- Proxy.new(text) do |text|
8
- Algolia::Client.new.query(text)["hits"].map! do |hit|
6
+ def self.search(text, conditions = {})
7
+ Proxy.new(text, conditions) do |text, conditions|
8
+
9
+ Algolia::Client.new.query(text, tags: conditions_to_tags(conditions))["hits"].map! do |hit|
9
10
  if hit["_tags"]
10
11
  hit["_tags"].each do |tag|
11
12
  k, v = tag.split(':')
@@ -18,6 +19,11 @@ module ActiveSearch
18
19
  end
19
20
  end
20
21
 
22
+ protected
23
+ def self.conditions_to_tags(conditions)
24
+ conditions.map { |c| c.join(':') }.join(',')
25
+ end
26
+
21
27
  module Algolia
22
28
  def self.included(base)
23
29
  base.class_eval do
@@ -7,14 +7,11 @@ module ActiveSearch
7
7
  field :_original_id, type: BSON::ObjectId
8
8
  field :_keywords
9
9
  field :_stored, type: Hash, default: {}
10
+ alias_method :to_hash, :_stored
10
11
 
11
12
  index :_keywords
12
13
  index [:_original_type, :_original_id], unique: true
13
14
 
14
- def to_hash
15
- _stored
16
- end
17
-
18
15
  def store_fields(original, fields, options)
19
16
  if options && options[:store]
20
17
  self._stored = {}
@@ -1,12 +1,16 @@
1
1
  require 'activesearch/base'
2
+ require 'activesearch/proxy'
2
3
  require 'activesearch/mongoid/model'
3
4
 
4
5
  module ActiveSearch
5
6
 
6
- # TODO: Wrap this so all engines behave consistently
7
- def self.search(text)
8
- text = text.split(/\s+/)
9
- Mongoid::Model.where(:_keywords.in => text + text.map { |word| "#{I18n.locale}:#{word}"})
7
+ def self.search(text, conditions = {})
8
+ Proxy.new(text, conditions) do |text, conditions|
9
+ text = text.split(/\s+/)
10
+ conditions.keys.each { |k| conditions["_stored.#{k}"] = conditions.delete(k) }
11
+ conditions.merge!(:_keywords.in => text + text.map { |word| "#{I18n.locale}:#{word}"})
12
+ Mongoid::Model.where(conditions)
13
+ end
10
14
  end
11
15
 
12
16
  module Mongoid
@@ -4,13 +4,14 @@ module ActiveSearch
4
4
  class Proxy
5
5
  include Enumerable
6
6
 
7
- def initialize(text, &implementation)
7
+ def initialize(text, conditions, &implementation)
8
8
  @text = text
9
+ @conditions = conditions
9
10
  @implementation = implementation
10
11
  end
11
12
 
12
13
  def each(&block)
13
- @implementation.call(@text).each { |result| block.call(Result.new(result)) }
14
+ @implementation.call(@text, @conditions).each { |result| block.call(Result.new(result)) }
14
15
  end
15
16
  end
16
17
  end
@@ -2,12 +2,10 @@ module ActiveSearch
2
2
  class Result < Hash
3
3
  def initialize(result)
4
4
  result.to_hash.each do |k,v|
5
- self[k.to_s] = v unless v.nil?
5
+ unless v.nil?
6
+ self[k.to_s] = v.respond_to?(:has_key?) && v.has_key?(I18n.locale.to_s) ? v[I18n.locale.to_s] : v
7
+ end
6
8
  end
7
9
  end
8
-
9
- def attributes
10
- self
11
- end
12
10
  end
13
11
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveSearch
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.14"
3
3
  end
data/spec/engines_spec.rb CHANGED
@@ -27,15 +27,16 @@ Dir[File.join(File.dirname(__FILE__), 'models', '*.rb')].map { |f| File.basename
27
27
 
28
28
  before do
29
29
  cleanup(engine)
30
- @findable = Object.const_get("#{engine}Model").create(title: "Findable Findable", junk: "Junk field")
31
- @quite_findable = Object.const_get("#{engine}Model").create(title: "Some title", text: "Findable text")
32
- @another = Object.const_get("Another#{engine}Model").create(title: "Another <strong>findable</strong> title with tags")
33
- @junk = Object.const_get("#{engine}Model").create(title: "Junk", junk: "Not Findable junk")
34
- @special = Object.const_get("#{engine}Model").create(title: "Not findable because it's special", special: true)
30
+ @findable = Object.const_get("#{engine}Model").create(title: "Findable Findable", junk: "Junk field", scope_id: 1)
31
+ @quite_findable = Object.const_get("#{engine}Model").create(title: "Some title", text: "Findable text", scope_id: 1)
32
+ @another = Object.const_get("Another#{engine}Model").create(title: "Another <strong>findable</strong> title with tags", scope_id: 1)
33
+ @junk = Object.const_get("#{engine}Model").create(title: "Junk", junk: "Not Findable junk", scope_id: 1)
34
+ @special = Object.const_get("#{engine}Model").create(title: "Not findable because it's special", special: true, scope_id: 1)
35
+ @foreign = Object.const_get("#{engine}Model").create(title: "Findable", scope_id: 2)
35
36
  end
36
37
 
37
38
  it "should find the expected documents" do
38
- results = ActiveSearch.search("findable").map { |doc| doc.to_hash.select { |k,v| %w[title junk virtual].include?(k.to_s) } }
39
+ results = ActiveSearch.search("findable", scope_id: 1).map { |doc| doc.select { |k,v| %w[title junk virtual].include?(k.to_s) } }
39
40
  results.sort_by { |result| result["title"] }.should == [
40
41
  {
41
42
  "title" => "Another <strong>findable</strong> title with tags",
@@ -55,7 +56,7 @@ Dir[File.join(File.dirname(__FILE__), 'models', '*.rb')].map { |f| File.basename
55
56
 
56
57
  it "should remove destroyed documents from index" do
57
58
  @findable.destroy
58
- ActiveSearch.search("findable").count.should == 2
59
+ ActiveSearch.search("findable").count.should == 3
59
60
  end
60
61
  end
61
62
  end
@@ -7,8 +7,9 @@ class AlgoliaModel < ActiveMimic
7
7
  attribute :text
8
8
  attribute :junk
9
9
  attribute :special, default: false
10
+ attribute :scope_id, type: Integer
10
11
 
11
- search_by [:title, :text, store: [:title, :junk]], if: lambda { !self.special }
12
+ search_by [:title, :text, store: [:title, :junk, :scope_id]], if: lambda { !self.special }
12
13
 
13
14
  end
14
15
 
@@ -16,7 +17,8 @@ class AnotherAlgoliaModel < ActiveMimic
16
17
  include ActiveSearch::Algolia
17
18
 
18
19
  attribute :title, type: String
19
- search_by [:title, store: [:title, :virtual]]
20
+ attribute :scope_id, type: Integer
21
+ search_by [:title, store: [:title, :virtual, :scope_id]]
20
22
 
21
23
  def virtual
22
24
  "virtual"
@@ -11,7 +11,8 @@ class MongoidModel
11
11
  field :text, type: String
12
12
  field :junk, type: String
13
13
  field :special, type: Boolean, default: false
14
- search_by [:title, :text, store: [:title, :junk]], unless: :special
14
+ field :scope_id, type: Integer
15
+ search_by [:title, :text, store: [:title, :junk, :scope_id]], unless: :special
15
16
  end
16
17
 
17
18
  class AnotherMongoidModel
@@ -19,10 +20,11 @@ class AnotherMongoidModel
19
20
  include ActiveSearch::Mongoid
20
21
 
21
22
  field :title, type: String
23
+ field :scope_id, type: Integer
22
24
  search_by :options_for_search
23
25
 
24
26
  def options_for_search
25
- [:title, :text, store: [:title, :virtual]]
27
+ [:title, :text, store: [:title, :virtual, :scope_id]]
26
28
  end
27
29
 
28
30
  def virtual
data/spec/mongoid_spec.rb CHANGED
@@ -26,17 +26,13 @@ describe ActiveSearch::Mongoid do
26
26
  end
27
27
 
28
28
  it "should be able to find by different locales" do
29
- ActiveSearch.search("english").first._stored["title"]["en"].should == "<strong>English</strong> English"
29
+ ActiveSearch.search("english").first["title"].should == "<strong>English</strong> English"
30
30
  I18n.with_locale(:es) do
31
- ActiveSearch.search("español").first._stored["title"]["es"].should == "Español Español"
31
+ ActiveSearch.search("español").first["title"].should == "Español Español"
32
32
  end
33
33
  end
34
34
 
35
- it "should store localized keywords" do
35
+ it "should store localized keywords with tags stripped" do
36
36
  ActiveSearch::Mongoid::Model.where(_original_type: "LocalizedMongoidModel", _original_id: @localized.id).first._keywords.should == ["en:english", "es:español"]
37
37
  end
38
-
39
- it "should strip tags" do
40
- ActiveSearch.search("english").first._keywords.should == ['en:english', 'es:español']
41
- end
42
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activesearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Alvarez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-19 00:00:00.000000000 Z
11
+ date: 2013-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport