bourgeois 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/README.md +13 -1
- data/gemfiles/Gemfile.activemodel-3.2.x +6 -0
- data/gemfiles/Gemfile.activemodel-4.0 +6 -0
- data/lib/bourgeois/errors.rb +11 -0
- data/lib/bourgeois/presenter.rb +5 -0
- data/lib/bourgeois/railtie.rb +1 -3
- data/lib/bourgeois/version.rb +1 -1
- data/lib/bourgeois/view_helper.rb +10 -2
- data/lib/bourgeois.rb +6 -0
- data/spec/bourgeois/presenter_spec.rb +25 -1
- data/spec/bourgeois/view_helper_spec.rb +56 -0
- data/spec/spec_helper.rb +2 -2
- metadata +9 -4
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Bourgeois
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/bourgeois.png)](https://rubygems.org/gems/bourgeois)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/mirego/bourgeois.png)](https://codeclimate.com/github/mirego/bourgeois)
|
4
5
|
[![Build Status](https://travis-ci.org/mirego/bourgeois.png?branch=master)](https://travis-ci.org/mirego/bourgeois)
|
5
6
|
|
6
7
|
Bourgeois is a Ruby library that makes using presenters a very simple thing.
|
@@ -41,7 +42,18 @@ Then, you can use the `present` helper in your views to wrap `ActiveModel` (and
|
|
41
42
|
<% end %>
|
42
43
|
```
|
43
44
|
|
44
|
-
Methods that aren’t in the presenter (`first_name` and `last_name`) are delegated to the presented object. You can also use the `view` method in the presenter to get the original view it was called in
|
45
|
+
Methods that aren’t in the presenter (`first_name` and `last_name`) are delegated to the presented object. You can also use the `view` method in the presenter to get the original view it was called in:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# app/presenters/user_presenter.rb
|
49
|
+
|
50
|
+
class UserPresenter < Bourgeois::Presenter
|
51
|
+
def birthdate
|
52
|
+
# To get the original `birthdate` value, you can either use `super` or `object.birthdate`
|
53
|
+
super.presence || view.content_tag(:em, 'Unknown')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
45
57
|
|
46
58
|
## Inspiration
|
47
59
|
|
data/lib/bourgeois/presenter.rb
CHANGED
@@ -33,6 +33,11 @@ module Bourgeois
|
|
33
33
|
@view
|
34
34
|
end
|
35
35
|
|
36
|
+
# Return the original delegated object
|
37
|
+
def object
|
38
|
+
@object
|
39
|
+
end
|
40
|
+
|
36
41
|
# Return the original object class based on the presenter class name
|
37
42
|
# We would be able to use `@object.class` but we need this in class methods
|
38
43
|
def self.klass
|
data/lib/bourgeois/railtie.rb
CHANGED
@@ -4,9 +4,7 @@ require 'rails'
|
|
4
4
|
module Bourgeois
|
5
5
|
class Railtie < Rails::Railtie
|
6
6
|
initializer 'bourgeois.action_view' do |app|
|
7
|
-
ActiveSupport.on_load :action_view
|
8
|
-
ActionView::Base.send(:include, ViewHelper)
|
9
|
-
end
|
7
|
+
ActiveSupport.on_load :action_view, {}, &Bourgeois.inject_into_action_view
|
10
8
|
end
|
11
9
|
end
|
12
10
|
end
|
data/lib/bourgeois/version.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Bourgeois
|
2
2
|
module ViewHelper
|
3
|
-
# Wrap a resource or a collection into its related
|
3
|
+
# Wrap a resource or a collection into its related presenter
|
4
4
|
#
|
5
5
|
# @example
|
6
6
|
# present User.new(name: 'Remi') do |user|
|
@@ -10,7 +10,15 @@ module Bourgeois
|
|
10
10
|
def present(object, klass = nil, &blk)
|
11
11
|
return object.map { |o| present(o, klass, &blk) } if object.respond_to?(:to_a)
|
12
12
|
|
13
|
-
klass
|
13
|
+
if klass.blank?
|
14
|
+
begin
|
15
|
+
klass_name = "#{object.class}Presenter"
|
16
|
+
klass = klass_name.constantize
|
17
|
+
rescue ::NameError
|
18
|
+
raise UnknownPresenter.new(klass_name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
14
22
|
presenter = klass.new(object, self)
|
15
23
|
yield presenter if block_given?
|
16
24
|
|
data/lib/bourgeois.rb
CHANGED
@@ -4,10 +4,16 @@ require 'delegate'
|
|
4
4
|
require 'active_model'
|
5
5
|
require 'action_view'
|
6
6
|
|
7
|
+
require 'bourgeois/errors'
|
7
8
|
require 'bourgeois/presenter'
|
8
9
|
require 'bourgeois/view_helper'
|
9
10
|
|
10
11
|
module Bourgeois
|
12
|
+
def self.inject_into_action_view
|
13
|
+
@inject_into_action_view ||= Proc.new do
|
14
|
+
ActionView::Base.send(:include, ViewHelper)
|
15
|
+
end
|
16
|
+
end
|
11
17
|
end
|
12
18
|
|
13
19
|
require 'bourgeois/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Bourgeois::Presenter do
|
4
|
-
let(:user) { User.new first_name: 'Patrick', last_name: 'Bourgeois' }
|
4
|
+
let(:user) { User.new first_name: 'Patrick', last_name: 'Bourgeois', birthdate: '1962-06-16' }
|
5
5
|
let(:view) { ActionView::Base.new }
|
6
6
|
let(:presenter) { UserPresenter.new(user, view) }
|
7
7
|
|
@@ -11,6 +11,10 @@ describe Bourgeois::Presenter do
|
|
11
11
|
def formatted_name
|
12
12
|
"#{first_name} #{last_name}".strip
|
13
13
|
end
|
14
|
+
|
15
|
+
def birthdate
|
16
|
+
super.presence || 'Unknown'
|
17
|
+
end
|
14
18
|
end
|
15
19
|
|
16
20
|
class User < OpenStruct
|
@@ -20,6 +24,7 @@ describe Bourgeois::Presenter do
|
|
20
24
|
it { expect(presenter.formatted_name).to eql 'Patrick Bourgeois' }
|
21
25
|
it { expect(presenter.first_name).to eql 'Patrick' }
|
22
26
|
it { expect(presenter.last_name).to eql 'Bourgeois' }
|
27
|
+
it { expect(presenter.birthdate).to eql '1962-06-16' }
|
23
28
|
end
|
24
29
|
|
25
30
|
describe :InstanceMethods do
|
@@ -39,6 +44,10 @@ describe Bourgeois::Presenter do
|
|
39
44
|
def local_name
|
40
45
|
view.t('users.attributes.local_name')
|
41
46
|
end
|
47
|
+
|
48
|
+
def first_name_in_bold
|
49
|
+
view.content_tag :strong, first_name
|
50
|
+
end
|
42
51
|
end
|
43
52
|
|
44
53
|
class ActionView::Base
|
@@ -51,6 +60,7 @@ describe Bourgeois::Presenter do
|
|
51
60
|
end
|
52
61
|
|
53
62
|
it { expect(presenter.local_name).to eql 'Fancy translated string from users.attributes.local_name' }
|
63
|
+
it { expect(presenter.first_name_in_bold).to eql '<strong>Patrick</strong>' }
|
54
64
|
end
|
55
65
|
|
56
66
|
context 'with blank view' do
|
@@ -65,6 +75,20 @@ describe Bourgeois::Presenter do
|
|
65
75
|
end
|
66
76
|
end
|
67
77
|
|
78
|
+
describe :object do
|
79
|
+
before do
|
80
|
+
class UserPresenter < Bourgeois::Presenter
|
81
|
+
def birthdate
|
82
|
+
object.birthdate.gsub(/-/, '/')
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class User < OpenStruct; end
|
87
|
+
end
|
88
|
+
|
89
|
+
it { expect(presenter.birthdate).to eql '1962/06/16' }
|
90
|
+
end
|
91
|
+
|
68
92
|
describe :inspect do
|
69
93
|
before do
|
70
94
|
class UserPresenter < Bourgeois::Presenter; end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Bourgeois::ViewHelper do
|
4
|
+
describe :present do
|
5
|
+
let(:view) { ActionView::Base.new }
|
6
|
+
|
7
|
+
before do
|
8
|
+
class UserPresenter < Bourgeois::Presenter
|
9
|
+
def formatted_name
|
10
|
+
"#{first_name} #{last_name}".strip
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class User < OpenStruct; end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'on a single resource' do
|
18
|
+
let(:user) { User.new first_name: 'Patrick', last_name: 'Bourgeois' }
|
19
|
+
|
20
|
+
context 'without a block' do
|
21
|
+
it { expect(view.present(user).formatted_name).to eql 'Patrick Bourgeois' }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'with a block' do
|
25
|
+
specify do
|
26
|
+
view.present(user) do |u|
|
27
|
+
expect(u.formatted_name).to eql 'Patrick Bourgeois'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'on a collection of resources' do
|
34
|
+
let(:user1) { User.new first_name: 'Patrick', last_name: 'Bourgeois' }
|
35
|
+
let(:user2) { User.new first_name: 'Francois', last_name: 'Jean' }
|
36
|
+
let(:user3) { User.new first_name: 'Alain', last_name: 'Lapointe' }
|
37
|
+
let(:users) { [user1, user2, user3] }
|
38
|
+
|
39
|
+
specify do
|
40
|
+
output = []
|
41
|
+
view.present(users) { |u| output << u.formatted_name }
|
42
|
+
|
43
|
+
expect(output).to eql ['Patrick Bourgeois', 'Francois Jean', 'Alain Lapointe']
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'on a resource without a defined presenter class' do
|
48
|
+
before do
|
49
|
+
class Project < OpenStruct; end
|
50
|
+
end
|
51
|
+
|
52
|
+
let(:project) { Project.new name: 'Les B.B.' }
|
53
|
+
it { expect { view.present(project) }.to raise_error(Bourgeois::UnknownPresenter, 'unknown presenter class ProjectPresenter') }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bourgeois
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-08-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -107,12 +107,16 @@ files:
|
|
107
107
|
- README.md
|
108
108
|
- Rakefile
|
109
109
|
- bourgeois.gemspec
|
110
|
+
- gemfiles/Gemfile.activemodel-3.2.x
|
111
|
+
- gemfiles/Gemfile.activemodel-4.0
|
110
112
|
- lib/bourgeois.rb
|
113
|
+
- lib/bourgeois/errors.rb
|
111
114
|
- lib/bourgeois/presenter.rb
|
112
115
|
- lib/bourgeois/railtie.rb
|
113
116
|
- lib/bourgeois/version.rb
|
114
117
|
- lib/bourgeois/view_helper.rb
|
115
118
|
- spec/bourgeois/presenter_spec.rb
|
119
|
+
- spec/bourgeois/view_helper_spec.rb
|
116
120
|
- spec/spec_helper.rb
|
117
121
|
homepage: https://github.com/mirego/bourgeois
|
118
122
|
licenses:
|
@@ -129,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
129
133
|
version: '0'
|
130
134
|
segments:
|
131
135
|
- 0
|
132
|
-
hash:
|
136
|
+
hash: 1321347131962644702
|
133
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
138
|
none: false
|
135
139
|
requirements:
|
@@ -138,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
142
|
version: '0'
|
139
143
|
segments:
|
140
144
|
- 0
|
141
|
-
hash:
|
145
|
+
hash: 1321347131962644702
|
142
146
|
requirements: []
|
143
147
|
rubyforge_project:
|
144
148
|
rubygems_version: 1.8.23
|
@@ -147,4 +151,5 @@ specification_version: 3
|
|
147
151
|
summary: Bourgeois is a Ruby library that makes using presenters a very simple thing.
|
148
152
|
test_files:
|
149
153
|
- spec/bourgeois/presenter_spec.rb
|
154
|
+
- spec/bourgeois/view_helper_spec.rb
|
150
155
|
- spec/spec_helper.rb
|