resubject 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - "1.9.3"
3
+ - 1.9.3
4
+ - 2.0.0
5
+ env:
6
+ - rails=3.2
7
+ - rails=4.0
@@ -1,3 +1,8 @@
1
+ # 0.2.0
2
+
3
+ - enhancements
4
+ - Introduce RSpec helpers
5
+
1
6
  # 0.1.1
2
7
 
3
8
  - bugfixes
data/Gemfile CHANGED
@@ -1,6 +1,17 @@
1
- source 'https://rubygems.org'
1
+ ENV['rails'] ||= '3.2'
2
+
3
+ source :rubygems
4
+
5
+ rails_version = ENV['rails']
6
+
7
+ case rails_version
8
+ when '3.2'
9
+ gem 'actionpack', "~> #{rails_version}"
10
+ when '4.0'
11
+ # Waiting for beta release of actionpack
12
+ gem 'rails', github: 'rails/rails'
13
+ end
2
14
 
3
- gem 'actionpack', '~> 3.2'
4
15
  gem 'redcarpet'
5
16
 
6
17
  # Specify your gem's dependencies in resubject.gemspec
data/README.md CHANGED
@@ -10,13 +10,17 @@ Uber simple presenters using Ruby's SimpleDelegator.
10
10
  Add this line to your application's Gemfile:
11
11
 
12
12
  ```ruby
13
- gem 'resubject', '~> 0.1.1'
13
+ gem 'resubject', '~> 0.2.0'
14
14
  ```
15
15
 
16
16
  And then execute:
17
17
 
18
18
  $ bundle
19
19
 
20
+ ## Ruby/Rails versions
21
+
22
+ This gem is compatible with Ruby 1.9 and 2.0 and Rails 3 and 4.
23
+
20
24
  ## Documentation
21
25
 
22
26
  Checkout the documentation in [rdoc.info/resubject](http://rdoc.info/github/felipeelias/resubject/master/frames)
@@ -194,7 +198,45 @@ time_ago time_ago_in_words ActionView::Helpers::DateHelper
194
198
  date_format to_s ActiveSupport::TimeWithZone
195
199
  ```
196
200
 
197
- More helpers will be added. Feel free to contribute with yours! Also, Check out [the extensions file](https://github.com/felipeelias/resubject/blob/master/lib/resubject/rails/extensions.rb).
201
+ More helpers will be added. Feel free to contribute with yours! Also, Check out [the extensions file](https://github.com/felipeelias/resubject/blob/master/lib/resubject/extensions/template_methods.rb).
202
+
203
+ ### Helpers without rails
204
+
205
+ If you'd like to use the generated helpers but you're not using Rails, you can either use `ActionView` or create your own template handler:
206
+
207
+ ```
208
+ require 'action_view'
209
+
210
+ post = PostPresenter.new(post, ActionView::Base.new)
211
+ # Or
212
+ post = PostPresenter.new(post, MyTemplateHandler.new)
213
+ ```
214
+
215
+ If you want to use your own template handler and still use Resubject helpers, you may want to define the same `ActionView` helpers in your handler (or only the ones you will actually use).
216
+
217
+ ## Testing
218
+
219
+ Resubject introduce a new context for RSpec that helps testing your presenters:
220
+
221
+ ```ruby
222
+ # spec/presenters/my_presenter_spec.rb
223
+ # require 'action_view' # require this first if you want to test action view helpers
224
+ require 'resubject/rspec'
225
+ require 'presenters/my_presenter'
226
+
227
+ describe UserPresenter do
228
+ let(:object) { mock :user }
229
+
230
+ it 'has full name' do
231
+ object.stub(first: 'User', last: 'Name')
232
+ expect(subject.name).to eq 'User Name'
233
+ end
234
+ end
235
+ ```
236
+
237
+ By placing the file into `spec/presenters`, Resubject automatically includes the `subject` and `template` variables into your spec, so you don't need to define them on every spec.
238
+
239
+ **NOTE:** Please note that the presenter is tested on isolation. It's not required but very recommended.
198
240
 
199
241
  ## Maintainers
200
242
 
@@ -1,5 +1,6 @@
1
1
  require 'resubject/version'
2
2
  require 'resubject/naming'
3
+ require 'resubject/extensions'
3
4
  require 'resubject/presenter'
4
5
  require 'resubject/builder'
5
6
  require 'resubject/rails' if defined? Rails
@@ -0,0 +1 @@
1
+ require 'resubject/extensions/template_methods'
@@ -1,7 +1,9 @@
1
+ require 'active_support/time'
2
+
1
3
  module Resubject
2
- module Rails
4
+ module Extensions
3
5
  # All Rails extensions
4
- module Extensions
6
+ module TemplateMethods
5
7
  # Generates an attribute using `number_to_currency` helper from rails
6
8
  #
7
9
  # @example
@@ -2,6 +2,8 @@ require 'delegate'
2
2
 
3
3
  module Resubject
4
4
  class Presenter < SimpleDelegator
5
+ extend Resubject::Extensions::TemplateMethods
6
+
5
7
  # the HTML helpers context
6
8
  attr_reader :context
7
9
  alias_method :template, :context
@@ -81,5 +83,25 @@ module Resubject
81
83
  present to_model.send(attribute), *presenters
82
84
  end
83
85
  end
86
+
87
+ private
88
+
89
+ def translate(*args, &block)
90
+ context.t(*args, &block)
91
+ end
92
+
93
+ alias_method :t, :translate
94
+
95
+ def localize(*args, &block)
96
+ context.l(*args, &block)
97
+ end
98
+
99
+ alias_method :l, :localize
100
+
101
+ def helpers
102
+ context
103
+ end
104
+
105
+ alias_method :h, :helpers
84
106
  end
85
107
  end
@@ -1,34 +1,13 @@
1
1
  require 'resubject/rails/engine'
2
- require 'resubject/rails/extensions'
3
2
 
4
3
  module Resubject
5
4
  class Presenter
6
- extend Resubject::Rails::Extensions
7
-
8
5
  private
9
6
 
10
- def translate(*args, &block)
11
- context.t(*args, &block)
12
- end
13
-
14
- alias_method :t, :translate
15
-
16
- def localize(*args, &block)
17
- context.l(*args, &block)
18
- end
19
-
20
- alias_method :l, :localize
21
-
22
7
  def routes
23
8
  ::Rails.application.routes.url_helpers
24
9
  end
25
10
 
26
11
  alias_method :r, :routes
27
-
28
- def helpers
29
- context
30
- end
31
-
32
- alias_method :h, :helpers
33
12
  end
34
13
  end
@@ -0,0 +1,52 @@
1
+ require 'resubject'
2
+
3
+ module Resubject
4
+ # RSpec configuration and helpers
5
+ #
6
+ # This helper automatically creates `subject` and `template`
7
+ # variables for RSpec when testing your presenter.
8
+ #
9
+ # To get this working, create a spec file in `spec/presenters`
10
+ # and require `resubject/rspec`. You only need to define the
11
+ # object under test by creating a `let(:object)`, like so:
12
+ #
13
+ # @example Isolated spec for MyPresenter
14
+ #
15
+ # # spec/presenters/my_presenter_spec.rb
16
+ # require 'resubject/rspec'
17
+ # require 'presenters/my_presenter'
18
+ #
19
+ # describe MyPresenter do
20
+ # let(:object) { mock :presented }
21
+ #
22
+ # it 'has full name' do
23
+ # object.stub(first: 'My', last: 'Name')
24
+ # expect(subject.name).to eq 'My Name'
25
+ # end
26
+ # end
27
+ #
28
+ module Rspec
29
+ # Extend RSpec configuration for files placed in `spec/presenters`
30
+ def self.included(base)
31
+ base.instance_eval do
32
+ let(:template) do
33
+ if defined? ActionView
34
+ ActionView::Base.new
35
+ else
36
+ mock :template
37
+ end
38
+ end
39
+
40
+ subject do
41
+ described_class.new(object, template)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ ::RSpec.configure do |c|
49
+ c.include Resubject::Rspec,
50
+ type: :presenter,
51
+ example_group: { file_path: %r(spec/presenters) }
52
+ end
@@ -1,3 +1,3 @@
1
1
  module Resubject
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_dependency 'activesupport', '~> 3.2'
21
+ gem.add_dependency 'activesupport', '>= 3.2'
22
22
 
23
23
  gem.add_development_dependency "rake"
24
24
  gem.add_development_dependency "rspec", "~> 2.12.0"
@@ -1,14 +1,11 @@
1
1
  require 'spec_helper'
2
2
  require 'action_view'
3
- require 'resubject/rails/extensions'
4
3
 
5
- describe Resubject::Presenter, 'extensions' do
4
+ describe Resubject::Presenter, 'template methods' do
6
5
  let(:model) { mock :model }
7
6
 
8
7
  let(:presenter) do
9
8
  Class.new(Resubject::Presenter) do
10
- extend Resubject::Rails::Extensions
11
-
12
9
  currency :price
13
10
  time_ago :posted_at
14
11
  percentage :rating, precision: 0
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resubject
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
4
  prerelease:
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Felipe Elias Philipp
@@ -10,72 +10,72 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-16 00:00:00.000000000 Z
13
+ date: 2013-01-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
17
- prerelease: false
18
- requirement: !ruby/object:Gem::Requirement
17
+ version_requirements: !ruby/object:Gem::Requirement
18
+ none: false
19
19
  requirements:
20
- - - ~>
20
+ - - ! '>='
21
21
  - !ruby/object:Gem::Version
22
22
  version: '3.2'
23
+ requirement: !ruby/object:Gem::Requirement
23
24
  none: false
24
- type: :runtime
25
- version_requirements: !ruby/object:Gem::Requirement
26
25
  requirements:
27
- - - ~>
26
+ - - ! '>='
28
27
  - !ruby/object:Gem::Version
29
28
  version: '3.2'
30
- none: false
29
+ prerelease: false
30
+ type: :runtime
31
31
  - !ruby/object:Gem::Dependency
32
32
  name: rake
33
- prerelease: false
34
- requirement: !ruby/object:Gem::Requirement
33
+ version_requirements: !ruby/object:Gem::Requirement
34
+ none: false
35
35
  requirements:
36
36
  - - ! '>='
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
+ requirement: !ruby/object:Gem::Requirement
39
40
  none: false
40
- type: :development
41
- version_requirements: !ruby/object:Gem::Requirement
42
41
  requirements:
43
42
  - - ! '>='
44
43
  - !ruby/object:Gem::Version
45
44
  version: '0'
46
- none: false
45
+ prerelease: false
46
+ type: :development
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- prerelease: false
50
- requirement: !ruby/object:Gem::Requirement
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ none: false
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: 2.12.0
55
+ requirement: !ruby/object:Gem::Requirement
55
56
  none: false
56
- type: :development
57
- version_requirements: !ruby/object:Gem::Requirement
58
57
  requirements:
59
58
  - - ~>
60
59
  - !ruby/object:Gem::Version
61
60
  version: 2.12.0
62
- none: false
61
+ prerelease: false
62
+ type: :development
63
63
  - !ruby/object:Gem::Dependency
64
64
  name: yard
65
- prerelease: false
66
- requirement: !ruby/object:Gem::Requirement
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ none: false
67
67
  requirements:
68
68
  - - ~>
69
69
  - !ruby/object:Gem::Version
70
70
  version: 0.8.3
71
+ requirement: !ruby/object:Gem::Requirement
71
72
  none: false
72
- type: :development
73
- version_requirements: !ruby/object:Gem::Requirement
74
73
  requirements:
75
74
  - - ~>
76
75
  - !ruby/object:Gem::Version
77
76
  version: 0.8.3
78
- none: false
77
+ prerelease: false
78
+ type: :development
79
79
  description: Uber simple presenters
80
80
  email:
81
81
  - felipe@applicake.com
@@ -96,18 +96,20 @@ files:
96
96
  - Rakefile
97
97
  - lib/resubject.rb
98
98
  - lib/resubject/builder.rb
99
+ - lib/resubject/extensions.rb
100
+ - lib/resubject/extensions/template_methods.rb
99
101
  - lib/resubject/naming.rb
100
102
  - lib/resubject/presenter.rb
101
103
  - lib/resubject/rails.rb
102
104
  - lib/resubject/rails/engine.rb
103
- - lib/resubject/rails/extensions.rb
104
105
  - lib/resubject/rails/helpers.rb
106
+ - lib/resubject/rspec.rb
105
107
  - lib/resubject/version.rb
106
108
  - resubject.gemspec
107
109
  - spec/resubject/builder_spec.rb
110
+ - spec/resubject/extensions/template_methods_spec.rb
108
111
  - spec/resubject/naming_spec.rb
109
112
  - spec/resubject/presenter_spec.rb
110
- - spec/resubject/rails/extensions_spec.rb
111
113
  - spec/spec_helper.rb
112
114
  homepage: https://github.com/felipeelias/resubject
113
115
  licenses:
@@ -117,17 +119,23 @@ rdoc_options: []
117
119
  require_paths:
118
120
  - lib
119
121
  required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
120
123
  requirements:
121
124
  - - ! '>='
122
125
  - !ruby/object:Gem::Version
123
126
  version: '0'
124
- none: false
127
+ hash: -249672256597893339
128
+ segments:
129
+ - 0
125
130
  required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
126
132
  requirements:
127
133
  - - ! '>='
128
134
  - !ruby/object:Gem::Version
129
135
  version: '0'
130
- none: false
136
+ hash: -249672256597893339
137
+ segments:
138
+ - 0
131
139
  requirements: []
132
140
  rubyforge_project:
133
141
  rubygems_version: 1.8.24
@@ -136,8 +144,8 @@ specification_version: 3
136
144
  summary: Uber simple presenters using Ruby's SimpleDelegator
137
145
  test_files:
138
146
  - spec/resubject/builder_spec.rb
147
+ - spec/resubject/extensions/template_methods_spec.rb
139
148
  - spec/resubject/naming_spec.rb
140
149
  - spec/resubject/presenter_spec.rb
141
- - spec/resubject/rails/extensions_spec.rb
142
150
  - spec/spec_helper.rb
143
151
  has_rdoc: