algoliasearch-rails 1.3.10 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b98457654ea69bba65dc9261a30eb05e56112d79
4
- data.tar.gz: 9e4a4c26ee00fb820627468a4eacb3ef25f722ce
3
+ metadata.gz: c7c04861a87809fcc0e5b1f6a3195b38e62e5634
4
+ data.tar.gz: 1f354ed3067a4c438deef7d1355eaeb98c75edd1
5
5
  SHA512:
6
- metadata.gz: cc9d4e8018b23c32f427e890f1288bfa66387f4f983dd01a2f45f4cad1eec7c6ae56d5e1bec542a8eec80f181c2d347176e16e45e50439ed9daaedb148c558e2
7
- data.tar.gz: 7c4d013065b66d6a0415c72ca6440896256440f4faaa872b3ff25982a425868091ce7d979e9409ec0b26065b402d26832f011af9fd3058d0687eaf1c3550bf51
6
+ metadata.gz: 4329b3f1440d9dbb810da2225dbf04de248cbb771cdddfd508a44bf038b643ee6ca76fafd3a3282086dda476073dca63478522ed9141478579aaae6154f70618
7
+ data.tar.gz: 56b9c819f5e5d7b7cd79c664adecc2f8b770b19c45113682a0193d666090712c755e3ee60350f22841ec8c38da331e3b05af102fe5835ba923d3471d28ac8785
data/ChangeLog CHANGED
@@ -1,5 +1,9 @@
1
1
  CHANGELOG
2
2
 
3
+ 2013-11-25 1.4.1
4
+
5
+ * ability to specify a block associated to an attribute as value
6
+
3
7
  2013-11-22 1.3.10
4
8
 
5
9
  * updated algoliasearch.js (2.3.2)
data/README.md CHANGED
@@ -149,6 +149,21 @@ class Contact < ActiveRecord::Base
149
149
  end
150
150
  ```
151
151
 
152
+ You can use a block to specify a complex attribute value
153
+
154
+ ```ruby
155
+ class Contact < ActiveRecord::Base
156
+ include AlgoliaSearch
157
+
158
+ algoliasearch do
159
+ attribute :email
160
+ attribute :full_name do
161
+ "#{first_name} #{last_name}"
162
+ end
163
+ end
164
+ end
165
+ ```
166
+
152
167
  Indexing
153
168
  ---------
154
169
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.10
1
+ 1.4.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "algoliasearch-rails"
8
- s.version = "1.3.10"
8
+ s.version = "1.4.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Algolia"]
12
- s.date = "2013-11-22"
12
+ s.date = "2013-11-25"
13
13
  s.description = "AlgoliaSearch integration to your favorite ORM"
14
14
  s.email = "contact@algolia.com"
15
15
  s.extra_rdoc_files = [
@@ -42,7 +42,7 @@ module AlgoliaSearch
42
42
 
43
43
  end
44
44
 
45
- class IndexOptions
45
+ class IndexSettings
46
46
 
47
47
  # AlgoliaSearch settings
48
48
  OPTIONS = [:attributesToIndex, :minWordSizefor1Typo,
@@ -56,26 +56,31 @@ module AlgoliaSearch
56
56
  end
57
57
  end
58
58
 
59
- # attributes to consider
60
- attr_accessor :attributes
61
-
62
59
  def initialize(block)
63
60
  instance_exec(&block) if block
64
61
  end
65
62
 
66
- def attribute(*names)
67
- self.attributes ||= []
68
- self.attributes += names
63
+ def attribute(*names, &block)
64
+ raise ArgumentError.new('Cannot pass multiple attribute names if block given') if block_given? and names.length > 1
65
+ @attributes ||= {}
66
+ names.each do |name|
67
+ @attributes[name] = block_given? ? Proc.new { |o| o.instance_eval(&block) } : Proc.new { |o| o.send(name) }
68
+ end
69
+ end
70
+
71
+ def get_attributes(object)
72
+ return object.attributes if @attributes.nil? or @attributes.length == 0
73
+ Hash[@attributes.map { |name, value| [name.to_s, value.call(object) ] }]
69
74
  end
70
75
 
71
- def get(setting)
72
- instance_variable_get("@#{setting}")
76
+ def get_setting(name)
77
+ instance_variable_get("@#{name}")
73
78
  end
74
79
 
75
80
  def to_settings
76
81
  settings = {}
77
82
  OPTIONS.each do |k|
78
- v = get(k)
83
+ v = get_setting(k)
79
84
  settings[k] = v if !v.nil?
80
85
  end
81
86
  settings
@@ -86,7 +91,10 @@ module AlgoliaSearch
86
91
  module ClassMethods
87
92
 
88
93
  def algoliasearch(options = {}, &block)
89
- @index_options = IndexOptions.new(block_given? ? Proc.new : nil)
94
+ @index_settings = IndexSettings.new(block_given? ? Proc.new : nil)
95
+ @settings = @index_settings.to_settings
96
+ @options = { type: model_name, per_page: @index_settings.get_setting(:hitsPerPage) || 10, page: 1 }.merge(options)
97
+
90
98
  attr_accessor :highlight_result
91
99
 
92
100
  if options[:synchronous] == true
@@ -100,8 +108,6 @@ module AlgoliaSearch
100
108
  unless options[:auto_remove] == false
101
109
  after_destroy { |searchable| searchable.remove_from_index! } if respond_to?(:after_destroy)
102
110
  end
103
-
104
- @options = { type: model_name, per_page: @index_options.get(:hitsPerPage) || 10, page: 1 }.merge(options)
105
111
  end
106
112
 
107
113
  def without_auto_index(&block)
@@ -118,7 +124,7 @@ module AlgoliaSearch
118
124
  ensure_init
119
125
  last_task = nil
120
126
  find_in_batches(batch_size: batch_size) do |group|
121
- objects = group.map { |o| attributes(o).merge 'objectID' => o.id.to_s }
127
+ objects = group.map { |o| @index_settings.get_attributes(o).merge 'objectID' => o.id.to_s }
122
128
  last_task = @index.save_objects(objects)
123
129
  end
124
130
  @index.wait_task(last_task["taskID"]) if last_task and synchronous == true
@@ -128,9 +134,9 @@ module AlgoliaSearch
128
134
  return if @without_auto_index_scope
129
135
  ensure_init
130
136
  if synchronous
131
- @index.add_object!(attributes(object), object.id.to_s)
137
+ @index.add_object!(@index_settings.get_attributes(object), object.id.to_s)
132
138
  else
133
- @index.add_object(attributes(object), object.id.to_s)
139
+ @index.add_object(@index_settings.get_attributes(object), object.id.to_s)
134
140
  end
135
141
  end
136
142
 
@@ -162,17 +168,15 @@ module AlgoliaSearch
162
168
  end
163
169
 
164
170
  def ensure_init
165
- new_settings = @index_options.to_settings
166
- return if @index and !index_settings_changed?(@settings, new_settings)
171
+ return if @index
167
172
  @index = Algolia::Index.new(index_name)
168
173
  current_settings = @index.get_settings rescue nil # if the index doesn't exist
169
- @index.set_settings(new_settings) if index_settings_changed?(current_settings, new_settings)
170
- @settings = new_settings
174
+ @index.set_settings(@settings) if index_settings_changed?(current_settings, @settings)
171
175
  end
172
176
 
173
177
  def must_reindex?(object)
174
178
  return true if object.id_changed?
175
- attributes(object).each do |k, v|
179
+ @index_settings.get_attributes(object).each do |k, v|
176
180
  changed_method = "#{k}_changed?"
177
181
  return true if object.respond_to?(changed_method) && object.send(changed_method)
178
182
  end
@@ -187,11 +191,6 @@ module AlgoliaSearch
187
191
 
188
192
  private
189
193
 
190
- def attributes(object)
191
- return object.attributes if @index_options.attributes.nil? or @index_options.attributes.length == 0
192
- Hash[@index_options.attributes.map { |attr| [attr.to_s, object.send(attr)] }]
193
- end
194
-
195
194
  def index_settings_changed?(prev, current)
196
195
  return true if prev.nil?
197
196
  current.each do |k, v|
@@ -233,7 +232,7 @@ module AlgoliaSearch
233
232
  end
234
233
 
235
234
  def mark_must_reindex
236
- @must_reindex = self.class.must_reindex?(self)
235
+ @must_reindex = new_record? || self.class.must_reindex?(self)
237
236
  true
238
237
  end
239
238
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: algoliasearch-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.10
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Algolia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-22 00:00:00.000000000 Z
11
+ date: 2013-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json