action_presenter 1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ # - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ # - jruby-18mode # JRuby in 1.8 mode
7
+ # - jruby-19mode # JRuby in 1.9 mode
8
+ # - rbx-18mode
9
+ # - rbx-19mode # currently in active development, may or may not work for your project
10
+ # uncomment this line if your project needs to run something other than `rake`:
11
+ # script: bundle exec rspec spec
@@ -1,3 +1,7 @@
1
1
  ## Version 1.0
2
2
 
3
3
  * initial release
4
+
5
+ ## Version 1.0.1
6
+
7
+ * add default presenter methods for `created_at` and `updated_at`
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ## Description
1
+ ## ActionPresenter [![Build Status](https://secure.travis-ci.org/zlw/action_presenter.png?branch=master)](http://travis-ci.org/zlw/action_presenter)
2
2
 
3
3
  Missing link between models and views.
4
4
  Use presenter pattern in Rails application without changing controllers.
@@ -17,7 +17,7 @@ or
17
17
  gem 'action_presenter', git: 'git@github.com:zlw/action_presenter.git'
18
18
  ```
19
19
 
20
- Gem was tested under 1.9.3 and Rails 3.2.1
20
+ Gem was tested under Ruby 1.9.2 and 1.9.3, Rails 3.2.1
21
21
 
22
22
  ## Usage
23
23
 
@@ -59,6 +59,23 @@ If You want to change presenter class pass it as second argument
59
59
 
60
60
  All that without any change in any model or controller
61
61
 
62
+ ## Defaults
63
+
64
+ There're some default presenter methods. They're generated if object respond to those methods
65
+
66
+ * `created_at` - localized date of create
67
+ * `updated_at` - localized date of last update
68
+
69
+ ```haml
70
+ - present @article do |p|
71
+ = p.created_at #=> "February 17, 2012 12:30"
72
+ = p.updated_at #=> "February 17, 2012 13:27"
73
+ ```
74
+
75
+ ## Maintainers
76
+
77
+ * Krzysztof Zalewski (https://github.com/zlw, http://kzalewski.blogspot.com)
78
+
62
79
  ## License
63
80
 
64
81
  Copyright (C) 2012 Krzysztof Zalewski
@@ -1,14 +1,24 @@
1
+ ## 3rd party libs
1
2
  require 'active_support/core_ext/string'
2
3
  require 'active_support/concern'
3
4
 
4
5
  require 'named_accessors'
5
6
 
7
+ ## Version
6
8
  require 'action_presenter/version'
9
+
10
+ ## Default fields
11
+ require 'action_presenter/defaults/timestamps'
12
+
13
+ ## Base presenter class and view helpers
7
14
  require 'action_presenter/base'
8
15
  require 'action_presenter/view_helper'
9
16
 
17
+ ## Railtie
10
18
  require 'action_presenter/railtie' if defined? Rails
11
19
 
12
20
 
21
+
22
+
13
23
  module ActionPresenter
14
24
  end
@@ -1,5 +1,7 @@
1
1
  module ActionPresenter
2
2
  class Base
3
+ include Defaults::Timestamps
4
+
3
5
  def initialize(object, template)
4
6
  @object, @template = object, template
5
7
  end
@@ -7,6 +9,7 @@ module ActionPresenter
7
9
  private
8
10
 
9
11
  named_reader :template, as: :h
12
+ attr_reader :object
10
13
 
11
14
  def self.presents(name)
12
15
  named_reader :object, as: name
@@ -0,0 +1,19 @@
1
+ module ActionPresenter
2
+ module Defaults
3
+ module Timestamps
4
+ def created_at
5
+ localize_time :created_at
6
+ end
7
+
8
+ def updated_at
9
+ localize_time :updated_at
10
+ end
11
+
12
+ private
13
+
14
+ def localize_time(time)
15
+ I18n.l(object.try(time)) if object.respond_to?(time)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module ActionPresenter
2
- VERSION = "1.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require 'support/base'
2
+ require 'support/dummy_presenter'
3
3
 
4
4
 
5
5
  describe ActionPresenter::Base do
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+ require 'support/dummy_presenter'
3
+ require 'support/dummy_action_controller'
4
+
5
+ describe ActionPresenter::Defaults::Timestamps do
6
+ include ActionControllerHelper
7
+
8
+ let(:time) { Time.now }
9
+
10
+ let(:object) { mock(Object, created_at: time, updated_at: time) }
11
+ let(:template) { mock(Object) }
12
+
13
+ let(:klass) { DummyPresenter.new(object, template) }
14
+
15
+ it 'should return localized #created_at' do
16
+ I18n.should_receive(:l).and_return("Fri, 17 Feb 2012 01:46:08 +0100")
17
+ klass.created_at.should == "Fri, 17 Feb 2012 01:46:08 +0100"
18
+ end
19
+
20
+ it 'should return localized #updated_at' do
21
+ I18n.should_receive(:l).and_return("Fri, 17 Feb 2012 01:46:08 +0100")
22
+ klass.updated_at.should == "Fri, 17 Feb 2012 01:46:08 +0100"
23
+ end
24
+
25
+ context 'in view context' do
26
+ let(:object) { 'foobar' }
27
+ before { I18n.should_receive(:l).and_return 'Fri, 17 Feb 2012 01:46:08 +0100' }
28
+
29
+ it 'should return #created_at' do
30
+ object.should_receive(:created_at).and_return '2012-02-17 01:46:08 +0100'
31
+
32
+ helper.present(object) do |p|
33
+ p.created_at
34
+ end.should == 'Fri, 17 Feb 2012 01:46:08 +0100'
35
+ end
36
+
37
+ it 'should return #updated_at' do
38
+ object.should_receive(:updated_at).and_return '2012-02-17 01:46:08 +0100'
39
+
40
+ helper.present(object) do |p|
41
+ p.updated_at
42
+ end.should == 'Fri, 17 Feb 2012 01:46:08 +0100'
43
+ end
44
+ end
45
+ end
@@ -1,9 +1,10 @@
1
1
  require 'spec_helper'
2
- require 'support/view_helper'
2
+ require 'support/string_presenter'
3
+ require 'support/dummy_action_controller'
3
4
 
4
5
 
5
6
  describe ActionPresenter::ViewHelper do
6
- let(:helper) { DummyActionController.send(:include, ActionPresenter::ViewHelper).new }
7
+ include ActionControllerHelper
7
8
 
8
9
  it 'should call default presenter if presenter class is not given' do
9
10
  helper.present('foobar') do |p|
@@ -0,0 +1,17 @@
1
+ class DummyActionController
2
+ def self.helper_method(name)
3
+ nil
4
+ end
5
+
6
+ def view_context
7
+ self
8
+ end
9
+ end
10
+
11
+ module ActionControllerHelper
12
+ extend ActiveSupport::Concern
13
+
14
+ included do
15
+ let(:helper) { DummyActionController.send(:include, ActionPresenter::ViewHelper).new }
16
+ end
17
+ end
@@ -1,13 +1,3 @@
1
- class DummyActionController
2
- def self.helper_method(name)
3
- nil
4
- end
5
-
6
- def view_context
7
- self
8
- end
9
- end
10
-
11
1
  class StringPresenter < ActionPresenter::Base
12
2
  def foobar
13
3
  'foo'
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.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
16
- requirement: &70319726257920 !ruby/object:Gem::Requirement
16
+ requirement: &70145687806220 !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: *70319726257920
24
+ version_requirements: *70145687806220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: named_accessors
27
- requirement: &70319726257500 !ruby/object:Gem::Requirement
27
+ requirement: &70145687805800 !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: *70319726257500
35
+ version_requirements: *70145687805800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70319726257080 !ruby/object:Gem::Requirement
38
+ requirement: &70145687805380 !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: *70319726257080
46
+ version_requirements: *70145687805380
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70319726256660 !ruby/object:Gem::Requirement
49
+ requirement: &70145687804920 !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: *70319726256660
57
+ version_requirements: *70145687804920
58
58
  description: Missing link between models and views. Use presenter pattern in Rails
59
59
  application without changing controllers.
60
60
  email:
@@ -64,6 +64,7 @@ extensions: []
64
64
  extra_rdoc_files: []
65
65
  files:
66
66
  - .gitignore
67
+ - .travis.yml
67
68
  - CHANGELOG.md
68
69
  - Gemfile
69
70
  - README.md
@@ -71,14 +72,17 @@ files:
71
72
  - action_presenter.gemspec
72
73
  - lib/action_presenter.rb
73
74
  - lib/action_presenter/base.rb
75
+ - lib/action_presenter/defaults/timestamps.rb
74
76
  - lib/action_presenter/railtie.rb
75
77
  - lib/action_presenter/version.rb
76
78
  - lib/action_presenter/view_helper.rb
77
79
  - spec/action_presenter/base_spec.rb
80
+ - spec/action_presenter/defaults/timestamps_spec.rb
78
81
  - spec/action_presenter/view_helper_spec.rb
79
82
  - spec/spec_helper.rb
80
- - spec/support/base.rb
81
- - spec/support/view_helper.rb
83
+ - spec/support/dummy_action_controller.rb
84
+ - spec/support/dummy_presenter.rb
85
+ - spec/support/string_presenter.rb
82
86
  homepage: https://github.com/zlw/action_presenter
83
87
  licenses: []
84
88
  post_install_message:
@@ -93,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
97
  version: '0'
94
98
  segments:
95
99
  - 0
96
- hash: 1357576686972883001
100
+ hash: -2937985063345046777
97
101
  required_rubygems_version: !ruby/object:Gem::Requirement
98
102
  none: false
99
103
  requirements:
@@ -102,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
106
  version: '0'
103
107
  segments:
104
108
  - 0
105
- hash: 1357576686972883001
109
+ hash: -2937985063345046777
106
110
  requirements: []
107
111
  rubyforge_project: action_presenter
108
112
  rubygems_version: 1.8.15
@@ -112,7 +116,9 @@ summary: Missing link between models and views. Use presenter pattern in Rails a
112
116
  without changing controllers.
113
117
  test_files:
114
118
  - spec/action_presenter/base_spec.rb
119
+ - spec/action_presenter/defaults/timestamps_spec.rb
115
120
  - spec/action_presenter/view_helper_spec.rb
116
121
  - spec/spec_helper.rb
117
- - spec/support/base.rb
118
- - spec/support/view_helper.rb
122
+ - spec/support/dummy_action_controller.rb
123
+ - spec/support/dummy_presenter.rb
124
+ - spec/support/string_presenter.rb