dapper 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Dapper
2
2
 
3
- Dapper adds the ability to generate presenters for your rails project.
3
+ Dapper adds the ability to generate presenters for your rails project. Presenters are helpful
4
+ with cleaning up your views. You can move any kind of logic out of your view and isolate it
5
+ in a presenter.
4
6
 
5
7
  ## Installation
6
8
 
@@ -24,22 +26,82 @@ Once the gem is installed, run the presenters generator.
24
26
 
25
27
  Example:
26
28
 
27
- $ rails generate presenter Photos index new edit
29
+ $ rails generate presenter Albums index edit show
28
30
 
29
31
  The result will be:
30
32
 
31
- myrailsproject
32
- |~app/
33
- |~presenters/
34
- |~photos/
35
- |-edit_presenter.rb
36
- |-index_presenter.rb
37
- `-new_presenter.rb
33
+ myrailsproject
34
+ |~app/
35
+ |~presenters/
36
+ |~albums/
37
+ |-edit_presenter.rb
38
+ |-index_presenter.rb
39
+ `-show_presenter.rb
38
40
 
39
41
  Running the presenter generator without any actions will default to
40
42
  one index_presenter.rb
41
43
 
42
44
 
45
+ To take advantage of the presenter, instantiate it in your controller.
46
+
47
+ class AlbumsController < ApplicationController
48
+
49
+ def edit
50
+ album = Album.find(params[:id])
51
+ @presenter = Albums::EditPresenter.new(album)
52
+ end
53
+
54
+ def index
55
+ albums = Album.all
56
+ @presenter = Albums::IndexPresenter.new(albums)
57
+ end
58
+
59
+ def show
60
+ album = Album.find(params[:id])
61
+ @presenter = Albums::ShowPresenter.new(album)
62
+ end
63
+
64
+ end
65
+
66
+ The presenter has access to view templates and helpers. This makes is very
67
+ easy to move ugly logic out of your views and put it in your presenters. Here
68
+ is an example that is pulling some logic for getting the most recent photos
69
+ from a photo album. Granted there are ways to better solve this in the model
70
+ itself, but you get the idea. This type of logic is not uncommon in views.
71
+ You could really put any type of display logic in a presenter. You can also
72
+ combine multiple models in a presenter. The big benefit here is that using a
73
+ presenter helps to maintain a flattened structure in your views.
74
+
75
+ module Albums
76
+ class ShowPresenter < Dapper::Base
77
+ def initialize(album)
78
+ @album = album
79
+ end
80
+
81
+ def recent_photos
82
+ unless album.photos.empty?
83
+ album.photos(:photo_id => nil, :order => [:created_at.desc])[0..11].each do |photo|
84
+ h.link_to( h.image_tag(photo.url_thumbnail), photo )
85
+ end
86
+ end
87
+ end
88
+
89
+ end
90
+ end
91
+
92
+ Presenters don't have to only make it easier to work with models. They can be used for
93
+ any type of view logic. They could adjust the text of a button based on some environment
94
+ conditions. That condition might have nothing to do with a model. You could also change
95
+ the visibility of sections of your page. Anything really. This is the reason there is a
96
+ presenter per view. Each view could have its own requirements for how it displays itself.
97
+
98
+ ## Acknowledgements
99
+
100
+ I wouldn't be doing justice to the open source community if I didn't give a big heads up to Jeff
101
+ Casimir and his [Draper](https://github.com/jcasimir/draper/) project. It had much influence on
102
+ the success of this project. It showed me how to do all the wiring into the Rails framework.
103
+
104
+
43
105
  ## Contributing
44
106
 
45
107
  1. Fork it
@@ -1,3 +1,3 @@
1
1
  module Dapper
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -2,7 +2,7 @@ class PresenterGenerator < Rails::Generators::NamedBase
2
2
  source_root File.expand_path('../templates', __FILE__)
3
3
  argument :actions, :type => :array, :default => ["index"], :banner => "action action"
4
4
 
5
- check_class_collision
5
+ #check_class_collision
6
6
  desc "Generate presenter."
7
7
 
8
8
  def manifest
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-17 00:00:00.000000000 Z
12
+ date: 2012-06-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A gem for using presenters in your rails application.
15
15
  email: