hal_api-rails 0.3.0 → 0.3.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b318397223e77cc82d2ab9164f9f9ec2dd9b127
|
4
|
+
data.tar.gz: 1a89f74d2c6685d025ddad7932957c591d7fda22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf12d6755045f0ba05b7620474d9d32e82aa0d85200a6ec42bede5e85bfeac03369b0f18c908ad1862f6dc29d061fba8821f87d7cc8ec233499f507ffe2ec09f
|
7
|
+
data.tar.gz: 6908296c63f9eda39063e3cc014718e25cdb16bad6bf5e72060714ebcbd621d13323f06513f6a5f48861dc3e58f6d431b8facc438ef770dbe35c6d4eb5d7f462
|
@@ -1,63 +1,5 @@
|
|
1
|
-
require 'hal_api/representer'
|
1
|
+
require 'hal_api/representer/collection_paging'
|
2
2
|
|
3
3
|
class HalApi::PagedCollectionRepresenter < HalApi::Representer
|
4
|
-
|
5
|
-
property :total
|
6
|
-
|
7
|
-
embeds :items, decorator: lambda{|*| item_decorator }, class: lambda{|*| item_class }, zoom: :always
|
8
|
-
|
9
|
-
link :prev do
|
10
|
-
href_url_helper(params.merge(page: represented.prev_page)) unless represented.first_page?
|
11
|
-
end
|
12
|
-
|
13
|
-
link :next do
|
14
|
-
href_url_helper(params.merge(page: represented.next_page)) unless represented.last_page?
|
15
|
-
end
|
16
|
-
|
17
|
-
link :first do
|
18
|
-
href_url_helper(params.merge(page: nil)) if represented.total_pages > 1
|
19
|
-
end
|
20
|
-
|
21
|
-
link :last do
|
22
|
-
href_url_helper(params.merge(page: represented.total_pages)) if represented.total_pages > 1
|
23
|
-
end
|
24
|
-
|
25
|
-
def params
|
26
|
-
represented.params
|
27
|
-
end
|
28
|
-
|
29
|
-
def self_url(represented)
|
30
|
-
href_url_helper(represented.params)
|
31
|
-
end
|
32
|
-
|
33
|
-
def profile_url(represented)
|
34
|
-
model_uri(:collection, represented.item_class)
|
35
|
-
end
|
36
|
-
|
37
|
-
# refactor to use single property, :url, that can be a method name, a string, or a lambda
|
38
|
-
# if it is a method name, execute against self - the representer - which has local url helpers methods
|
39
|
-
# if it is a sym/string, but self does not respond to it, then just use that string
|
40
|
-
# if it is a lambda, execute in the context against the represented.parent (if there is one) or represented
|
41
|
-
def href_url_helper(options={})
|
42
|
-
if represented_url.nil?
|
43
|
-
result = url_for(options.merge(only_path: true)) rescue nil
|
44
|
-
if represented.parent
|
45
|
-
result ||= polymorphic_path([:api, represented.parent, represented.item_class], options) rescue nil
|
46
|
-
end
|
47
|
-
result ||= polymorphic_path([:api, represented.item_class], options) rescue nil
|
48
|
-
return result
|
49
|
-
end
|
50
|
-
|
51
|
-
if represented_url.respond_to?(:call)
|
52
|
-
self.instance_exec(options, &represented_url)
|
53
|
-
elsif self.respond_to?(represented_url)
|
54
|
-
self.send(represented_url, options)
|
55
|
-
else
|
56
|
-
represented_url.to_s
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def represented_url
|
61
|
-
@represented_url ||= represented.try(:url)
|
62
|
-
end
|
4
|
+
include HalApi::Representer::CollectionPaging
|
63
5
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'hal_api/representer'
|
2
|
+
|
3
|
+
module HalApi::Representer::CollectionPaging
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
class_eval do
|
8
|
+
property :count
|
9
|
+
property :total
|
10
|
+
|
11
|
+
embeds :items, decorator: lambda{|*| item_decorator }, class: lambda{|*| item_class }, zoom: :always
|
12
|
+
|
13
|
+
link :prev do
|
14
|
+
href_url_helper(params.merge(page: represented.prev_page)) unless represented.first_page?
|
15
|
+
end
|
16
|
+
|
17
|
+
link :next do
|
18
|
+
href_url_helper(params.merge(page: represented.next_page)) unless represented.last_page?
|
19
|
+
end
|
20
|
+
|
21
|
+
link :first do
|
22
|
+
href_url_helper(params.merge(page: nil)) if represented.total_pages > 1
|
23
|
+
end
|
24
|
+
|
25
|
+
link :last do
|
26
|
+
href_url_helper(params.merge(page: represented.total_pages)) if represented.total_pages > 1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def params
|
32
|
+
represented.params
|
33
|
+
end
|
34
|
+
|
35
|
+
def self_url(represented)
|
36
|
+
href_url_helper(represented.params)
|
37
|
+
end
|
38
|
+
|
39
|
+
def profile_url(represented)
|
40
|
+
model_uri(:collection, represented.item_class)
|
41
|
+
end
|
42
|
+
|
43
|
+
# refactor to use single property, :url, that can be a method name, a string, or a lambda
|
44
|
+
# if it is a method name, execute against self - the representer - which has local url helpers methods
|
45
|
+
# if it is a sym/string, but self does not respond to it, then just use that string
|
46
|
+
# if it is a lambda, execute in the context against the represented.parent (if there is one) or represented
|
47
|
+
def href_url_helper(options={})
|
48
|
+
if represented_url.nil?
|
49
|
+
result = url_for(options.merge(only_path: true)) rescue nil
|
50
|
+
if represented.parent
|
51
|
+
result ||= polymorphic_path([:api, represented.parent, represented.item_class], options) rescue nil
|
52
|
+
end
|
53
|
+
result ||= polymorphic_path([:api, represented.item_class], options) rescue nil
|
54
|
+
return result
|
55
|
+
end
|
56
|
+
|
57
|
+
if represented_url.respond_to?(:call)
|
58
|
+
self.instance_exec(options, &represented_url)
|
59
|
+
elsif self.respond_to?(represented_url)
|
60
|
+
self.send(represented_url, options)
|
61
|
+
else
|
62
|
+
represented_url.to_s
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def represented_url
|
67
|
+
@represented_url ||= represented.try(:url)
|
68
|
+
end
|
69
|
+
end
|
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.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Rhoden
|
@@ -226,6 +226,7 @@ files:
|
|
226
226
|
- lib/hal_api/representer.rb
|
227
227
|
- lib/hal_api/representer/caches.rb
|
228
228
|
- lib/hal_api/representer/caches/serialized_json.rb
|
229
|
+
- lib/hal_api/representer/collection_paging.rb
|
229
230
|
- lib/hal_api/representer/curies.rb
|
230
231
|
- lib/hal_api/representer/embeds.rb
|
231
232
|
- lib/hal_api/representer/format_keys.rb
|