supplejack_client 1.0.2 → 1.0.4

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 950c231c1f7bf0f55b66622babdf1524376b02da
4
- data.tar.gz: 497695e5a2d9ba4a8bc4fa690a9291eb328d9625
3
+ metadata.gz: 0f40b6b28bd0108c37c997ef3fd6c0e346e48341
4
+ data.tar.gz: 0eb25481a3fdea69d0597739ba2aeb966fc671f8
5
5
  SHA512:
6
- metadata.gz: 31e663f7fcd4154e2d0bb71cfd4a076dcddc9ed426f61beb68cbf901f6272f3a65c47b06c442ada1805fbf73b5378f8784fcaa87d619484d6bdb622357e2cb70
7
- data.tar.gz: fbc71a0fbcbb7c1421c71a19ab0d41a0fb40d73abf476dd288fa659b4026ff29e023fe791d1da6e99cd4ced6775e70120c3d6cbc6111d70b376405b01c3bbcad
6
+ metadata.gz: f4520818d551545b38294f5d157fe6b52caf1af07379cf329e138ab67b674687944305a3503d807e1f9966834e1e9245a4545228071b12ffed50e32d06d9762e
7
+ data.tar.gz: 8a72b8a32b0365258c722781db9b52ddcd29705612d7e3a0170f5d1220d6bce9c89b9f5cad4856db6a30de55f4c70f9bd57532aad77cad29634afece752a1d9a
@@ -34,12 +34,13 @@ module Supplejack
34
34
  SEARCH_KLASS = nil
35
35
  FIELDS = [:default]
36
36
  SUPPLEJACK_FIELDS = []
37
- ADMIN_FIELDS = []
37
+ SPECIAL_FIELDS = []
38
38
  ENABLE_DEBUGGING = false
39
39
  ENABLE_CACHING = false
40
40
  ATTRIBUTE_TAG = :p
41
41
  LABEL_TAG = :strong
42
42
  LABEL_CLASS = nil
43
+ STICKY_FACETS = false
43
44
 
44
45
  VALID_OPTIONS_KEYS = [
45
46
  :api_key,
@@ -58,12 +59,13 @@ module Supplejack
58
59
  :search_klass,
59
60
  :fields,
60
61
  :supplejack_fields,
61
- :admin_fields,
62
+ :special_fields,
62
63
  :enable_debugging,
63
64
  :enable_caching,
64
65
  :attribute_tag,
65
66
  :label_tag,
66
- :label_class
67
+ :label_class,
68
+ :sticky_facets
67
69
  ]
68
70
 
69
71
  SINGLE_VALUE_METHODS = [
@@ -96,6 +98,7 @@ module Supplejack
96
98
  self.facets = FACETS
97
99
  self.facets_per_page = FACETS_PER_PAGE
98
100
  self.facets_sort = FACETS_SORT
101
+ self.sticky_facets = STICKY_FACETS
99
102
  self.single_value_methods = SINGLE_VALUE_METHODS
100
103
  self.search_attributes = SEARCH_ATTRIBUTES
101
104
  self.url_format = URL_FORMAT
@@ -107,7 +110,7 @@ module Supplejack
107
110
  self.search_klass = SEARCH_KLASS
108
111
  self.fields = FIELDS
109
112
  self.supplejack_fields = SUPPLEJACK_FIELDS
110
- self.admin_fields = ADMIN_FIELDS
113
+ self.special_fields = SPECIAL_FIELDS
111
114
  self.enable_debugging = ENABLE_DEBUGGING
112
115
  self.enable_caching = ENABLE_CACHING
113
116
  self.attribute_tag = ATTRIBUTE_TAG
@@ -41,10 +41,10 @@ module Supplejack
41
41
  # @return [ String ] A HTML snippet with the attribute name and value
42
42
  #
43
43
  def attribute(record, attributes, options={})
44
- options.reverse_merge!(:label => true, :label_inline => true, :limit => nil, :delimiter => ", ",
45
- :link_path => false, :tag => Supplejack.attribute_tag, :label_tag => Supplejack.label_tag,
46
- :label_class => Supplejack.label_class, :trans_key => nil, :link => false,
47
- :extra_html => nil, :tag_class => nil)
44
+ options.reverse_merge!(label: true, label_inline: true, limit: nil, delimiter: ", ",
45
+ link_path: false, tag: Supplejack.attribute_tag, label_tag: Supplejack.label_tag,
46
+ label_class: Supplejack.label_class, trans_key: nil, link: false,
47
+ extra_html: nil, tag_class: nil)
48
48
 
49
49
  value = []
50
50
  attributes = [attributes] unless attributes.is_a?(Array)
@@ -72,7 +72,7 @@ module Supplejack
72
72
  end
73
73
 
74
74
  if options[:link_path]
75
- value = value.map {|v| link_to(v, send(options[:link_path], {:i => {attribute => v}})) }
75
+ value = value.map {|v| link_to(v, send(options[:link_path], {i: {attribute => v}})) }
76
76
  end
77
77
 
78
78
  if options[:link]
@@ -82,11 +82,11 @@ module Supplejack
82
82
  end
83
83
 
84
84
  value = value.join(options[:delimiter]).html_safe
85
- value = truncate(value, :length => options[:limit]) if options[:limit].to_i > 20
85
+ value = truncate(value, length: options[:limit]) if options[:limit].to_i > 20
86
86
  value
87
87
  else
88
88
  if options[:limit] && options[:limit].to_i > 0
89
- value = truncate(value, :length => options[:limit].to_i)
89
+ value = truncate(value, length: options[:limit].to_i)
90
90
  end
91
91
 
92
92
  if options[:link]
@@ -97,22 +97,66 @@ module Supplejack
97
97
  content = ""
98
98
  if options[:label]
99
99
  if options[:trans_key].present?
100
- translation = I18n.t(options[:trans_key], :default => attribute.to_s.capitalize) + ": "
100
+ translation = I18n.t(options[:trans_key], default: attribute.to_s.capitalize) + ": "
101
101
  else
102
102
  i18n_class_name = record.class.to_s.tableize.downcase.gsub(/\//, "_")
103
- translation = "#{I18n.t("#{i18n_class_name}.#{attribute}", :default => attribute.to_s.capitalize)}: "
103
+ translation = "#{I18n.t("#{i18n_class_name}.#{attribute}", default: attribute.to_s.capitalize)}: "
104
104
  end
105
- content = content_tag(options[:label_tag], translation, :class => options[:label_class]).html_safe
105
+ content = content_tag(options[:label_tag], translation, class: options[:label_class]).html_safe
106
106
  content << "<br/>".html_safe unless options[:label_inline]
107
107
  end
108
108
 
109
109
  content << value.to_s
110
110
  content << options[:extra_html] if options[:extra_html]
111
111
  if value.present? and value != "Not specified"
112
- options[:tag] ? content_tag(options[:tag], content.html_safe, :class => options[:tag_class]) : content.html_safe
112
+ options[:tag] ? content_tag(options[:tag], content.html_safe, class: options[:tag_class]) : content.html_safe
113
113
  end
114
114
  end
115
115
 
116
+ # Displays the next and/or previous links based on the record and current search
117
+ #
118
+ # @params [ Dnz::Record ] The record object which has information about the next/previous record and pages.
119
+ # @params [ Hash ] options Hash of options to customize the output,
120
+ # supported options: :prev_class, :next_class, :prev_label, :next_label
121
+ #
122
+ # @option options [ String ] :prev_class The CSS class to use on the previous button
123
+ # @option options [ String ] :next_class The CSS class to use on the next button
124
+ # @option options [ String ] :wrapper_class The CSS class to use on the wrapping span
125
+ # @option options [ String ] :prev_label Any HTML to be put inside the previous button
126
+ # @option options [ String ] :next_label Any HTML to be put inside the next button
127
+ #
128
+ def next_previous_links(record, html_options={})
129
+ html_options.reverse_merge!({prev_class: "prev", next_class: "next", wrapper_class: 'nav', prev_label: nil, next_label: nil})
130
+
131
+ return "" unless params[:search]
132
+ links = "".html_safe
133
+
134
+ options = search.options
135
+
136
+ previous_label = html_options[:prev_label] ||= t('supplejack_client.previous', default: "Previous")
137
+ next_label = html_options[:next_label] ||= t('supplejack_client.next', default: "Next")
138
+ previous_label = previous_label.html_safe
139
+ next_label = next_label.html_safe
140
+
141
+ options[:path] = params[:search][:path].gsub(/(\W|\d)/, '') if params[:search] && params[:search][:path]
142
+
143
+ if record.previous_record
144
+ options[:page] = record.previous_page if record.previous_page.to_i > 1
145
+ links += link_to(raw(previous_label), record_path(record.previous_record, search: options), class: html_options[:prev_class]).html_safe
146
+ else
147
+ links += content_tag(:span, previous_label, class: html_options[:prev_class])
148
+ end
149
+
150
+ if record.next_record
151
+ options[:page] = record.next_page if record.next_page.to_i > 1
152
+ links += link_to(raw(next_label), record_path(record.next_record, search: options), class: html_options[:next_class]).html_safe
153
+ else
154
+ links += content_tag(:span, next_label, class: html_options[:next_class])
155
+ end
156
+
157
+ content_tag(:span, links, class: html_options[:wrapper_class])
158
+ end
159
+
116
160
  def attribute_link_replacement(value, link_pattern)
117
161
  if link_pattern.is_a?(String)
118
162
  link_pattern = URI.decode(link_pattern)
@@ -123,6 +167,170 @@ module Supplejack
123
167
  end
124
168
  end
125
169
 
170
+ # Generates hidden fields with all the filters of a search object
171
+ # It is used in forms, so that when a user enteres another term in the search
172
+ # box the state of the search is preserved
173
+ #
174
+ # @param [ Dnz::Search ] search A instance of the Dnz::Search class
175
+ # @param [ Hash ] options Hash of options to remove any filter
176
+ #
177
+ # @option options [ Array ] except A array of fields which should not generate a hidden field
178
+ #
179
+ # @return [ String ] A HTML snippet with hidden fields
180
+ #
181
+ def form_fields(search, options={})
182
+ if search
183
+ tags = "".html_safe
184
+
185
+ fields = [:record_type, :sort, :direction]
186
+ fields.delete(:record_type) if search.record?
187
+
188
+ if options[:except].try(:any?)
189
+ fields.delete_if {|field| options[:except].include?(field)}
190
+ end
191
+
192
+ fields.each do |field|
193
+ tags += hidden_field_tag(field.to_s, search.send(field)) unless search.send(field).blank?
194
+ end
195
+
196
+ {i: :i_unlocked, il: :i_locked, h: :h_unlocked, hl: :h_locked}.each_pair do |symbol, instance_name|
197
+ if Supplejack.sticky_facets || [:il, :hl].include?(symbol) || options[:all_filters]
198
+ filters = search.url_format.send(instance_name) rescue {}
199
+ filters.each do |name, value|
200
+ field_name = value.is_a?(Array) ? "#{symbol.to_s}[#{name.to_s}][]" : "#{symbol.to_s}[#{name.to_s}]"
201
+ values = *value
202
+ values.each {|v| tags << hidden_field_tag(field_name, v) }
203
+ end
204
+ end
205
+ end
206
+
207
+ tags
208
+ end
209
+ end
210
+
211
+ # Returns a link with all existing search options except the specified in the params
212
+ #
213
+ # @param [ Symbol ] name The name of the facet. Ex: :category, :subject
214
+ # @param [ String ] value The value withing the facet. Ex: "Wellington", "Books", etc..
215
+ # @param [ String ] path_name The name of the path used to generate the url path.
216
+ # For example if you have a records_path route in your app, then specify "records"
217
+ # and it will call the records_path method
218
+ # @param [ Hash ] options Set of options to customize the output
219
+ # @param [ Hash ] html_options HTML options that are passed directly to the link_to method
220
+ #
221
+ # @return [ String ] A link to with the correct search options.
222
+ #
223
+ def link_to_remove_filter(name, value, path_name, options={}, html_options={}, &block)
224
+ path = generate_path(path_name, search.options(except: [{name => value}, :page]))
225
+ link_text = options[:display_name].present? ? options[:display_name] : I18n.t("facets.values.#{value}", default: value)
226
+ link_to block_given? ? capture(&block) : link_text, path.html_safe, html_options
227
+ end
228
+
229
+ # Returns a link with the existing search options and adds the specified facet and value
230
+ #
231
+ # @param [ Symbol ] name The name of the facet. Ex: :category, :subject
232
+ # @param [ String ] value The value withing the facet. Ex: "Wellington", "Books", etc..
233
+ # @param [ String ] path_name The name of the path used to generate the url path.
234
+ # For example if you have a records_path route in your app, then specify "records"
235
+ # and it will call the records_path method
236
+ # @param [ Hash ] options Set of options to customize the output
237
+ # @param [ Hash ] html_options HTML options that are passed directly to the link_to method
238
+ #
239
+ # @return [ String ] A link to with the correct search options.
240
+ #
241
+ def link_to_add_filter(name, value, path_name, options={}, html_options={}, &block)
242
+ symbol = search.record_type == 0 ? :i : :h
243
+ options[:except] = Util.array(options[:except]) + [:page]
244
+ path = generate_path(path_name, search.options(plus: {symbol => {name => value}}, except: options[:except]))
245
+ link_text = options[:display_name].present? ? options[:display_name] : I18n.t("facets.values.#{value}", default: value)
246
+ link_to block_given? ? capture(&block) : link_text, path.html_safe, html_options
247
+ end
248
+
249
+ def link_to_lock_filter(name, value, path_name, options={}, html_options={}, &block)
250
+ symbol = search.record_type == 0 ? :il : :hl
251
+ path = generate_path(path_name, search.options(except: [{name => value}], plus: {symbol => {name => value}}))
252
+ link_text = options[:display_name].present? ? options[:display_name] : I18n.t("facets.values.#{value}", default: value)
253
+ link_to block_given? ? capture(&block) : link_text, path.html_safe, html_options
254
+ end
255
+
256
+ # Provides a link to record landing page that is augmented with a persisted search object
257
+ # Used in results listings
258
+ #
259
+ # @params [ String ] name A text to display in the link (not required if block is passed)
260
+ # @params [ String ] url The url for the link
261
+ # @params [ Hash ] search_options The options to pass in the search options (usually @search.options)
262
+ # @params [ Hash ] html_options The options to pass to link_to
263
+ # @params [ Block ] &block A block containing ERB, this replaces the name parameter
264
+ #
265
+ def link_to_record(*args, &block)
266
+ if block_given?
267
+ name = capture(&block)
268
+ url = args[0]
269
+ search_options = args[1] || {}
270
+ html_options = args[2] || {}
271
+ else
272
+ name = args[0]
273
+ url = args[1]
274
+ search_options = args[2] || {}
275
+ html_options = args[3] || {}
276
+ end
277
+ url = url + "?" + {search: search_options}.to_query if search_options.try(:any?)
278
+ link_to(name, url, html_options)
279
+ end
280
+
281
+ # Displays the next and/or previous links based on the record and current search
282
+ #
283
+ # @params [ Dnz::Record ] The record object which has information about the next/previous record and pages.
284
+ # @params [ Hash ] options Hash of options to customize the output,
285
+ # supported options: :prev_class, :next_class, :prev_label, :next_label
286
+ #
287
+ # @option options [ String ] :prev_class The CSS class to use on the previous button
288
+ # @option options [ String ] :next_class The CSS class to use on the next button
289
+ # @option options [ String ] :wrapper_class The CSS class to use on the wrapping span
290
+ # @option options [ String ] :prev_label Any HTML to be put inside the previous button
291
+ # @option options [ String ] :next_label Any HTML to be put inside the next button
292
+ #
293
+ def next_previous_links(record, html_options={})
294
+ html_options.reverse_merge!({prev_class: "prev", next_class: "next", wrapper_class: 'nav', prev_label: nil, next_label: nil})
295
+
296
+ return "" unless params[:search]
297
+ links = "".html_safe
298
+
299
+ options = search.options
300
+
301
+ previous_label = html_options[:prev_label] ||= t('dnz_client.previous', default: "Previous")
302
+ next_label = html_options[:next_label] ||= t('dnz_client.next', default: "Next")
303
+ previous_label = previous_label.html_safe
304
+ next_label = next_label.html_safe
305
+
306
+ options[:path] = params[:search][:path].gsub(/(\W|\d)/, '') if params[:search] && params[:search][:path]
307
+
308
+ if record.previous_record
309
+ options[:page] = record.previous_page if record.previous_page.to_i > 1
310
+ links += link_to(raw(previous_label), record_path(record.previous_record, search: options), class: html_options[:prev_class]).html_safe
311
+ else
312
+ links += content_tag(:span, previous_label, class: html_options[:prev_class])
313
+ end
314
+
315
+ if record.next_record
316
+ options[:page] = record.next_page if record.next_page.to_i > 1
317
+ links += link_to(raw(next_label), record_path(record.next_record, search: options), class: html_options[:next_class]).html_safe
318
+ else
319
+ links += content_tag(:span, next_label, class: html_options[:next_class])
320
+ end
321
+
322
+ content_tag(:span, links, class: html_options[:wrapper_class])
323
+ end
324
+
325
+ def generate_path(name, options={})
326
+ segments = name.split(".")
327
+ if segments.size == 1
328
+ send("#{segments[0]}_path", options)
329
+ elsif segments.size == 2
330
+ send(segments[0]).send("#{segments[1]}_path", options)
331
+ end
332
+ end
333
+
126
334
  end
127
335
  end
128
336
  end
@@ -23,7 +23,9 @@ module Supplejack
23
23
  class Item
24
24
  include Supplejack::Request
25
25
 
26
- ATTRIBUTES = [:record_id, :date]
26
+ ATTRIBUTES = [:record_id, :title, :description, :large_thumbnail_url, :thumbnail_url,
27
+ :contributing_partner, :display_content_partner, :display_collection, :landing_url, :category, :date,
28
+ :dnz_type, :dc_identifier, :creator]
27
29
 
28
30
  attr_reader *ATTRIBUTES
29
31
  attr_reader :attributes, :user_set_id
@@ -17,6 +17,16 @@ module Supplejack
17
17
  extend Supplejack::Request
18
18
  extend ActiveModel::Naming
19
19
  include ActiveModel::Conversion
20
+
21
+ # Some of the records in the API return an array of values, but in practice
22
+ # most of them have on only one value. What this does is just convert the array
23
+ # to a string for the methods defined in the configuration.
24
+ Supplejack.single_value_methods.each do |method|
25
+ define_method("#{method}") do
26
+ values = @attributes[method]
27
+ values.is_a?(Array) ? values.first : values
28
+ end
29
+ end
20
30
  end
21
31
 
22
32
  def initialize(attributes={})
@@ -49,18 +59,25 @@ module Supplejack
49
59
  def metadata
50
60
  metadata = []
51
61
 
52
- ['supplejack', 'admin'].each do |schema|
53
- Supplejack.send("#{schema}_fields").each do |field|
62
+ Supplejack.send("special_fields").each do |schema, fields|
63
+ fields[:fields].each do |field|
54
64
  if @attributes.has_key?(field)
55
65
  values = @attributes[field]
56
66
  values ||= [] unless !!values == values #Testing if boolean
57
67
  values = [values] unless values.is_a?(Array)
58
- field = field.to_s.camelcase(:lower) if schema == "dcterms"
68
+
69
+ case fields[:format]
70
+ when "uppercase" then field = field.to_s.upcase
71
+ when "lowercase" then field = field.to_s.downcase
72
+ when "camelcase" then field = field.to_s.camelcase
73
+ end
74
+
75
+ # field = field.to_s.camelcase(:lower) if schema == :dcterms
59
76
  field = field.to_s.sub(/#{schema}_/, '')
60
77
  values.each do |value|
61
- metadata << {:name => field, :schema => schema, :value => value }
78
+ metadata << {:name => field, :schema => schema.to_s, :value => value }
62
79
  end
63
- end
80
+ end
64
81
  end
65
82
  end
66
83
 
@@ -72,16 +89,6 @@ module Supplejack
72
89
  @attributes[:format]
73
90
  end
74
91
 
75
- # Some of the records in the API return an array of values, but in practice
76
- # most of them have on only one value. What this does is just convert the array
77
- # to a string for the methods defined in the configuration.
78
- Supplejack.single_value_methods.each do |method|
79
- define_method("#{method}") do
80
- values = @attributes[method]
81
- values.is_a?(Array) ? values.first : values
82
- end
83
- end
84
-
85
92
  [:next_page, :previous_page, :next_record, :previous_record].each do |pagination_field|
86
93
  define_method(pagination_field) do
87
94
  @attributes[pagination_field]
@@ -92,7 +99,25 @@ module Supplejack
92
99
  true
93
100
  end
94
101
 
102
+ # def build_authorities(name)
103
+ # @attributes[:authorities] ||= []
104
+ # authorities = @attributes[:authorities].find_all {|authority| authority["name"] == name }
105
+ # authorities.map {|attributes| Supplejack::Authority.new(attributes) }
106
+ # end
107
+
95
108
  def method_missing(symbol, *args, &block)
109
+ if symbol.to_s.match(/(.+)_authorities/)
110
+ return build_authorities("#{$1}_authority")
111
+ end
112
+
113
+ if symbol.to_s.match(/(.+)_terms/)
114
+ return build_authorities("#{$1}_term")
115
+ end
116
+
117
+ if [:series_parent, :child_series, :collection_parent, :collection_root, :collection_mid].include?(symbol)
118
+ return build_authorities(symbol.to_s)
119
+ end
120
+
96
121
  raise NoMethodError, "undefined method '#{symbol.to_s}' for Supplejack::Record:Module" unless @attributes.has_key?(symbol)
97
122
  @attributes[symbol]
98
123
  end
@@ -10,13 +10,13 @@ require 'digest/md5'
10
10
 
11
11
  module Supplejack
12
12
  class Search
13
- include Supplejack::Request
13
+ include Supplejack::Request
14
14
 
15
- attr_accessor :results, :text, :page, :per_page, :pagination_limit, :direction, :sort, :filters, :record_type, :record_klass
16
- attr_accessor :url_format, :without, :and, :or, :params, :api_params
15
+ attr_accessor :results, :text, :page, :per_page, :pagination_limit, :direction, :sort, :filters, :record_type, :record_klass
16
+ attr_accessor :url_format, :without, :and, :or, :params, :api_params
17
17
 
18
- def initialize(params={})
19
- @params = params.clone rescue {}
18
+ def initialize(params={})
19
+ @params = params.clone rescue {}
20
20
  @params[:facets] ||= Supplejack.facets.join(',')
21
21
  @params[:facets_per_page] ||= Supplejack.facets_per_page
22
22
  [:action, :controller].each {|p| @params.delete(p) }
@@ -30,10 +30,10 @@ module Supplejack
30
30
  @pagination_limit = @params[:pagination_limit] || Supplejack.pagination_limit
31
31
  @sort = @params[:sort]
32
32
  @direction = @params[:direction]
33
- @url_format = Supplejack.url_format_klass.new(@params, self)
34
- @filters = @url_format.filters
35
- @api_params = @url_format.to_api_hash
36
- @record_klass = @params[:record_klass] || Supplejack.record_klass
33
+ @url_format = Supplejack.url_format_klass.new(@params, self)
34
+ @filters = @url_format.filters
35
+ @api_params = @url_format.to_api_hash
36
+ @record_klass = @params[:record_klass] || Supplejack.record_klass
37
37
 
38
38
  # Do not execute the actual search right away, it should be lazy loaded
39
39
  # when the user needs one of the following values.
@@ -49,7 +49,7 @@ module Supplejack
49
49
  self.class.send(:attr_accessor, attribute)
50
50
  self.send("#{attribute}=", @filters[attribute]) unless @filters[attribute] == 'all'
51
51
  end
52
- end
52
+ end
53
53
 
54
54
  # Returns by default a array of two element arrays with all the active filters
55
55
  # in the search object and their values
@@ -217,9 +217,9 @@ module Supplejack
217
217
  #
218
218
  # @return [Hash{String => Integer}] A hash of type names and counts
219
219
  #
220
- def types(options={})
221
- return @types if @types
222
- @types = facet_values('type', options)
220
+ def categories(options={})
221
+ return @categories if @categories
222
+ @categories = facet_values('category', options)
223
223
  end
224
224
 
225
225
  # Gets the facet values unrestricted by the current filter
@@ -309,6 +309,15 @@ module Supplejack
309
309
  return true
310
310
  end
311
311
 
312
+ # Gets the category facet unrestricted by the current category filter
313
+ #
314
+ # @return [Hash{String => Integer}] A hash of category names and counts
315
+ #
316
+ def categories(options={})
317
+ return @categories if @categories
318
+ @categories = facet_values("category", options)
319
+ end
320
+
312
321
  # Convienence method to find out if the search object has any specific filter
313
322
  # applied to it. It works for both single and multiple value filters.
314
323
  # This methods are actually defined on method_missing.
@@ -341,6 +350,6 @@ module Supplejack
341
350
 
342
351
  Util.deep_merge(existing_filters, extra_filters)
343
352
  end
344
-
353
+
345
354
  end
346
355
  end
@@ -40,6 +40,10 @@ module Supplejack
40
40
  end
41
41
  end
42
42
 
43
+ def sets
44
+ @sets ||= UserSetRelation.new(self)
45
+ end
46
+
43
47
  # Initializes a UserSetRelation class which adds behaviour to build, create
44
48
  # find and order sets related to this particular User instance
45
49
  #
@@ -6,5 +6,5 @@
6
6
  # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
7
 
8
8
  module Supplejack
9
- VERSION = '1.0.2'
9
+ VERSION = '1.0.4'
10
10
  end
@@ -82,10 +82,16 @@ module Supplejack
82
82
  end
83
83
 
84
84
  describe '#metadata' do
85
- it 'returns an array of hashes with supplejack fields their values and schemas' do
86
- Supplejack.stub(:supplejack_fields) { [:location] }
85
+ it 'returns an array of hashes with special fields their values and schemas' do
86
+ Supplejack.stub(:special_fields) { {admin: {fields: [:location]}} }
87
87
  record = SupplejackRecord.new({:location => 'Wellington'})
88
- record.metadata.should include({:name => 'location', :schema => 'supplejack', :value => 'Wellington'})
88
+ record.metadata.should include({:name => 'location', :schema => 'admin', :value => 'Wellington'})
89
+ end
90
+
91
+ it 'returns an array of hashes with special fields their values and schemas for multiple special_fields configured' do
92
+ Supplejack.stub(:special_fields) { {admin: {fields: [:location]}, dnz: {fields: [:description]}} }
93
+ record = SupplejackRecord.new({:location => 'Wellington', :description => "Some description"})
94
+ record.metadata.should include({:name => 'location', :schema => 'admin', :value => 'Wellington'}, {:name => 'description', :schema => 'dnz', :value => 'Some description'})
89
95
  end
90
96
 
91
97
  it 'should not return metadata for inexistent attribtues' do
@@ -95,9 +101,9 @@ module Supplejack
95
101
  end
96
102
 
97
103
  it 'returns multiple elements for a multi value field' do
98
- Supplejack.stub(:supplejack_fields) { [:location] }
104
+ Supplejack.stub(:special_fields) { {admin: {fields: [:location]}} }
99
105
  record = SupplejackRecord.new({:location => ['Wellington', 'Auckland']})
100
- record.metadata.should include({:name => 'location', :schema => 'supplejack', :value => 'Wellington'}, {:name => 'location', :schema => 'supplejack', :value => 'Auckland'})
106
+ record.metadata.should include({:name => 'location', :schema => 'admin', :value => 'Wellington'}, {:name => 'location', :schema => 'admin', :value => 'Auckland'})
101
107
  end
102
108
 
103
109
  it 'returns a empty array for a empty field' do
@@ -107,27 +113,27 @@ module Supplejack
107
113
  end
108
114
 
109
115
  it 'works for boolean fields too' do
110
- Supplejack.stub(:supplejack_fields) { [:is_human] }
116
+ Supplejack.stub(:special_fields) { {admin: {fields: [:is_human]}} }
111
117
  record = SupplejackRecord.new({:is_human => true})
112
- record.metadata.should include({:name => 'is_human', :schema => 'supplejack', :value => true})
118
+ record.metadata.should include({:name => 'is_human', :schema => 'admin', :value => true})
113
119
  end
114
120
 
115
- it 'works for boolean fields when they are true' do
116
- Supplejack.stub(:admin_fields) { [:is_animal] }
117
- record = SupplejackRecord.new({:is_animal => true})
118
- record.metadata.should include({:name => 'is_animal', :schema => 'admin', :value => true})
119
- end
121
+ # it 'works for boolean fields when they are true' do
122
+ # Supplejack.stub(:admin_fields) { [:is_animal] }
123
+ # record = SupplejackRecord.new({:is_animal => true})
124
+ # record.metadata.should include({:name => 'is_animal', :schema => 'admin', :value => true})
125
+ # end
120
126
 
121
127
  it 'works for boolean fields when they are false' do
122
- Supplejack.stub(:admin_fields) { [:is_animal] }
123
- record = SupplejackRecord.new({:is_animal => false})
124
- record.metadata.should include({:name => 'is_animal', :schema => 'admin', :value => false})
128
+ Supplejack.stub(:special_fields) { {admin: {fields: [:is_human]}} }
129
+ record = SupplejackRecord.new({:is_human => false})
130
+ record.metadata.should include({:name => 'is_human', :schema => 'admin', :value => false})
125
131
  end
126
132
 
127
133
  it 'returns names with the schema removed' do
128
- Supplejack.stub(:supplejack_fields) { [:identifier] }
129
- record = SupplejackRecord.new({:identifier => 'sj:IE1174615'})
130
- record.metadata.should include({:name => 'identifier', :schema => 'supplejack', :value => 'sj:IE1174615'})
134
+ Supplejack.stub(:special_fields) { {admin: {fields: [:admin_identifier]}} }
135
+ record = SupplejackRecord.new({:admin_identifier => 'sj:IE1174615'})
136
+ record.metadata.should include({:name => 'identifier', :schema => 'admin', :value => 'sj:IE1174615'})
131
137
  end
132
138
  end
133
139
 
@@ -18,9 +18,9 @@ class TestItem
18
18
  end
19
19
 
20
20
  module Supplejack
21
- describe Search do
21
+ describe Search do
22
22
 
23
- describe '#initalize' do
23
+ describe '#initalize' do
24
24
  it 'doesn\'t break when initalized with nil' do
25
25
  Search.new(nil)
26
26
  end
@@ -533,7 +533,7 @@ module Supplejack
533
533
  end
534
534
  end
535
535
 
536
- describe '#types' do
536
+ describe '#categories' do
537
537
  before(:each) do
538
538
  @search = Search.new({:i => {:category => 'Books', :year => 2001}, :text => 'Dogs'})
539
539
  @search.stub(:get) { {'search' => {'facets' => {'category' => {'Books' => 123}}, 'result_count' => 123}} }
@@ -541,30 +541,30 @@ module Supplejack
541
541
 
542
542
  it 'should call the fetch_values method' do
543
543
  @search.should_receive(:facet_values).with('category', {})
544
- @search.types
544
+ @search.categories
545
545
  end
546
546
 
547
547
  it 'removes category filter from the search request' do
548
548
  @search.should_receive(:get).with('/records', hash_including(:and => {:year => 2001})).and_return({'search' => {'facets' => {'category' => {'Books' => 123}}}})
549
- @search.types
549
+ @search.categories
550
550
  end
551
551
 
552
552
  it 'returns the category facet hash ' do
553
- @search.types.should include('Books' => 123)
553
+ @search.categories.should include('Books' => 123)
554
554
  end
555
555
 
556
556
  it 'asks the API for 0 results' do
557
557
  @search.should_receive(:get).with('/records', hash_including({:per_page => 0}))
558
- @search.types
558
+ @search.categories
559
559
  end
560
560
 
561
561
  it 'should return add the All count to the hash' do
562
- @search.types['All'].should eq 123
562
+ @search.categories['All'].should eq 123
563
563
  end
564
564
 
565
565
  it 'orders the category values by :count' do
566
566
  @search.should_receive(:facet_values).with('category', {:sort => :count})
567
- @search.types({:sort => :count})
567
+ @search.categories({:sort => :count})
568
568
  end
569
569
  end
570
570
 
@@ -723,5 +723,5 @@ module Supplejack
723
723
  end
724
724
  end
725
725
 
726
- end
726
+ end
727
727
  end
@@ -10,6 +10,7 @@ require 'spec_helper'
10
10
  module Supplejack
11
11
  describe User do
12
12
  let(:user) { Supplejack::User.new({'id' => 'abc', 'authentication_token' => '12345'}) }
13
+ let(:relation) { Supplejack::UserSetRelation.new(user) }
13
14
 
14
15
  before(:each) do
15
16
  Supplejack::User.stub(:get) { {'user' => {'id' => 'abc', 'authentication_token' => '12345'}} }
@@ -37,6 +38,14 @@ module Supplejack
37
38
  end
38
39
  end
39
40
 
41
+ describe "#sets" do
42
+ it "initializes a Dnz::UserSetRelation object" do
43
+ @relation = relation
44
+ Supplejack::UserSetRelation.should_receive(:new).with(user) { @relation }
45
+ user.sets.should be_a Supplejack::UserSetRelation
46
+ end
47
+ end
48
+
40
49
  describe '#save' do
41
50
  it 'should execute a put request with the user attribtues' do
42
51
  user.stub(:api_attributes) { {username: 'John', email: 'john@boost.co.nz'} }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supplejack_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Supplejack