geckorate 0.2.1 → 0.3.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
  SHA256:
3
- metadata.gz: 4368265d1232c7456946e705182d89e4f515b0ba182cd2028c02287096ba21a7
4
- data.tar.gz: 2648e7f27065c9ce0f3980635ae9a63cb44f8c4e0611a83706a71abb0f26b6b8
3
+ metadata.gz: ae64051898a555265e590a35db8dd7772269f7d03b1305dda594bcd3fbe1c31e
4
+ data.tar.gz: 211bdf9f83780632ff92c01f3a46e96c64b0606c3abcc43ce0bd41105d3e3960
5
5
  SHA512:
6
- metadata.gz: e1190360f285279d08756159094328a1b46bbebe8ba0d9a03f33b33a088b3ce739c3172c19461693a106ccb82fe1b9419636105c0853f265e6f3aaac50853d5a
7
- data.tar.gz: 307a7a3c98e7aa4e7e4e375f6901274bb2d869fe560fdf760a677f17f2790efe1ea5e7dec4c9d7d71fa132aec4deb9d9049e197268bbcf92819ad581fc2e6bc7
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
@@ -1,3 +1,3 @@
1
1
  module Geckorate
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
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: {}); end
5
+ def decorate(options = {}); end
6
6
 
7
7
  class << self
8
- def decorate_collection(collection, class_name: nil, options: {})
8
+ def decorate_collection(collection, options = {})
9
9
  return [] if collection.empty?
10
10
 
11
- klass = class_name || collection.first.class
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: options)
16
+ decorator_klass.new(item).decorate(options)
17
17
  end
18
18
  end
19
19
 
20
- def decorate_kaminari_collection(collection, class_name: nil, options: {})
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, class_name: class_name, options: options)
25
+ records: decorate_collection(collection, options)
26
26
  }
27
27
  end
28
28
 
29
- def decorate_will_paginate_collection(collection, class_name: nil, options: {})
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, class_name: class_name, options: options)
34
+ records: decorate_collection(collection, options)
35
35
  }
36
36
  end
37
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geckorate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmed Saleh