public
Clone URL: git://github.com/nmerouze/action_presenter.git
Search Repo:
Initial import
Nicolas Mérouze (author)
Sun May 04 10:59:14 -0700 2008
commit  fd1def3f0b56db6bb8ce4d3c40f1819f704dbdae
tree    7cbc0ec7873f4062cd4ac0fba4aa6919f8e1b530
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
@@ -0,0 +1,21 @@
0
+Copyright (c) 2008 Nicolas Mérouze
0
+
0
+The MIT License
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining a copy
0
+of this software and associated documentation files (the "Software"), to deal
0
+in the Software without restriction, including without limitation the rights
0
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0
+copies of the Software, and to permit persons to whom the Software is
0
+furnished to do so, subject to the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be included in
0
+all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0
+THE SOFTWARE.
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
0
@@ -0,0 +1,55 @@
0
+= Action Presenter
0
+
0
+Inspired by : http://www.caboo.se/articles/2007/8/23/simple-presenters
0
+
0
+== Warning
0
+
0
+Works only with Edge Rails.
0
+
0
+== Install
0
+
0
+ ruby script/plugin install git://github.com/nmerouze/action_presenter.git
0
+
0
+== Usage
0
+
0
+Here's a presenter :
0
+
0
+ class ArticlePresenter < ActionPresenter::Base
0
+ def title
0
+ h @source.title
0
+ end
0
+ end
0
+
0
+And to call it in the views, there are 2 helpers :
0
+
0
+ <%= present(@article).title %> or <%= p(@article).title %>
0
+
0
+== Render files
0
+
0
+You can also render files for a presenter method :
0
+
0
+ class ArticlePresenter < ActionPresenter::Base
0
+ def title
0
+ render :title
0
+ end
0
+
0
+ def body
0
+ render :body, :msg => 'Hello World!'
0
+ end
0
+ end
0
+
0
+The partial for title will be located in app/presenters/article/_title.html.erb :
0
+
0
+ <%=h article.title %>
0
+
0
+The partial for body will be located in app/presenters/article/_body.html.erb :
0
+
0
+ <%= article.body + msg %>
0
+
0
+== Presenters in your plugins
0
+
0
+You can have presenters in plugins. Just create a directory app/presenters and you're ready to go. It's great if you have Engines too.
0
+
0
+== Contact
0
+
0
+nicolas.merouze (at) gmail (dot) com
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
0
@@ -0,0 +1,12 @@
0
+Dependencies.load_paths << Rails.root + '/app/presenters'
0
+
0
+config.after_initialize do
0
+ Rails.plugins.each do |plugin|
0
+ path = Rails.root + "/vendor/plugins/#{plugin.name}/app/presenters"
0
+
0
+ if File.directory?(path)
0
+ Dependencies.load_paths << path
0
+ ActionPresenter.view_paths << path
0
+ end
0
+ end
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
0
@@ -0,0 +1,42 @@
0
+module ActionPresenter
0
+ @@view_paths = [Rails.root + '/app/presenters']
0
+ mattr_accessor :view_paths
0
+
0
+ class Base
0
+ ActionView::Helpers.constants.each do |c|
0
+ mod = ActionView::Helpers.const_get(c)
0
+ include mod unless mod.is_a? Class
0
+ end
0
+
0
+ include ActionController::UrlWriter # named routes
0
+
0
+ attr_reader :controller, :source, :name
0
+
0
+ def initialize(controller, record)
0
+ @controller, @source = controller, record
0
+ @name = record.class.name.underscore.to_sym
0
+ end
0
+
0
+ protected
0
+
0
+ def render(view, options = {})
0
+ file = "#{name}/_#{view}"
0
+ action_view = ActionView::Base.new([], {}, @controller)
0
+ action_view.finder.view_paths = ActionPresenter.view_paths
0
+ action_view.render_file(file, true, options.merge(@name => @source))
0
+ end
0
+ end
0
+
0
+ module Helpers
0
+ @@presenters = {}
0
+
0
+ def present(record, name = nil)
0
+ name = record.class.name if name.nil?
0
+ @@presenters[name.underscore.to_sym] ||= "#{name.classify}Presenter".constantize.new(@controller, record)
0
+ end
0
+
0
+ alias :p :present
0
+ end
0
+end
0
+
0
+ActionView::Base.__send__ :include, ActionPresenter::Helpers
0
\ No newline at end of file

Comments

    No one has commented yet.