action_presenter 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -1
- data/README.md +53 -3
- data/lib/action_presenter.rb +0 -2
- data/lib/action_presenter/defaults/timestamps.rb +6 -6
- data/lib/action_presenter/version.rb +1 -1
- data/lib/generators/action_presenter/install/install_generator.rb +21 -0
- data/spec/action_presenter/defaults/timestamps_spec.rb +25 -11
- metadata +14 -13
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -7,14 +7,22 @@ Use presenter pattern in Rails application without changing controllers.
|
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
10
|
+
**Add `action_presenter` to Gemfile**
|
11
|
+
|
10
12
|
```ruby
|
11
13
|
gem 'action_presenter'
|
14
|
+
# or
|
15
|
+
gem 'action_presenter', git: 'git@github.com:zlw/action_presenter.git'
|
12
16
|
```
|
13
17
|
|
14
|
-
|
18
|
+
**and than install**
|
15
19
|
|
16
|
-
```
|
17
|
-
|
20
|
+
```bash
|
21
|
+
# for RSpec
|
22
|
+
rails generate action_presenter:intall -t rspec
|
23
|
+
|
24
|
+
# for Test::Unit
|
25
|
+
rails generate action_presenter:install -t test_unit
|
18
26
|
```
|
19
27
|
|
20
28
|
Gem was tested under Ruby 1.9.2 and 1.9.3, Rails 3.2.1
|
@@ -72,6 +80,48 @@ There're some default presenter methods. They're generated if object respond to
|
|
72
80
|
= p.updated_at #=> "February 17, 2012 13:27"
|
73
81
|
```
|
74
82
|
|
83
|
+
It's also possible to change timestamps format
|
84
|
+
|
85
|
+
```haml
|
86
|
+
- present @article do |p|
|
87
|
+
= p.created_at :short
|
88
|
+
= p.updated_at :long
|
89
|
+
```
|
90
|
+
|
91
|
+
## Testing
|
92
|
+
|
93
|
+
### RSpec
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
require 'spec_helper'
|
97
|
+
|
98
|
+
describe ArticlePresenter do
|
99
|
+
include ActionView::TestCase::Behavior
|
100
|
+
|
101
|
+
let(:article) { mock_model(Article) }
|
102
|
+
let(:presenter) { ArticlePresenter.new(article, view) }
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
You can change RSpec configuration to include `ActionView::TestCase::Behavior` in all specs in `/spec/presenters` folder
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
RSpec.configure do |config|
|
110
|
+
# a lot of code here ...
|
111
|
+
|
112
|
+
config.include ActionView::TestCase::Behavior, example_group: { file_path: %r{spec/presenters} }
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
### Test::Unit
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
require 'test_helper'
|
120
|
+
|
121
|
+
class TestArticlePresenter < ActionView::TestCase
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
75
125
|
## Maintainers
|
76
126
|
|
77
127
|
* Krzysztof Zalewski (https://github.com/zlw, http://kzalewski.blogspot.com)
|
data/lib/action_presenter.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
module ActionPresenter
|
2
2
|
module Defaults
|
3
3
|
module Timestamps
|
4
|
-
def created_at
|
5
|
-
localize_time :created_at
|
4
|
+
def created_at(format = :default)
|
5
|
+
localize_time :created_at, format
|
6
6
|
end
|
7
7
|
|
8
|
-
def updated_at
|
9
|
-
localize_time :updated_at
|
8
|
+
def updated_at(format = :default)
|
9
|
+
localize_time :updated_at, format
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
13
13
|
|
14
|
-
def localize_time(time)
|
15
|
-
I18n.l(object.try(time)) if object.respond_to?(time)
|
14
|
+
def localize_time(time, format)
|
15
|
+
I18n.l(object.try(time), format: format) if object.respond_to?(time)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActionPresenter
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
desc <<-DESC
|
4
|
+
Description:
|
5
|
+
Generate application and test/spec presenter folders
|
6
|
+
DESC
|
7
|
+
|
8
|
+
class_option 'test-framework', type: :string, default: :rspec, aliases: '-t', desc: 'Test framework (RSpec or Test::Unit)'
|
9
|
+
|
10
|
+
def build_presenters_folder
|
11
|
+
empty_directory 'app/presenters'
|
12
|
+
|
13
|
+
case options['test-framework']
|
14
|
+
when 'rspec'
|
15
|
+
empty_directory 'spec/presenters'
|
16
|
+
when 'test_unit'
|
17
|
+
empty_directory 'test/unit/presenters'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -5,41 +5,55 @@ require 'support/dummy_action_controller'
|
|
5
5
|
describe ActionPresenter::Defaults::Timestamps do
|
6
6
|
include ActionControllerHelper
|
7
7
|
|
8
|
-
let(:
|
8
|
+
let(:time_now) { Time.now }
|
9
|
+
let(:time) { 'Fri, 17 Feb 2012 01:46:08 +0100' }
|
9
10
|
|
10
|
-
let(:object) { mock(Object, created_at:
|
11
|
+
let(:object) { mock(Object, created_at: time_now, updated_at: time_now) }
|
11
12
|
let(:template) { mock(Object) }
|
12
13
|
|
13
14
|
let(:klass) { DummyPresenter.new(object, template) }
|
14
15
|
|
15
16
|
it 'should return localized #created_at' do
|
16
|
-
I18n.should_receive(:l).and_return
|
17
|
-
klass.created_at.should ==
|
17
|
+
I18n.should_receive(:l).and_return time
|
18
|
+
klass.created_at.should == time
|
18
19
|
end
|
19
20
|
|
20
21
|
it 'should return localized #updated_at' do
|
21
|
-
I18n.should_receive(:l).and_return
|
22
|
-
klass.updated_at.should ==
|
22
|
+
I18n.should_receive(:l).and_return time
|
23
|
+
klass.updated_at.should == time
|
23
24
|
end
|
24
25
|
|
25
26
|
context 'in view context' do
|
26
27
|
let(:object) { 'foobar' }
|
27
|
-
before { I18n.should_receive(:l).and_return
|
28
|
+
before { I18n.should_receive(:l).and_return time }
|
28
29
|
|
29
30
|
it 'should return #created_at' do
|
30
|
-
object.should_receive(:created_at).and_return
|
31
|
+
object.should_receive(:created_at).and_return time
|
31
32
|
|
32
33
|
helper.present(object) do |p|
|
33
34
|
p.created_at
|
34
|
-
end.should ==
|
35
|
+
end.should == time
|
35
36
|
end
|
36
37
|
|
37
38
|
it 'should return #updated_at' do
|
38
|
-
object.should_receive(:updated_at).and_return
|
39
|
+
object.should_receive(:updated_at).and_return time
|
39
40
|
|
40
41
|
helper.present(object) do |p|
|
41
42
|
p.updated_at
|
42
|
-
end.should ==
|
43
|
+
end.should == time
|
43
44
|
end
|
44
45
|
end
|
46
|
+
|
47
|
+
context "#format" do
|
48
|
+
it 'should return short localized date' do
|
49
|
+
I18n.should_receive(:l).with(time_now, {format: :short}).and_return time
|
50
|
+
klass.created_at(:short).should == time
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should return long localized date' do
|
54
|
+
I18n.should_receive(:l).with(time_now, {format: :long}).and_return time
|
55
|
+
klass.created_at(:long).should == time
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
45
59
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_presenter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|
16
|
-
requirement: &
|
16
|
+
requirement: &70165903634720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70165903634720
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: named_accessors
|
27
|
-
requirement: &
|
27
|
+
requirement: &70165903634300 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70165903634300
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70165903633780 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70165903633780
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70165903633320 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70165903633320
|
58
58
|
description: Missing link between models and views. Use presenter pattern in Rails
|
59
59
|
application without changing controllers.
|
60
60
|
email:
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- lib/action_presenter/railtie.rb
|
77
77
|
- lib/action_presenter/version.rb
|
78
78
|
- lib/action_presenter/view_helper.rb
|
79
|
+
- lib/generators/action_presenter/install/install_generator.rb
|
79
80
|
- spec/action_presenter/base_spec.rb
|
80
81
|
- spec/action_presenter/defaults/timestamps_spec.rb
|
81
82
|
- spec/action_presenter/view_helper_spec.rb
|
@@ -97,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
98
|
version: '0'
|
98
99
|
segments:
|
99
100
|
- 0
|
100
|
-
hash: -
|
101
|
+
hash: -4165987321406038830
|
101
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
103
|
none: false
|
103
104
|
requirements:
|
@@ -106,10 +107,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
107
|
version: '0'
|
107
108
|
segments:
|
108
109
|
- 0
|
109
|
-
hash: -
|
110
|
+
hash: -4165987321406038830
|
110
111
|
requirements: []
|
111
112
|
rubyforge_project: action_presenter
|
112
|
-
rubygems_version: 1.8.
|
113
|
+
rubygems_version: 1.8.16
|
113
114
|
signing_key:
|
114
115
|
specification_version: 3
|
115
116
|
summary: Missing link between models and views. Use presenter pattern in Rails application
|