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 CHANGED
@@ -4,4 +4,8 @@ rvm:
4
4
  - 2.0.0
5
5
  - 1.9.3
6
6
 
7
+ gemfile:
8
+ - gemfiles/Gemfile.activemodel-4.0
9
+ - gemfiles/Gemfile.activemodel-3.2.x
10
+
7
11
  script: "echo 'DO IT' && bundle exec rake spec"
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
 
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'activemodel', '~> 3.2.0'
6
+ gem 'actionpack', '~> 3.2.0'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'activemodel', '~> 4.0.0'
6
+ gem 'actionpack', '~> 4.0.0'
@@ -0,0 +1,11 @@
1
+ module Bourgeois
2
+ class UnknownPresenter < StandardError
3
+ def initialize(klass)
4
+ @klass = klass
5
+ end
6
+
7
+ def to_s
8
+ "unknown presenter class #{@klass}"
9
+ end
10
+ end
11
+ end
@@ -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
@@ -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 do
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
@@ -1,3 +1,3 @@
1
1
  module Bourgeois
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  module Bourgeois
2
2
  module ViewHelper
3
- # Wrap a resource or a collection into its related delegator
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 ||= "#{object.class}Presenter".constantize
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
@@ -4,5 +4,5 @@ require 'ostruct'
4
4
  require 'rspec'
5
5
  require 'bourgeois'
6
6
 
7
- RSpec.configure do |config|
8
- end
7
+ # Inject our helper into ActionView
8
+ ActionView::Base.class_eval(&Bourgeois.inject_into_action_view)
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.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-07-25 00:00:00.000000000 Z
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: -4422150060977082943
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: -4422150060977082943
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