decorates_before_rendering 0.0.2 → 0.0.3

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.
@@ -25,11 +25,22 @@ require 'active_support/concern'
25
25
  #
26
26
  # @thing_1 will be a ThingListDecorator (or contain them), and @thing_2 will be a Thing2Decorator.
27
27
  #
28
+ # For Draper 1.0 and above, collection elements are no longer decorated with
29
+ # Decorator.decorate(collection), but with Decorator.decorate_collection(collection).
30
+ # Specify that you want to decorate a collection, and with what decorator, with this syntax:
31
+ #
32
+ # class StuffController < ApplicationController
33
+ # include DecoratesBeforeRendering
34
+ #
35
+ # decorates_collection :things_1, :with => ThingListDecorator
36
+ # end
37
+ #
28
38
  module DecoratesBeforeRendering
29
39
  extend ActiveSupport::Concern
30
40
 
31
41
  included do
32
42
  class_attribute :__decorates__, :instance_writer => false
43
+ class_attribute :__decorates_collection__, :instance_writer => false
33
44
 
34
45
  class_eval do
35
46
  def self.decorates(*args)
@@ -38,6 +49,15 @@ module DecoratesBeforeRendering
38
49
  self.__decorates__ ||= []
39
50
  self.__decorates__ << [ args.map { |i| "@#{i}" }, options ]
40
51
  end
52
+
53
+ def self.decorates_collection(*args)
54
+ options = args.extract_options!
55
+
56
+ raise ArgumentError, ":with is required for now" if !options[:with]
57
+
58
+ self.__decorates_collection__ ||= []
59
+ self.__decorates_collection__ << [ args.map { |i| "@#{i}" }, options ]
60
+ end
41
61
  end
42
62
  end
43
63
 
@@ -49,15 +69,31 @@ module DecoratesBeforeRendering
49
69
  private
50
70
 
51
71
  def __decorate_ivars__
52
- return if __decorates__.nil? || __decorates__.empty?
72
+ return if (__decorates__.nil? || __decorates__.empty?) and
73
+ (__decorates_collection__.nil? || __decorates_collection__.empty?)
74
+
75
+ if !__decorates__.nil?
76
+ __decorate_ivar_names__(__decorates__) do |ivar_name, ivar, options|
77
+ decorator = options.key?(:with) ? options.fetch(:with) : __decorator_for__(ivar)
78
+ decorated = decorator.decorate(ivar)
79
+ instance_variable_set(ivar_name, decorated)
80
+ end
81
+ end
82
+
83
+ if !__decorates_collection__.nil?
84
+ __decorate_ivar_names__(__decorates_collection__) do |ivar_name, ivar, options|
85
+ decorated = options.fetch(:with).decorate_collection(ivar)
86
+ instance_variable_set(ivar_name, decorated)
87
+ end
88
+ end
89
+ end
53
90
 
54
- __decorates__.each do |ivar_names, options|
91
+ def __decorate_ivar_names__(ivars)
92
+ ivars.each do |ivar_names, options|
55
93
  ivar_names.each do |ivar_name|
56
94
  ivar = instance_variable_get(ivar_name)
57
95
  if ivar
58
- decorator = options.key?(:with) ? options.fetch(:with) : __decorator_for__(ivar)
59
- decorated = decorator.decorate(ivar)
60
- instance_variable_set(ivar_name, decorated)
96
+ yield ivar_name, ivar, options
61
97
  end
62
98
  end
63
99
  end
@@ -1,5 +1,5 @@
1
1
  module DecoratesBeforeRendering
2
2
  unless defined? DecoratesBeforeRendering::VERSION
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -4,44 +4,44 @@ class MyCompletelyFakeModelDecorator; end
4
4
  class MyOtherCompletelyFakeModelDecorator; end
5
5
 
6
6
  describe DecoratesBeforeRendering do
7
- # NOTE: these are married together, so they're tested together.
8
- describe '::decorates + #render' do
9
- let(:sentinel) { double(:sentinel) }
10
- let(:ivar) { double('@ivar') }
11
- let(:ivars) { double('@ivars') }
12
-
13
- # NOTE: This superclass is here so we know that the correct render gets
14
- # called. It can't be defined in the subclass, or else that one
15
- # will be the one that's used, as modules sit above their includers
16
- # in the class hierarchy.
17
- let(:superclass) do
18
- Class.new do
19
- def initialize(sentinel)
20
- @sentinel = sentinel
21
- end
22
-
23
- def render(*args)
24
- @sentinel.render(*args)
25
- end
7
+ let(:sentinel) { double(:sentinel) }
8
+ let(:ivar) { double('@ivar') }
9
+ let(:ivars) { double('@ivars') }
10
+
11
+ # NOTE: This superclass is here so we know that the correct render gets
12
+ # called. It can't be defined in the subclass, or else that one
13
+ # will be the one that's used, as modules sit above their includers
14
+ # in the class hierarchy.
15
+ let(:superclass) do
16
+ Class.new do
17
+ def initialize(sentinel)
18
+ @sentinel = sentinel
19
+ end
20
+
21
+ def render(*args)
22
+ @sentinel.render(*args)
26
23
  end
27
24
  end
28
- let(:klass) do
29
- Class.new(superclass) do
30
- include DecoratesBeforeRendering
25
+ end
26
+ let(:klass) do
27
+ Class.new(superclass) do
28
+ include DecoratesBeforeRendering
31
29
 
32
- attr_reader :ivar, :ivars
30
+ attr_reader :ivar, :ivars
33
31
 
34
- def initialize(sentinel, ivar, ivars = nil)
35
- super(sentinel)
32
+ def initialize(sentinel, ivar, ivars = nil)
33
+ super(sentinel)
36
34
 
37
- @ivar = ivar
38
- @ivars = ivars
39
- end
35
+ @ivar = ivar
36
+ @ivars = ivars
40
37
  end
41
38
  end
42
- let(:instance) { klass.new(sentinel, ivar, ivars) }
43
- let(:args) { double('*args') }
39
+ end
40
+ let(:instance) { klass.new(sentinel, ivar, ivars) }
41
+ let(:args) { double('*args') }
44
42
 
43
+ # NOTE: these are married together, so they're tested together.
44
+ describe '::decorates + #render' do
45
45
  context "no ivars" do
46
46
  it 'should render' do
47
47
  sentinel.should_receive(:render).with(args)
@@ -109,5 +109,22 @@ describe DecoratesBeforeRendering do
109
109
  end
110
110
  end
111
111
  end
112
+
113
+ # for draper >= 1.0
114
+ describe "#decorates_collection + #render" do
115
+ it "requires decorator class (for now)" do
116
+ expect {
117
+ klass.decorates_collection(:ivars)
118
+ }.to raise_error(ArgumentError)
119
+ end
120
+
121
+ it "should decorate collection and render" do
122
+ klass.decorates_collection(:ivars, :with => MyCompletelyFakeModelDecorator)
123
+ subclass_instance = Class.new(klass).new(sentinel, ivar, ivars)
124
+ sentinel.should_receive(:render).with(args)
125
+ MyCompletelyFakeModelDecorator.should_receive(:decorate_collection).with(ivars)
126
+ subclass_instance.render(args)
127
+ end
128
+ end
112
129
  end
113
130
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decorates_before_rendering
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-18 00:00:00.000000000 Z
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70158643611580 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 2.10.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70158643611580
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.10.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rbx-require-relative
27
- requirement: &70158643610220 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: 0.0.9
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70158643610220
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.9
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: activesupport
38
- requirement: &70158643609720 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,7 +53,12 @@ dependencies:
43
53
  version: 3.2.6
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *70158643609720
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.6
47
62
  description: Small add-on for Draper that decorates models before rendering.
48
63
  email:
49
64
  - rob@mediapiston.com
@@ -81,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
96
  version: '0'
82
97
  requirements: []
83
98
  rubyforge_project:
84
- rubygems_version: 1.8.17
99
+ rubygems_version: 1.8.25
85
100
  signing_key:
86
101
  specification_version: 3
87
102
  summary: Small add-on for Draper that decorates models before rendering.