action_presenter 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rspec
6
+ .rvmrc
7
+ /.idea
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## Version 1.0
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in simple_presenter.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ ## Description
2
+
3
+ Missing link between models and views.
4
+ Use presenter pattern in Rails application without changing controllers.
5
+
6
+ **Based on *'Presenters from Scratch'* by Ryan Bates.**
7
+
8
+ ## Installation
9
+
10
+ ```ruby
11
+ gem 'action_presenter'
12
+ ```
13
+
14
+ or
15
+
16
+ ```ruby
17
+ gem 'action_presenter', git: 'git@github.com:zlw/action_presenter.git'
18
+ ```
19
+
20
+ Gem was tested under 1.9.3 and Rails 3.2.1
21
+
22
+ ## Usage
23
+
24
+ Create presenter class
25
+
26
+ ```ruby
27
+ # /app/presenters/post_presenter.rb
28
+ class PostPresenter < ActionPresenter::Base
29
+ presents :post
30
+
31
+ def title
32
+ link_to_unless_current post.title, post_path(post), class: :post_title_link
33
+ end
34
+
35
+ def content
36
+ content_tag :pre, post.content
37
+ end
38
+ end
39
+ ```
40
+
41
+ You can use all view helpers without any changes. Reference to model by method with the name passed to `presents` class method.
42
+
43
+ and then use it in view
44
+
45
+ ```haml
46
+ # /app/views/posts/index.html.haml
47
+ - present @post do |p|
48
+ = p.title
49
+ = p.content
50
+ ```
51
+
52
+ If You want to change presenter class pass it as second argument
53
+
54
+ ```haml
55
+ # /app/views/post/show.html.haml
56
+ - present @post, PostShowPresenter do |p|
57
+ = p.title
58
+ ```
59
+
60
+ All that without any change in any model or controller
61
+
62
+ ## License
63
+
64
+ Copyright (C) 2012 Krzysztof Zalewski
65
+
66
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
67
+
68
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
69
+
70
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "action_presenter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "action_presenter"
7
+ s.version = ActionPresenter::VERSION
8
+ s.authors = ["Krzysztof Zalewski"]
9
+ s.email = ["zlw.zalewski@gmail.com"]
10
+ s.homepage = "https://github.com/zlw/action_presenter"
11
+ s.summary = %q{Missing link between models and views. Use presenter pattern in Rails application without changing controllers.}
12
+ s.description = %q{Missing link between models and views. Use presenter pattern in Rails application without changing controllers.}
13
+
14
+ s.rubyforge_project = "action_presenter"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'actionpack'
22
+ s.add_dependency 'named_accessors'
23
+
24
+ s.add_development_dependency 'rake'
25
+ s.add_development_dependency 'rspec'
26
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_support/core_ext/string'
2
+ require 'active_support/concern'
3
+
4
+ require 'named_accessors'
5
+
6
+ require 'action_presenter/version'
7
+ require 'action_presenter/base'
8
+ require 'action_presenter/view_helper'
9
+
10
+ require 'action_presenter/railtie' if defined? Rails
11
+
12
+
13
+ module ActionPresenter
14
+ end
@@ -0,0 +1,19 @@
1
+ module ActionPresenter
2
+ class Base
3
+ def initialize(object, template)
4
+ @object, @template = object, template
5
+ end
6
+
7
+ private
8
+
9
+ named_reader :template, as: :h
10
+
11
+ def self.presents(name)
12
+ named_reader :object, as: name
13
+ end
14
+
15
+ def method_missing(*args, &block)
16
+ h.send(*args, &block)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module ActionPresenter
2
+ class Railtie < Rails::Railtie
3
+ initializer 'action_presenter.view_helper' do |app|
4
+ ActiveSupport.on_load :action_controller do
5
+ include ActionPresenter::ViewHelper
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ActionPresenter
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ module ActionPresenter
2
+ module ViewHelper
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :present
7
+ end
8
+
9
+ def present(object, klass = "#{object.class}Presenter".constantize)
10
+ yield klass.new(object, view_context) if block_given?
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require 'support/base'
3
+
4
+
5
+ describe ActionPresenter::Base do
6
+ let(:template) { mock(Object) }
7
+ let(:object) { mock(Object) }
8
+
9
+ let(:klass) { DummyPresenter }
10
+ let(:presenter) { klass.new(object, template) }
11
+
12
+ context '#instance_variables' do
13
+ it 'should have @object instance variable' do
14
+ presenter.instance_variables.should include :@object
15
+ end
16
+
17
+ it 'should have @template instance variable' do
18
+ presenter.instance_variables.should include :@template
19
+ end
20
+ end
21
+
22
+ context '#accessors' do
23
+ it 'should have #h reader method for @template' do
24
+ presenter.should respond_to :h
25
+ end
26
+
27
+ it 'should respond to reader method with the same name as parameter to #presents' do
28
+ klass.presents :dummy
29
+ presenter.should respond_to :dummy
30
+ end
31
+ end
32
+
33
+ context '#method_missing' do
34
+ it 'should pass every missing method to @template object' do
35
+ presenter.h.should_receive(:non_existing_method).and_return('i am here')
36
+ presenter.non_existing_method.should == 'i am here'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require 'support/view_helper'
3
+
4
+
5
+ describe ActionPresenter::ViewHelper do
6
+ let(:helper) { DummyActionController.send(:include, ActionPresenter::ViewHelper).new }
7
+
8
+ it 'should call default presenter if presenter class is not given' do
9
+ helper.present('foobar') do |p|
10
+ p.foobar
11
+ end.should == 'foo'
12
+ end
13
+
14
+ it 'should call given presenter class' do
15
+ helper.present('foobar', FoobarPresenter) do |p|
16
+ p.foobar
17
+ end.should == 'bar'
18
+ end
19
+
20
+ it 'should return nil if no block is given' do
21
+ helper.present('foobar').should be_nil
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ require 'action_presenter'
@@ -0,0 +1,2 @@
1
+ class DummyPresenter < ActionPresenter::Base
2
+ end
@@ -0,0 +1,21 @@
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
+ class StringPresenter < ActionPresenter::Base
12
+ def foobar
13
+ 'foo'
14
+ end
15
+ end
16
+
17
+ class FoobarPresenter < ActionPresenter::Base
18
+ def foobar
19
+ 'bar'
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_presenter
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Krzysztof Zalewski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: &70319726257920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70319726257920
25
+ - !ruby/object:Gem::Dependency
26
+ name: named_accessors
27
+ requirement: &70319726257500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70319726257500
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70319726257080 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70319726257080
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70319726256660 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70319726256660
58
+ description: Missing link between models and views. Use presenter pattern in Rails
59
+ application without changing controllers.
60
+ email:
61
+ - zlw.zalewski@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - CHANGELOG.md
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - action_presenter.gemspec
72
+ - lib/action_presenter.rb
73
+ - lib/action_presenter/base.rb
74
+ - lib/action_presenter/railtie.rb
75
+ - lib/action_presenter/version.rb
76
+ - lib/action_presenter/view_helper.rb
77
+ - spec/action_presenter/base_spec.rb
78
+ - spec/action_presenter/view_helper_spec.rb
79
+ - spec/spec_helper.rb
80
+ - spec/support/base.rb
81
+ - spec/support/view_helper.rb
82
+ homepage: https://github.com/zlw/action_presenter
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
95
+ - 0
96
+ hash: 1357576686972883001
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: 1357576686972883001
106
+ requirements: []
107
+ rubyforge_project: action_presenter
108
+ rubygems_version: 1.8.15
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Missing link between models and views. Use presenter pattern in Rails application
112
+ without changing controllers.
113
+ test_files:
114
+ - spec/action_presenter/base_spec.rb
115
+ - spec/action_presenter/view_helper_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/support/base.rb
118
+ - spec/support/view_helper.rb