decorates_before_rendering 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ # DecoratesBeforeRendering Changelog
2
+
3
+ ## 0.0.2
4
+
5
+ * A specific decorator class can be provided using the :with option to decorates. Thanks to
6
+ @johnbintz!
7
+
8
+ ## 0.0.1
9
+
10
+ * Initial release.
@@ -2,6 +2,7 @@
2
2
  require "decorates_before_rendering/version"
3
3
  require 'active_support/core_ext/string/inflections'
4
4
  require 'active_support/core_ext/class/attribute'
5
+ require 'active_support/concern'
5
6
 
6
7
  # Decorates the specified fields. For instance, if you have
7
8
  #
@@ -13,10 +14,30 @@ require 'active_support/core_ext/class/attribute'
13
14
  #
14
15
  # @thing_1 and @thing_2 will be decorated right before a rendering occurs.
15
16
  #
17
+ # You can also specify the decorator you wish to use for a particular instance variable:
18
+ #
19
+ # class StuffController < ApplicationController
20
+ # include DecoratesBeforeRendering
21
+ #
22
+ # decorates :thing_1, :with => ThingListDecorator
23
+ # decorates :thing_2
24
+ # end
25
+ #
26
+ # @thing_1 will be a ThingListDecorator (or contain them), and @thing_2 will be a Thing2Decorator.
27
+ #
16
28
  module DecoratesBeforeRendering
17
- module ClassMethods
18
- def decorates(*unsigiled_ivar_names)
19
- self.__ivars_to_decorate__ = unsigiled_ivar_names.map { |i| "@#{i}" }
29
+ extend ActiveSupport::Concern
30
+
31
+ included do
32
+ class_attribute :__decorates__, :instance_writer => false
33
+
34
+ class_eval do
35
+ def self.decorates(*args)
36
+ options = args.extract_options!
37
+
38
+ self.__decorates__ ||= []
39
+ self.__decorates__ << [ args.map { |i| "@#{i}" }, options ]
40
+ end
20
41
  end
21
42
  end
22
43
 
@@ -28,18 +49,22 @@ module DecoratesBeforeRendering
28
49
  private
29
50
 
30
51
  def __decorate_ivars__
31
- ivars_to_decorate = self.class.__ivars_to_decorate__
52
+ return if __decorates__.nil? || __decorates__.empty?
32
53
 
33
- return if ivars_to_decorate.nil?
34
-
35
- ivars_to_decorate.each do |ivar_name|
36
- ivar = instance_variable_get(ivar_name)
37
- instance_variable_set(ivar_name, __decorator_for__(ivar)) unless ivar.nil?
54
+ __decorates__.each do |ivar_names, options|
55
+ ivar_names.each do |ivar_name|
56
+ ivar = instance_variable_get(ivar_name)
57
+ 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)
61
+ end
62
+ end
38
63
  end
39
64
  end
40
65
 
41
66
  def __decorator_for__(ivar)
42
- __decorator_name_for__(ivar).constantize.decorate(ivar)
67
+ __decorator_name_for__(ivar).constantize
43
68
  end
44
69
 
45
70
  def __decorator_name_for__(ivar)
@@ -48,16 +73,13 @@ private
48
73
 
49
74
  def __model_name_for__(ivar)
50
75
  if ivar.respond_to?(:model_name)
51
- ivar
76
+ source = ivar
52
77
  elsif ivar.class.respond_to?(:model_name)
53
- ivar.class
78
+ source = ivar.class
54
79
  else
55
80
  raise ArgumentError, "#{ivar} does not have an associated model"
56
- end.model_name
57
- end
81
+ end
58
82
 
59
- def self.included(base)
60
- base.class_attribute :__ivars_to_decorate__, :instance_accessor => false
61
- base.extend ClassMethods
83
+ source.model_name
62
84
  end
63
85
  end
@@ -1,5 +1,5 @@
1
1
  module DecoratesBeforeRendering
2
2
  unless defined? DecoratesBeforeRendering::VERSION
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -1,12 +1,14 @@
1
1
  require_relative '../lib/decorates_before_rendering'
2
2
 
3
3
  class MyCompletelyFakeModelDecorator; end
4
+ class MyOtherCompletelyFakeModelDecorator; end
4
5
 
5
6
  describe DecoratesBeforeRendering do
6
7
  # NOTE: these are married together, so they're tested together.
7
8
  describe '::decorates + #render' do
8
9
  let(:sentinel) { double(:sentinel) }
9
10
  let(:ivar) { double('@ivar') }
11
+ let(:ivars) { double('@ivars') }
10
12
 
11
13
  # NOTE: This superclass is here so we know that the correct render gets
12
14
  # called. It can't be defined in the subclass, or else that one
@@ -27,16 +29,17 @@ describe DecoratesBeforeRendering do
27
29
  Class.new(superclass) do
28
30
  include DecoratesBeforeRendering
29
31
 
30
- attr_reader :ivar
32
+ attr_reader :ivar, :ivars
31
33
 
32
- def initialize(sentinel, ivar)
34
+ def initialize(sentinel, ivar, ivars = nil)
33
35
  super(sentinel)
34
36
 
35
37
  @ivar = ivar
38
+ @ivars = ivars
36
39
  end
37
40
  end
38
41
  end
39
- let(:instance) { klass.new(sentinel, ivar) }
42
+ let(:instance) { klass.new(sentinel, ivar, ivars) }
40
43
  let(:args) { double('*args') }
41
44
 
42
45
  context "no ivars" do
@@ -92,5 +95,19 @@ describe DecoratesBeforeRendering do
92
95
  subclass_instance.render(args)
93
96
  end
94
97
  end
98
+
99
+ context "Specify a different decorator class for an automatic decorator" do
100
+ it "should function correctly" do
101
+ klass.decorates(:ivars, :with => MyOtherCompletelyFakeModelDecorator)
102
+ klass.decorates(:ivar)
103
+ subclass_instance = Class.new(klass).new(sentinel, ivar, ivars)
104
+ sentinel.should_receive(:render).with(args)
105
+ MyOtherCompletelyFakeModelDecorator.should_receive(:decorate).with(ivars)
106
+ MyCompletelyFakeModelDecorator.should_receive(:decorate).with(ivar)
107
+ ivar.stub_chain(:class, :model_name => 'MyCompletelyFakeModel')
108
+ subclass_instance.render(args)
109
+ end
110
+ end
95
111
  end
96
112
  end
113
+
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.1
4
+ version: 0.0.2
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-08-21 00:00:00.000000000 Z
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &70158643611580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: 2.10.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: 2.10.0
24
+ version_requirements: *70158643611580
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: rbx-require-relative
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &70158643610220 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,15 +32,10 @@ dependencies:
37
32
  version: 0.0.9
38
33
  type: :development
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: 0.0.9
35
+ version_requirements: *70158643610220
46
36
  - !ruby/object:Gem::Dependency
47
37
  name: activesupport
48
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &70158643609720 !ruby/object:Gem::Requirement
49
39
  none: false
50
40
  requirements:
51
41
  - - ! '>='
@@ -53,12 +43,7 @@ dependencies:
53
43
  version: 3.2.6
54
44
  type: :runtime
55
45
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: 3.2.6
46
+ version_requirements: *70158643609720
62
47
  description: Small add-on for Draper that decorates models before rendering.
63
48
  email:
64
49
  - rob@mediapiston.com
@@ -67,6 +52,7 @@ extensions: []
67
52
  extra_rdoc_files: []
68
53
  files:
69
54
  - .gitignore
55
+ - CHANGELOG.markdown
70
56
  - Gemfile
71
57
  - LICENSE
72
58
  - README.md
@@ -95,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
81
  version: '0'
96
82
  requirements: []
97
83
  rubyforge_project:
98
- rubygems_version: 1.8.24
84
+ rubygems_version: 1.8.17
99
85
  signing_key:
100
86
  specification_version: 3
101
87
  summary: Small add-on for Draper that decorates models before rendering.