presents 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +23 -0
- data/.gemtest +0 -0
- data/History.txt +6 -0
- data/Manifest.txt +11 -0
- data/README.txt +62 -0
- data/Rakefile +24 -0
- data/lib/presents.rb +5 -0
- data/lib/presents/aggregate_presenter.rb +60 -0
- data/lib/presents/application_helper_methods.rb +60 -0
- data/lib/presents/base_presenter.rb +69 -0
- data/lib/presents/railtie.rb +7 -0
- data/test/test_presents.rb +21 -0
- metadata +160 -0
data/.autotest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'autotest/restart'
|
4
|
+
|
5
|
+
# Autotest.add_hook :initialize do |at|
|
6
|
+
# at.extra_files << "../some/external/dependency.rb"
|
7
|
+
#
|
8
|
+
# at.libs << ":../some/external"
|
9
|
+
#
|
10
|
+
# at.add_exception 'vendor'
|
11
|
+
#
|
12
|
+
# at.add_mapping(/dependency.rb/) do |f, _|
|
13
|
+
# at.files_matching(/test_.*rb$/)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# %w(TestA TestB).each do |klass|
|
17
|
+
# at.extra_class_map[klass] = "test/test_misc.rb"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Autotest.add_hook :run_command do |at|
|
22
|
+
# system "rake build"
|
23
|
+
# end
|
data/.gemtest
ADDED
File without changes
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= presents
|
2
|
+
|
3
|
+
* https://github.com/matthewcalebsmith/presents
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Minimalist presenter pattern for Rails
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Present models in a view context through ApplicationHelper methods.
|
12
|
+
* Wrap any object by passing it to presenter's initalize method.
|
13
|
+
* Bulk up tests.
|
14
|
+
* Bulk up documentation.
|
15
|
+
|
16
|
+
== SYNOPSIS:
|
17
|
+
|
18
|
+
- present @foo do |_foo|
|
19
|
+
= @foo.do_normal
|
20
|
+
= _foo.do_special
|
21
|
+
|
22
|
+
== REQUIREMENTS:
|
23
|
+
|
24
|
+
* None besides Rails. Yea!
|
25
|
+
|
26
|
+
== INSTALL:
|
27
|
+
|
28
|
+
* gem 'presents'
|
29
|
+
|
30
|
+
== DEVELOPERS:
|
31
|
+
|
32
|
+
After checking out the source, run:
|
33
|
+
|
34
|
+
$ rake newb
|
35
|
+
|
36
|
+
This task will install any missing dependencies, run the tests/specs,
|
37
|
+
and generate the RDoc.
|
38
|
+
|
39
|
+
== LICENSE:
|
40
|
+
|
41
|
+
(The MIT License)
|
42
|
+
|
43
|
+
Copyright (c) 2012 Matthew Smith
|
44
|
+
|
45
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
46
|
+
a copy of this software and associated documentation files (the
|
47
|
+
'Software'), to deal in the Software without restriction, including
|
48
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
49
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
50
|
+
permit persons to whom the Software is furnished to do so, subject to
|
51
|
+
the following conditions:
|
52
|
+
|
53
|
+
The above copyright notice and this permission notice shall be
|
54
|
+
included in all copies or substantial portions of the Software.
|
55
|
+
|
56
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
57
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
58
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
59
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
60
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
61
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
62
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
|
6
|
+
Hoe.plugin :git
|
7
|
+
Hoe.plugin :minitest
|
8
|
+
Hoe.plugin :isolate
|
9
|
+
|
10
|
+
Hoe.spec 'presents' do
|
11
|
+
# HEY! If you fill these out in ~/.hoe_template/Rakefile.erb then
|
12
|
+
# you'll never have to touch them again!
|
13
|
+
# (delete this comment too, of course)
|
14
|
+
|
15
|
+
developer('Matt Smith', 'matt@nearapogee.com')
|
16
|
+
|
17
|
+
license 'MIT'
|
18
|
+
|
19
|
+
dependency('rails', '>1.0.0')
|
20
|
+
dependency('isolate', '~>3.2.2', :dev)
|
21
|
+
dependency('hoe-git', '~>1.5.0', :dev)
|
22
|
+
end
|
23
|
+
|
24
|
+
# vim: syntax=ruby
|
data/lib/presents.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Presents
|
2
|
+
class AggregatePresenter
|
3
|
+
|
4
|
+
# Internal: Create a new presenter.
|
5
|
+
#
|
6
|
+
# Called by present method in ApplicationHelper.
|
7
|
+
#
|
8
|
+
# presenters - A hash of the access method name and the presenter
|
9
|
+
#
|
10
|
+
# Returns self.
|
11
|
+
def initialize(presenters, template)
|
12
|
+
@presenters = presenters
|
13
|
+
@template = template
|
14
|
+
|
15
|
+
@presenters.each do |presenter|
|
16
|
+
class_eval do
|
17
|
+
define_method(presenter.class.name.underscore.to_sym) do
|
18
|
+
presenter
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
# Public: Access the view for helper methods.
|
26
|
+
#
|
27
|
+
# Examples
|
28
|
+
#
|
29
|
+
# class XxxPresenter < AggregatePresenter
|
30
|
+
# ...
|
31
|
+
# def my_attribute
|
32
|
+
# h.content_tag :span, "Duck", :class => "mallard"
|
33
|
+
# end
|
34
|
+
# ...
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# Returns the template.
|
38
|
+
def h
|
39
|
+
@template
|
40
|
+
end
|
41
|
+
|
42
|
+
# Internal: Send any unknown methods to the template.
|
43
|
+
#
|
44
|
+
# This is done so you don't have to access them through h.
|
45
|
+
#
|
46
|
+
# Examples
|
47
|
+
#
|
48
|
+
# class XxxPresenter < AggregatePresenter
|
49
|
+
# ...
|
50
|
+
# def my_attribute
|
51
|
+
# content_tag :span, "Duck", :class => "mallard"
|
52
|
+
# end
|
53
|
+
# ...
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
def method_missing(*args, &block)
|
57
|
+
@template.send(*args, &block)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Presents
|
2
|
+
module ApplicationHelperMethods
|
3
|
+
|
4
|
+
# Public: Render a context in the view to wrap a model with a presenter.
|
5
|
+
#
|
6
|
+
# object - The object (model) to be presented.
|
7
|
+
# cls - Defaults to nil. Allows manual specification of which Presenter
|
8
|
+
# to use.
|
9
|
+
#
|
10
|
+
# Examples
|
11
|
+
#
|
12
|
+
# <% present @user do |user_presenter| %>
|
13
|
+
# <%= user_presenter.do_presenter_method %>
|
14
|
+
# <%= user_presenter.do_model_method %>
|
15
|
+
# <% end %>
|
16
|
+
#
|
17
|
+
# Returns the presenter for the model.
|
18
|
+
def present(object, cls = nil)
|
19
|
+
cls ||= "#{object.class}Presenter".constantize
|
20
|
+
presenter = cls.new(object, self)
|
21
|
+
yield presenter if block_given?
|
22
|
+
presenter
|
23
|
+
end
|
24
|
+
|
25
|
+
# Public: Render a context in the view to wrap thwo presenters in an
|
26
|
+
# aggregate presenter.
|
27
|
+
#
|
28
|
+
# cls - Full classname constant of the aggregate presenter. This is required
|
29
|
+
# unlike the present method.
|
30
|
+
# presenters - Variable argument list of presenters/objects. They are
|
31
|
+
# accessed by their underscore class name in the presenter. There
|
32
|
+
# should never be a need to pass two of the same class.
|
33
|
+
#
|
34
|
+
# Examples
|
35
|
+
#
|
36
|
+
# <% present @user do |user_presenter| %>
|
37
|
+
# <% present @organization do |organization_presenter| %>
|
38
|
+
# <% aggregate UserOrganizationPresenter, user_presenter, organization_presenter do |user_organization_presenter| %>
|
39
|
+
#
|
40
|
+
# <%= user_presenter.do_presenter_method %>
|
41
|
+
# <%= user_presenter.do_model_method %>
|
42
|
+
#
|
43
|
+
# <%= organization_presenter.do_presenter_method %>
|
44
|
+
# <%= organization_presenter.do_model_method %>
|
45
|
+
#
|
46
|
+
# <%= user_organization_presenter.do_presenter_method %>
|
47
|
+
# <%= user_organization_presenter.do_model_method %>
|
48
|
+
# <% end %>
|
49
|
+
# <% end %>
|
50
|
+
# <% end %>
|
51
|
+
#
|
52
|
+
# Returns the aggregate presenter for the model.
|
53
|
+
def aggregate(cls, *presenters)
|
54
|
+
presenter = cls.new(presenters, self)
|
55
|
+
yield presenter if block_given?
|
56
|
+
presenter
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Presents
|
2
|
+
class BasePresenter
|
3
|
+
|
4
|
+
# Internal: Create a new presenter.
|
5
|
+
#
|
6
|
+
# Called by present method in ApplicationHelper.
|
7
|
+
#
|
8
|
+
def initialize(object, template = nil)
|
9
|
+
@object = object
|
10
|
+
@template = template
|
11
|
+
end
|
12
|
+
|
13
|
+
# Public: Create an accessor to the wrapped object.
|
14
|
+
#
|
15
|
+
# Examples
|
16
|
+
#
|
17
|
+
# class DuckPresenter < BasePresenter
|
18
|
+
# presents :duck
|
19
|
+
#
|
20
|
+
# ...
|
21
|
+
#
|
22
|
+
# def my_attribute
|
23
|
+
# h.number_as_currency(duck.attribute)
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
def self.presents(name)
|
29
|
+
define_method(name) do
|
30
|
+
@object
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Public: Access the view for helper methods.
|
35
|
+
#
|
36
|
+
# Examples
|
37
|
+
#
|
38
|
+
# class XxxPresenter < BasePresenter
|
39
|
+
# ...
|
40
|
+
# def my_attribute
|
41
|
+
# h.content_tag :span, "Duck", :class => "mallard"
|
42
|
+
# end
|
43
|
+
# ...
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# Returns the template.
|
47
|
+
def h
|
48
|
+
@template
|
49
|
+
end
|
50
|
+
|
51
|
+
# Internal: Send any unknown methods to the template.
|
52
|
+
#
|
53
|
+
# This is done so you don't have to access them through h.
|
54
|
+
#
|
55
|
+
# Examples
|
56
|
+
#
|
57
|
+
# class XxxPresenter < BasePresenter
|
58
|
+
# ...
|
59
|
+
# def my_attribute
|
60
|
+
# content_tag :span, "Duck", :class => "mallard"
|
61
|
+
# end
|
62
|
+
# ...
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
def method_missing(*args, &block)
|
66
|
+
@template.send(*args, &block) if @template
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "rails"
|
3
|
+
require "presents"
|
4
|
+
|
5
|
+
class TestPresents < Test::Unit::TestCase
|
6
|
+
class Foo
|
7
|
+
def name
|
8
|
+
"Foo"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
class MyPresenter < Presents::BasePresenter
|
12
|
+
presents :foo
|
13
|
+
def name_upcase
|
14
|
+
foo.name.upcase
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_base_presenter_wraps_object
|
19
|
+
assert_equal "FOO", MyPresenter.new(Foo.new).name_upcase
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: presents
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matt Smith
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>'
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>'
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.4'
|
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: '3.4'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rdoc
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.10'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.10'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: isolate
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 3.2.2
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 3.2.2
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: hoe-git
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 1.5.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.5.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: hoe
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '3.1'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '3.1'
|
110
|
+
description: Minimalist presenter pattern for Rails
|
111
|
+
email:
|
112
|
+
- matt@nearapogee.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files:
|
116
|
+
- History.txt
|
117
|
+
- Manifest.txt
|
118
|
+
- README.txt
|
119
|
+
files:
|
120
|
+
- .autotest
|
121
|
+
- History.txt
|
122
|
+
- Manifest.txt
|
123
|
+
- README.txt
|
124
|
+
- Rakefile
|
125
|
+
- lib/presents.rb
|
126
|
+
- lib/presents/aggregate_presenter.rb
|
127
|
+
- lib/presents/application_helper_methods.rb
|
128
|
+
- lib/presents/base_presenter.rb
|
129
|
+
- lib/presents/railtie.rb
|
130
|
+
- test/test_presents.rb
|
131
|
+
- .gemtest
|
132
|
+
homepage: https://github.com/matthewcalebsmith/presents
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options:
|
137
|
+
- --main
|
138
|
+
- README.txt
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ! '>='
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
none: false
|
149
|
+
requirements:
|
150
|
+
- - ! '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project: presents
|
155
|
+
rubygems_version: 1.8.23
|
156
|
+
signing_key:
|
157
|
+
specification_version: 3
|
158
|
+
summary: Minimalist presenter pattern for Rails
|
159
|
+
test_files:
|
160
|
+
- test/test_presents.rb
|