showcase 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5957c56e03cde7b1dd14cf36b1dc35d970cf207
4
- data.tar.gz: 4ce0737cf2e57c29f3c5a5b618c662c48597c73f
3
+ metadata.gz: 93da90243af0c3fe8b795954fbdf5df12a28007b
4
+ data.tar.gz: 463b88936212e9e181cb15fe44613e947ab116cb
5
5
  SHA512:
6
- metadata.gz: d073f3e53bc1117267e08a8e18ecc0ad7f69f5488583d1c8b8c479f5c6d700f8ab307e04bd7276ed5bf9225bbc1230708fa51574f78633ad784a73d77ccfcb66
7
- data.tar.gz: a53b94115a6826d77656234eb136bf5a17bd7b7a7ac2fb87599f47c5eae07cd4637d7e73c796d0a5b523141bc689bffcf87a0e53007655471b95315753ffca48
6
+ metadata.gz: b7a38f5e9d326581b10ea36a15ffd78019a5b3fae9deb475ed6565723e187e3894383ee1ec3c8574784cd3c25e65aeed63f8793891ca8b66db588780371e615a
7
+ data.tar.gz: 69ed3a3ac0c0dbc8c7a12f4d1b0fd018a5ffe465ddb611de1ba576e2fbca792e692b09296a93e33e6a90eb63fb447d9821dda374a9122c6e3ba30a838218d059
@@ -1,5 +1,6 @@
1
1
  require 'active_support/concern'
2
2
  require 'active_support/inflector'
3
+ require 'active_support/core_ext/hash/keys'
3
4
 
4
5
  module Showcase
5
6
  module Helpers
@@ -13,13 +14,19 @@ module Showcase
13
14
  end
14
15
  end
15
16
 
16
- def present(obj, klass = nil, context = presenter_context)
17
- klass ||= "#{obj.class.name}Presenter".constantize
18
- klass.new(obj, context)
17
+ def present(obj, klass = nil, context = presenter_context, options = {})
18
+ options.assert_valid_keys(:nil_presenter)
19
+
20
+ if obj || options.fetch(:nil_presenter, false)
21
+ klass ||= "#{obj.class.name}Presenter".constantize
22
+ klass.new(obj, context)
23
+ else
24
+ nil
25
+ end
19
26
  end
20
27
 
21
- def present_collection(obj, klass = nil, context = presenter_context)
22
- obj.map { |o| present(o, klass, context) }
28
+ def present_collection(obj, klass = nil, context = presenter_context, options = {})
29
+ obj.map { |o| present(o, klass, context, options) }
23
30
  end
24
31
  end
25
32
  end
@@ -43,13 +43,17 @@ module Showcase
43
43
 
44
44
  def self.wrap_methods(args, method)
45
45
  options = args.extract_options!
46
- options.assert_valid_keys(:with)
46
+ options.assert_valid_keys(:with, :nil_presenter)
47
47
  presenter_klass = options.fetch(:with, nil)
48
48
 
49
49
  methods_module = Module.new do
50
50
  args.each do |attr|
51
51
  define_method attr do
52
- send(method, object.send(attr), presenter_klass, view_context)
52
+ send(method,
53
+ object.send(attr),
54
+ presenter_klass,
55
+ view_context,
56
+ options.slice(:nil_presenter))
53
57
  end
54
58
  end
55
59
  end
@@ -1,4 +1,4 @@
1
1
  module Showcase
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
4
4
 
data/spec/fixtures.rb CHANGED
@@ -14,6 +14,12 @@ class Project < Struct.new(:name)
14
14
  collaborators.first
15
15
  end
16
16
 
17
+ def owner_child
18
+ end
19
+
20
+ def foobar
21
+ end
22
+
17
23
  def dummy
18
24
  "foobar"
19
25
  end
@@ -39,6 +45,7 @@ class ProjectPresenter < Showcase::Presenter
39
45
  presents :owner, with: AdminPresenter
40
46
  presents_collection :collaborators
41
47
  presents :owner_child
48
+ presents :foobar, nil_presenter: true
42
49
 
43
50
  def name
44
51
  "Presented #{object.name}"
@@ -57,6 +64,12 @@ class ProjectPresenter < Showcase::Presenter
57
64
  end
58
65
  end
59
66
 
67
+ class NilClassPresenter < Showcase::Presenter
68
+ def hi
69
+ "hi!"
70
+ end
71
+ end
72
+
60
73
  class Context
61
74
  include Showcase::Helpers
62
75
 
@@ -55,6 +55,12 @@ describe Showcase::Presenter do
55
55
  it 'passes the context' do
56
56
  subject.owner.bold_name.should == '**Admin Stefano Verna**'
57
57
  end
58
+ it 'returns null if nil_presenter is false or undefined' do
59
+ subject.owner_child.should be_nil
60
+ end
61
+ it 'returns a null presenter if nil_presenter is true' do
62
+ subject.foobar.hi.should eq "hi!"
63
+ end
58
64
  end
59
65
 
60
66
  describe '#presents_collection' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: showcase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-31 00:00:00.000000000 Z
11
+ date: 2013-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport