showcase 0.0.1 → 0.1.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 +4 -4
- data/README.md +3 -3
- data/lib/showcase/helpers.rb +25 -3
- data/lib/showcase/presenter.rb +7 -7
- data/lib/showcase/version.rb +1 -1
- data/spec/fixtures.rb +18 -2
- data/spec/showcase_spec.rb +17 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33e9eda486961eacbd46b9b6c28c8f8507a372fa
|
4
|
+
data.tar.gz: 5081731bb69b9682a2897e89f1083651d1727033
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5daf3d2356b19ea36a413a11b5eeab4c70ee9a253ae91060879f0a6593339cc63c2cc5d28dd3ac859fc530253839682a63ff8d02e864eaf23de251ed42f2dcb
|
7
|
+
data.tar.gz: 207122c61b08d546ca187823ce209a926670f39db14615cae5bb442649b14bfb23d276b1eae2bd194c3f005905b5b7a01e365f497a400b9d02b680a6928d8963
|
data/README.md
CHANGED
@@ -18,11 +18,11 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
Include Showcase::Helpers
|
21
|
+
Include Showcase::Helpers on your ApplicationController:
|
22
22
|
|
23
23
|
```ruby
|
24
24
|
class ApplicationController < ActionController::Base
|
25
|
-
|
25
|
+
include Showcase::Helpers
|
26
26
|
end
|
27
27
|
```
|
28
28
|
|
@@ -55,7 +55,7 @@ class ProjectPresenter < Showcase::Presenter
|
|
55
55
|
# expects project.task to return an enumerable. automatically wraps each task in a TaskPresenter presenter
|
56
56
|
presents_collection :tasks
|
57
57
|
|
58
|
-
# you can use `
|
58
|
+
# you can use `view_context`, or the shortcut `h`, to access the context.
|
59
59
|
# `object` refers to the object being presented
|
60
60
|
def title
|
61
61
|
h.link_to object.title, object
|
data/lib/showcase/helpers.rb
CHANGED
@@ -1,6 +1,29 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
1
4
|
module Showcase
|
2
5
|
module Helpers
|
3
|
-
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
if respond_to?(:helper_method)
|
10
|
+
helper_method :present
|
11
|
+
helper_method :present_collection
|
12
|
+
end
|
13
|
+
if respond_to?(:hide_action)
|
14
|
+
hide_action :presenter_context
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def presenter_context
|
19
|
+
if respond_to? :view_context
|
20
|
+
view_context
|
21
|
+
else
|
22
|
+
self
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def present(obj, klass = nil, context = presenter_context)
|
4
27
|
if obj.is_a? ::Showcase::Presenter
|
5
28
|
obj
|
6
29
|
else
|
@@ -9,10 +32,9 @@ module Showcase
|
|
9
32
|
end
|
10
33
|
end
|
11
34
|
|
12
|
-
def present_collection(obj, klass = nil, context =
|
35
|
+
def present_collection(obj, klass = nil, context = presenter_context)
|
13
36
|
obj.map { |o| present(o, klass, context) }
|
14
37
|
end
|
15
38
|
end
|
16
39
|
end
|
17
40
|
|
18
|
-
|
data/lib/showcase/presenter.rb
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
require 'delegate'
|
2
|
-
require 'active_support/inflector'
|
3
2
|
require 'showcase/helpers'
|
4
3
|
|
5
4
|
module Showcase
|
6
5
|
class Presenter < SimpleDelegator
|
7
6
|
include Helpers
|
8
7
|
|
9
|
-
attr_reader :
|
8
|
+
attr_reader :view_context
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
alias_method :object, :__getobj__
|
11
|
+
alias_method :h, :view_context
|
13
12
|
|
14
13
|
def class
|
15
14
|
object.class
|
@@ -17,13 +16,13 @@ module Showcase
|
|
17
16
|
|
18
17
|
def initialize(obj, context)
|
19
18
|
super(obj)
|
20
|
-
@
|
19
|
+
@view_context = context
|
21
20
|
end
|
22
21
|
|
23
22
|
def self.presents(*attrs)
|
24
23
|
attrs.each do |attr|
|
25
24
|
define_method attr do
|
26
|
-
present(object.send(attr), nil,
|
25
|
+
present(object.send(attr), nil, view_context)
|
27
26
|
end
|
28
27
|
end
|
29
28
|
end
|
@@ -31,9 +30,10 @@ module Showcase
|
|
31
30
|
def self.presents_collection(*attrs)
|
32
31
|
attrs.each do |attr|
|
33
32
|
define_method attr do
|
34
|
-
present_collection(object.send(attr), nil,
|
33
|
+
present_collection(object.send(attr), nil, view_context)
|
35
34
|
end
|
36
35
|
end
|
37
36
|
end
|
38
37
|
end
|
39
38
|
end
|
39
|
+
|
data/lib/showcase/version.rb
CHANGED
data/spec/fixtures.rb
CHANGED
@@ -10,6 +10,10 @@ class Project < Struct.new(:name)
|
|
10
10
|
[ Person.new("Ju Liu") ]
|
11
11
|
end
|
12
12
|
|
13
|
+
def first_collaborator
|
14
|
+
collaborators.first
|
15
|
+
end
|
16
|
+
|
13
17
|
def dummy
|
14
18
|
"foobar"
|
15
19
|
end
|
@@ -19,6 +23,10 @@ class PersonPresenter < Showcase::Presenter
|
|
19
23
|
def sex
|
20
24
|
'male'
|
21
25
|
end
|
26
|
+
|
27
|
+
def bold_name
|
28
|
+
h.bold(object.name)
|
29
|
+
end
|
22
30
|
end
|
23
31
|
|
24
32
|
class ProjectPresenter < Showcase::Presenter
|
@@ -29,11 +37,19 @@ class ProjectPresenter < Showcase::Presenter
|
|
29
37
|
"Presented #{object.name}"
|
30
38
|
end
|
31
39
|
|
32
|
-
def
|
33
|
-
h.
|
40
|
+
def bold_name
|
41
|
+
h.bold(object.name)
|
42
|
+
end
|
43
|
+
|
44
|
+
def first_collaborator
|
45
|
+
present(object.first_collaborator)
|
34
46
|
end
|
35
47
|
end
|
36
48
|
|
37
49
|
class Context
|
38
50
|
include Showcase::Helpers
|
51
|
+
|
52
|
+
def bold(text)
|
53
|
+
"**#{text}**"
|
54
|
+
end
|
39
55
|
end
|
data/spec/showcase_spec.rb
CHANGED
@@ -2,14 +2,13 @@ require 'showcase'
|
|
2
2
|
require_relative './fixtures'
|
3
3
|
|
4
4
|
describe Showcase::Presenter do
|
5
|
-
|
6
|
-
let(:context) { stub(:context, foo: 'bar') }
|
5
|
+
let(:context) { Context.new }
|
7
6
|
let(:object) { Project.new('Showcase') }
|
8
7
|
let(:subject) { ProjectPresenter.new(object, context) }
|
9
8
|
|
10
9
|
it 'takes the object and a context as parameters' do
|
11
10
|
subject.object.should == object
|
12
|
-
subject.
|
11
|
+
subject.view_context.should == context
|
13
12
|
end
|
14
13
|
|
15
14
|
it 'preserves original .class' do
|
@@ -25,19 +24,31 @@ describe Showcase::Presenter do
|
|
25
24
|
end
|
26
25
|
|
27
26
|
it 'allows .h as shortcut to access the context' do
|
28
|
-
subject.
|
27
|
+
subject.bold_name.should == '**Showcase**'
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '.present' do
|
31
|
+
it 'passes the context' do
|
32
|
+
subject.first_collaborator.bold_name.should == '**Ju Liu**'
|
33
|
+
end
|
29
34
|
end
|
30
35
|
|
31
36
|
describe '#presents' do
|
32
37
|
it 'wraps the specified attributes inside a presenter' do
|
33
38
|
subject.owner.sex.should == 'male'
|
34
39
|
end
|
40
|
+
it 'passes the context' do
|
41
|
+
subject.owner.bold_name.should == '**Stefano Verna**'
|
42
|
+
end
|
35
43
|
end
|
36
44
|
|
37
45
|
describe '#presents_collection' do
|
38
46
|
it 'wraps the specifieed collection attributes inside a presenter' do
|
39
47
|
subject.collaborators.first.sex.should == 'male'
|
40
48
|
end
|
49
|
+
it 'passes the context' do
|
50
|
+
subject.collaborators.first.bold_name.should == '**Ju Liu**'
|
51
|
+
end
|
41
52
|
end
|
42
53
|
end
|
43
54
|
|
@@ -63,7 +74,7 @@ describe Showcase::Helpers do
|
|
63
74
|
end
|
64
75
|
it 'the context to use can be specified as third parameter' do
|
65
76
|
different_context = stub
|
66
|
-
context.present(object, ProjectPresenter, different_context).
|
77
|
+
context.present(object, ProjectPresenter, different_context).view_context.should == different_context
|
67
78
|
end
|
68
79
|
end
|
69
80
|
end
|
@@ -79,5 +90,5 @@ describe Showcase::Helpers do
|
|
79
90
|
presented_collection.should == [ 'foo', 'bar' ]
|
80
91
|
end
|
81
92
|
end
|
82
|
-
|
83
93
|
end
|
94
|
+
|