decorates_before_rendering 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in decorates_before_rendering.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Rob Hanlon
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # DecoratesBeforeRendering
2
+
3
+ A small add-on for [Draper](http://github.com/jcasimir/draper) that automatically decorates
4
+ specified controller instance variables before rendering. Currently only works on objects
5
+ that respond to ```model_name````, but this could easily be expanded upon.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'decorates_before_rendering'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install decorates_before_rendering
20
+
21
+ ## Usage
22
+
23
+ See ```lib/decorates_before_rendering.rb``` for usage instructions.
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/decorates_before_rendering/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Rob Hanlon"]
6
+ gem.email = ["rob@mediapiston.com"]
7
+ gem.description = %q{Small add-on for Draper that decorates models before rendering.}
8
+ gem.summary = %q{Small add-on for Draper that decorates models before rendering.}
9
+ gem.homepage = "http://github.com/ohwillie/decorates_before_rendering"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "decorates_before_rendering"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = DecoratesBeforeRendering::VERSION
17
+
18
+ gem.add_development_dependency 'rspec', '>= 2.10.0'
19
+ gem.add_development_dependency 'rbx-require-relative', '>= 0.0.9'
20
+
21
+ gem.add_dependency 'activesupport', '>= 3.2.6'
22
+ end
@@ -0,0 +1,5 @@
1
+ module DecoratesBeforeRendering
2
+ unless defined? DecoratesBeforeRendering::VERSION
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,63 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "decorates_before_rendering/version"
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'active_support/core_ext/class/attribute'
5
+
6
+ # Decorates the specified fields. For instance, if you have
7
+ #
8
+ # class StuffController < ApplicationController
9
+ # include DecoratesBeforeRendering
10
+ #
11
+ # decorates :thing_1, :thing_2
12
+ # end
13
+ #
14
+ # @thing_1 and @thing_2 will be decorated right before a rendering occurs.
15
+ #
16
+ module DecoratesBeforeRendering
17
+ module ClassMethods
18
+ def decorates(*unsigiled_ivar_names)
19
+ self.__ivars_to_decorate__ = unsigiled_ivar_names.map { |i| "@#{i}" }
20
+ end
21
+ end
22
+
23
+ def render(*args)
24
+ __decorate_ivars__
25
+ super(*args)
26
+ end
27
+
28
+ private
29
+
30
+ def __decorate_ivars__
31
+ ivars_to_decorate = self.class.__ivars_to_decorate__
32
+
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?
38
+ end
39
+ end
40
+
41
+ def __decorator_for__(ivar)
42
+ __decorator_name_for__(ivar).constantize.decorate(ivar)
43
+ end
44
+
45
+ def __decorator_name_for__(ivar)
46
+ "#{__model_name_for__(ivar)}Decorator"
47
+ end
48
+
49
+ def __model_name_for__(ivar)
50
+ if ivar.respond_to?(:model_name)
51
+ ivar
52
+ elsif ivar.class.respond_to?(:model_name)
53
+ ivar.class
54
+ else
55
+ raise ArgumentError, "#{ivar} does not have an associated model"
56
+ end.model_name
57
+ end
58
+
59
+ def self.included(base)
60
+ base.class_attribute :__ivars_to_decorate__, :instance_accessor => false
61
+ base.extend ClassMethods
62
+ end
63
+ end
@@ -0,0 +1,96 @@
1
+ require_relative '../lib/decorates_before_rendering'
2
+
3
+ class MyCompletelyFakeModelDecorator; end
4
+
5
+ describe DecoratesBeforeRendering do
6
+ # NOTE: these are married together, so they're tested together.
7
+ describe '::decorates + #render' do
8
+ let(:sentinel) { double(:sentinel) }
9
+ let(:ivar) { double('@ivar') }
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)
23
+ end
24
+ end
25
+ end
26
+ let(:klass) do
27
+ Class.new(superclass) do
28
+ include DecoratesBeforeRendering
29
+
30
+ attr_reader :ivar
31
+
32
+ def initialize(sentinel, ivar)
33
+ super(sentinel)
34
+
35
+ @ivar = ivar
36
+ end
37
+ end
38
+ end
39
+ let(:instance) { klass.new(sentinel, ivar) }
40
+ let(:args) { double('*args') }
41
+
42
+ context "no ivars" do
43
+ it 'should render' do
44
+ sentinel.should_receive(:render).with(args)
45
+ instance.render(args)
46
+ end
47
+ end
48
+
49
+ context "ivar is not present" do
50
+ it 'should render' do
51
+ sentinel.should_receive(:render).with(args)
52
+ instance.render(args)
53
+ end
54
+ end
55
+
56
+ context "cannot find model name for ivar" do
57
+ it 'should raise an ArgumentError' do
58
+ klass.decorates(:ivar)
59
+ expect {
60
+ instance.render(args)
61
+ }.to raise_error(ArgumentError)
62
+ end
63
+ end
64
+
65
+ context "ivar responds to model name" do
66
+ it "should decorate and render" do
67
+ sentinel.should_receive(:render).with(args)
68
+ MyCompletelyFakeModelDecorator.should_receive(:decorate).with(ivar)
69
+ ivar.stub(:model_name => 'MyCompletelyFakeModel')
70
+ klass.decorates(:ivar)
71
+ instance.render(args)
72
+ end
73
+ end
74
+
75
+ context "ivar's class responds to model name" do
76
+ it "should decorate and render" do
77
+ sentinel.should_receive(:render).with(args)
78
+ MyCompletelyFakeModelDecorator.should_receive(:decorate).with(ivar)
79
+ ivar.stub_chain(:class, :model_name => 'MyCompletelyFakeModel')
80
+ klass.decorates(:ivar)
81
+ instance.render(args)
82
+ end
83
+ end
84
+
85
+ context "subclass inherits attributes" do
86
+ it "should function correctly" do
87
+ klass.decorates(:ivar)
88
+ subclass_instance = Class.new(klass).new(sentinel, ivar)
89
+ sentinel.should_receive(:render).with(args)
90
+ MyCompletelyFakeModelDecorator.should_receive(:decorate).with(ivar)
91
+ ivar.stub_chain(:class, :model_name => 'MyCompletelyFakeModel')
92
+ subclass_instance.render(args)
93
+ end
94
+ end
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: decorates_before_rendering
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Hanlon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.10.0
22
+ type: :development
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
30
+ - !ruby/object:Gem::Dependency
31
+ name: rbx-require-relative
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.9
38
+ type: :development
39
+ 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
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.6
54
+ type: :runtime
55
+ 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
62
+ description: Small add-on for Draper that decorates models before rendering.
63
+ email:
64
+ - rob@mediapiston.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - decorates_before_rendering.gemspec
75
+ - lib/decorates_before_rendering.rb
76
+ - lib/decorates_before_rendering/version.rb
77
+ - spec/decorates_before_rendering_spec.rb
78
+ homepage: http://github.com/ohwillie/decorates_before_rendering
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.24
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Small add-on for Draper that decorates models before rendering.
102
+ test_files:
103
+ - spec/decorates_before_rendering_spec.rb