geckorate 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -4
- data/lib/geckorate/version.rb +1 -1
- data/lib/geckorate.rb +8 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae64051898a555265e590a35db8dd7772269f7d03b1305dda594bcd3fbe1c31e
|
4
|
+
data.tar.gz: 211bdf9f83780632ff92c01f3a46e96c64b0606c3abcc43ce0bd41105d3e3960
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d132d63d8176d5940a9a022634b0f663a0dbf730b4f0d89e0eb5b696870fae5364633f759300d91eeda96c8d4ccb644d9d438f93c156037050e04fc03e90ba47
|
7
|
+
data.tar.gz: 02f6595dcf68fa25ff1e8f3478373802a5160fade930d755d08ecaf99c2866e422f912957bc4a32720990d612fef8ed91ae9880566b881195bc2e4d059bb3bc1
|
data/README.md
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
A dead simple decorator for Ruby
|
4
4
|
|
5
|
+
# Why
|
6
|
+
|
7
|
+
This gem groups the functionality I used to implement in every Rails project I
|
8
|
+
had so I thought of packaging it into a gem and get over with it. Also, see
|
9
|
+
[Build your own decorator pattern](https://pdabrowski.com/blog/build-your-own-decorator-pattern).
|
10
|
+
|
5
11
|
## Installation
|
6
12
|
|
7
13
|
Add this line to your application's Gemfile:
|
@@ -40,7 +46,7 @@ Now, define your decorator as such:
|
|
40
46
|
# app/decorators/post_decorator.rb
|
41
47
|
|
42
48
|
class PostDecorator < Geckorate::Decorator
|
43
|
-
def decorate
|
49
|
+
def decorate(options = {})
|
44
50
|
{
|
45
51
|
title: title,
|
46
52
|
username: user.name
|
@@ -65,15 +71,15 @@ end
|
|
65
71
|
|
66
72
|
### For collections
|
67
73
|
|
68
|
-
There's a method `decorate_collection(collection)` for decorating collections.
|
74
|
+
There's a method `decorate_collection(collection, options = {})` for decorating collections.
|
69
75
|
Example: `PostDecorator.decorate_collection(Post.all)` returns an array of
|
70
76
|
decorated Post objects.
|
71
77
|
|
72
78
|
### With paginated collections
|
73
79
|
|
74
80
|
Currently, there are two methods for pagination; one for Kaminari
|
75
|
-
`decorate_kaminari_collection(collection)` and another for will_paginate
|
76
|
-
`decorate_will_paginate_collection(collection)`. The returned hash from both
|
81
|
+
`decorate_kaminari_collection(collection, options = {})` and another for will_paginate
|
82
|
+
`decorate_will_paginate_collection(collection, options = {})`. The returned hash from both
|
77
83
|
methods looks like this:
|
78
84
|
|
79
85
|
```ruby
|
data/lib/geckorate/version.rb
CHANGED
data/lib/geckorate.rb
CHANGED
@@ -2,36 +2,36 @@ require "geckorate/version"
|
|
2
2
|
|
3
3
|
module Geckorate
|
4
4
|
class Decorator < SimpleDelegator
|
5
|
-
def decorate(options
|
5
|
+
def decorate(options = {}); end
|
6
6
|
|
7
7
|
class << self
|
8
|
-
def decorate_collection(collection,
|
8
|
+
def decorate_collection(collection, options = {})
|
9
9
|
return [] if collection.empty?
|
10
10
|
|
11
|
-
klass = class_name
|
11
|
+
klass = options.fetch(:class_name, collection.first.class)
|
12
12
|
full_klass_name = klass.to_s.concat('Decorator')
|
13
13
|
decorator_klass = Class.const_get(full_klass_name)
|
14
14
|
|
15
15
|
collection.map do |item|
|
16
|
-
decorator_klass.new(item).decorate(options
|
16
|
+
decorator_klass.new(item).decorate(options)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def decorate_kaminari_collection(collection,
|
20
|
+
def decorate_kaminari_collection(collection, options = {})
|
21
21
|
{
|
22
22
|
page: collection.current_page,
|
23
23
|
per_page: collection.current_per_page,
|
24
24
|
total: collection.total_count,
|
25
|
-
records: decorate_collection(collection,
|
25
|
+
records: decorate_collection(collection, options)
|
26
26
|
}
|
27
27
|
end
|
28
28
|
|
29
|
-
def decorate_will_paginate_collection(
|
29
|
+
def decorate_will_paginate_collection(collectionf, options = {})
|
30
30
|
{
|
31
31
|
page: collection.current_page,
|
32
32
|
per_page: collection.per_page,
|
33
33
|
total: collection.total_entries,
|
34
|
-
records: decorate_collection(collection,
|
34
|
+
records: decorate_collection(collection, options)
|
35
35
|
}
|
36
36
|
end
|
37
37
|
end
|