hal_api-rails 0.6.0 → 0.7.0

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: 4f8ad6ad0044a8438881c35775259d46cce7448d
4
- data.tar.gz: c4dd5e6a42a4cca9d744e7050a91dc6d06e335f9
3
+ metadata.gz: e8ca09414db3082766a5b86ee83310563af05628
4
+ data.tar.gz: 002aceca75878356759760d73f5dd68cd6dc03d4
5
5
  SHA512:
6
- metadata.gz: ae0cf7fc44f171df5972c660d732cbd837f89216e001ef76f8b9aa97a0cc25b8581e975a90efdfdee7401cc7e9ae3ad947442239119828a374806ac023b3ce3a
7
- data.tar.gz: 9d8eb8563e870a584b7eb69696a8d205cbb03b5b6ea68b33c64913ced078a158cfc21a37fb62a0c349b2a66c4cb026a8e3448080139f27ec88bc939ebd6e0d12
6
+ metadata.gz: 25f710db7f628fbbd0b708c06e04f4b1f2eb8335ecbb8a48311e68ac4259ad5cfe80d5bbbd02df41d55ea846c8b0d4423055cf1fcbe64f09e37217d1d7c92d33
7
+ data.tar.gz: df4c7da616d62d01b5dddeb031b2e2c7bd94b61e6399452ab17fbe7a43ffe12c1909e5b138821de4e74dad83ac98295355a1550729a5a4220508f24faaa9f849
@@ -53,6 +53,24 @@ module HalApi::Controller::Filtering
53
53
  @filters ||= parse_filters_param
54
54
  end
55
55
 
56
+ def filter_facets
57
+ end
58
+
59
+ def index_collection
60
+ collection = defined?(super) ? super : HalApi::PagedCollection.new([])
61
+
62
+ # add facets if defined, removing filters/facets with counts of 0
63
+ non_zero_facets = (filter_facets || {}).with_indifferent_access.tap do |hash|
64
+ hash.each do |filter_key, facets|
65
+ hash[filter_key] = facets.try(:select) { |f| f.try(:[], :count) > 0 }
66
+ hash.delete(filter_key) if hash[filter_key].blank?
67
+ end
68
+ end
69
+ collection.facets = non_zero_facets unless non_zero_facets.blank?
70
+
71
+ collection
72
+ end
73
+
56
74
  private
57
75
 
58
76
  def parse_filters_param
@@ -75,6 +75,10 @@ module HalApi::Controller::Resources
75
75
  self.class.resource_class.where(nil)
76
76
  end
77
77
 
78
+ def resources_query
79
+ filtered(scoped(resources_base))
80
+ end
81
+
78
82
  def find_base
79
83
  filtered(scoped(included(resources_base)))
80
84
  end
@@ -6,7 +6,7 @@ class HalApi::PagedCollection
6
6
  extend ActiveModel::Naming
7
7
  extend Forwardable
8
8
 
9
- attr_accessor :items, :request, :options
9
+ attr_accessor :items, :request, :options, :facets
10
10
 
11
11
  def_delegators :items, :total_count, :prev_page, :next_page, :total_pages, :first_page?, :last_page?
12
12
  alias_method :total, :total_count
@@ -1,5 +1,5 @@
1
1
  module HalApi
2
2
  module Rails
3
- VERSION = "0.6.0"
3
+ VERSION = "0.7.0"
4
4
  end
5
5
  end
@@ -21,5 +21,6 @@ class HalApi::Representer < Roar::Decorator
21
21
  include HalApi::Representer::Caches
22
22
  include HalApi::Representer::LinkSerialize
23
23
  self_link
24
+ vary_link
24
25
  profile_link
25
26
  end
@@ -7,6 +7,7 @@ module HalApi::Representer::CollectionPaging
7
7
  class_eval do
8
8
  property :count
9
9
  property :total
10
+ property :facets
10
11
 
11
12
  embeds :items, decorator: lambda{|*| item_decorator }, class: lambda{|*| item_class }, zoom: :always
12
13
 
@@ -36,6 +37,14 @@ module HalApi::Representer::CollectionPaging
36
37
  href_url_helper(represented.params)
37
38
  end
38
39
 
40
+ def vary_url(represented)
41
+ href_url_helper(represented.params.except(*vary_params))
42
+ end
43
+
44
+ def vary_params
45
+ %w(page per zoom filters sorts)
46
+ end
47
+
39
48
  def profile_url(represented)
40
49
  model_uri(:collection, represented.item_class)
41
50
  end
@@ -46,6 +55,7 @@ module HalApi::Representer::CollectionPaging
46
55
  # if it is a lambda, execute in the context against the represented.parent (if there is one) or represented
47
56
  def href_url_helper(options={})
48
57
  if represented_url.nil?
58
+ options = options.except(:format)
49
59
  result = url_for(options.merge(only_path: true)) rescue nil
50
60
  if represented.parent
51
61
  result ||= polymorphic_path([:api, represented.parent, represented.item_class], options) rescue nil
@@ -21,6 +21,15 @@ module HalApi::Representer::UriMethods
21
21
  end
22
22
  end
23
23
 
24
+ def vary_link
25
+ link(:vary) do
26
+ {
27
+ href: vary_url(represented) + vary_query_params,
28
+ templated: true,
29
+ } if vary_url(represented).present? && vary_params.present?
30
+ end
31
+ end
32
+
24
33
  def profile_link
25
34
  link(:profile) { profile_url(represented) }
26
35
  end
@@ -46,6 +55,18 @@ module HalApi::Representer::UriMethods
46
55
  polymorphic_path([:api, rep])
47
56
  end
48
57
 
58
+ def vary_url(represented)
59
+ self_url(represented)
60
+ end
61
+
62
+ def vary_params
63
+ []
64
+ end
65
+
66
+ def vary_query_params
67
+ "{?#{vary_params.join(',')}}"
68
+ end
69
+
49
70
  def becomes_represented_class(rep)
50
71
  return rep unless rep.respond_to?(:becomes)
51
72
  klass = rep.try(:item_class) || rep.class.try(:base_class)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hal_api-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Rhoden
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-08-16 00:00:00.000000000 Z
12
+ date: 2019-09-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel