delegate_presenter 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ 0.0.2 (October 20, 2011)
2
+ ============================
3
+ The Previous version of the gem automatically extended ApplicationController. Pulling this gem into an actual project, it was found that this automatic extension broke controller class methods.
4
+
5
+ Now you must manually extend ApplicationController yourself. See the README on how to do this.
6
+
7
+ 0.0.1 (October 19, 2011)
8
+ ============================
9
+ Initial public release of this code (extracted from other projects)
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ gem 'actionpack', "~> 3.1.0", :require => 'action_view'
4
+
3
5
  # Specify your gem's dependencies in delegate_presenter.gemspec
4
6
  gemspec
@@ -32,11 +32,11 @@ As `DelegatePresenter::Base` subclasses are just `SimpleDelegator`s at heart, th
32
32
  So, what is DelegatePresenter, really?
33
33
  ================================
34
34
 
35
- DelegatePresenter does two things:
35
+ DelegatePresenter does three things:
36
36
 
37
- 1. Inherits from Ruby Standard Library's SimpleDelegator. This simple class solves many of the problems I've seen with decorators in the past. (calling object.method everywhere)
37
+ 1. Inherits from Ruby Standard Library's SimpleDelegator. This simple class solves many of the problems I've seen with presenter APIs in the past. (calling object.method everywhere)
38
38
 
39
- 2. Makes Rails helpers available to you via the helpers method, gives you s (above) and h (your old Rails 2 friend)
39
+ 2. Makes Rails helpers available to you via the `helpers` method, gives you `s` (above) and `h` (your old Rails 2 friend).
40
40
 
41
41
  3. Gives you a `record_id` method, which will return the ActiveRecord ID of the database object. Because I think it should have been this way in the first place :)
42
42
 
@@ -45,5 +45,39 @@ Installation
45
45
  ================================
46
46
 
47
47
  1. Add me to your Gemfile
48
- 2. In your `config/application.rb`, add an autoload path for `app/presenters/`
49
- 3. Create presenters for your classes. For example: `app/presenters/todo_presenter.rb`. These classes should subclass `DelegatePresenter::Base`
48
+ 2. In your ApplicationController `extend DelegatePresenter::ApplicationController`
49
+ 3. In your `config/application.rb`, add an autoload path for `app/presenters/`
50
+ 4. Create presenters for your classes. For example: `app/presenters/todo_presenter.rb`. These classes should subclass `DelegatePresenter::Base`
51
+
52
+
53
+ Credit where Credit Is Due
54
+ =================================
55
+
56
+ Most of this gem was inspired by (and the initial structure stolen from), [Avdi Grimm](avdi.org)'s blog posts on [Demeter](http://avdi.org/devblog/2011/07/05/demeter-its-not-just-a-good-idea-its-the-law/) and [Nil objects and Falsiness](http://avdi.org/devblog/2011/05/30/null-objects-and-falsiness/). While these articles talk about other things, they give part of a very simple Presenter strategy.
57
+
58
+ The simplest presenter strategy that could possibly work.
59
+
60
+ So I used it on a few projects, and extended the abilities were I had to. It's still *very* simple, doing only one or two things more than that original pattern. I just built on the shoulders of giants.
61
+
62
+ But what about....
63
+ ==============================
64
+
65
+ I know there are a few other presenter frameworks out there for Rails. One hasn't seen any activity since 2008, and one is [Draper](https://rubygems.org/gems/draper).
66
+
67
+ I'm not a big fan of Draper's API, and it seemed too heavy to me. If Draper's your style, that's great: different strokes for different folks.
68
+
69
+
70
+ License
71
+ ==============================
72
+
73
+ LICENSE:
74
+
75
+ (The MIT License)
76
+
77
+ Copyright © 2011 Ryan Wilcox
78
+
79
+ 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:
80
+
81
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
82
+
83
+ 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.
@@ -2,6 +2,8 @@ require "delegate_presenter/version"
2
2
  require 'delegate_presenter/application_controller'
3
3
  require 'delegate_presenter/present'
4
4
 
5
+ require 'delegate'
6
+
5
7
  module DelegatePresenter
6
8
  class Base < SimpleDelegator
7
9
 
@@ -16,7 +18,7 @@ module DelegatePresenter
16
18
 
17
19
 
18
20
  def helpers
19
- ApplicationController.all_helpers
21
+ ::ApplicationController.all_helpers # the GLOBAL ApplicationController, not the ApplicationController in this namespace
20
22
  end
21
23
 
22
24
  def s(*args)
@@ -1,3 +1,9 @@
1
+
2
+ # This module adds a method so DelegatePresenter::Base can access your Rails helpers
3
+ # You'll need to extend your ApplicationController with this module name
4
+ #
5
+ # Earlier versions of the gem extended ApplicationController for you, but introduced bugs
6
+ # (possibly dealing with load order???) WD-rpw 10-20-2011
1
7
  module DelegatePresenter::ApplicationController
2
8
  # Provide access to helper methods from outside controllers and views,
3
9
  # such as in Presenter objects. Rails provides ActionController::Base.helpers,
@@ -28,6 +34,3 @@ module DelegatePresenter::ApplicationController
28
34
  end
29
35
  end
30
36
 
31
- class ApplicationController
32
- extend DelegatePresenter::ApplicationController
33
- end
@@ -1,3 +1,3 @@
1
1
  module DelegatePresenter
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,12 +2,27 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
+ # Pull in some fake helpers. In a Real Rails app these would be provided by Rails
6
+ # TODO: is there an easy way to test this closer to the actual behavior? By pulling in
7
+ # some of Rails, for example?
8
+ class FakeHelpers
9
+ def h(s)
10
+ s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
11
+ end
12
+ end
13
+
14
+ class FakeDelegatePresenter < DelegatePresenter::Base
15
+ def helpers
16
+ FakeHelpers.new
17
+ end
18
+ end
19
+
5
20
  describe "DelegatePresenter::Base" do
6
21
 
7
22
  describe "HTML building helpers" do
8
23
 
9
24
  before do
10
- @ap = DelegatePresenter::Base.new(1)
25
+ @ap = FakeDelegatePresenter.new(1)
11
26
  end
12
27
 
13
28
  it "handles HTML elements that should not be escaped" do
@@ -0,0 +1,32 @@
1
+ module ActionController
2
+ class Base
3
+ @@before_filters = []
4
+ def self.before_filters
5
+ @@before_filters
6
+ end
7
+ def self.before_filter(name)
8
+ @@before_filters << name
9
+ end
10
+ end
11
+ end
12
+
13
+ module ApplicationHelper
14
+ end
15
+
16
+ class ApplicationController < ActionController::Base
17
+ extend ActionView::Helpers
18
+ extend ActionView::Helpers::TagHelper
19
+ extend ActionView::Helpers::UrlHelper
20
+ extend ApplicationHelper
21
+
22
+ extend DelegatePresenter::ApplicationController
23
+
24
+ def view_context
25
+ @view_context ||= ApplicationController
26
+ end
27
+
28
+ def view_context=(input)
29
+ @view_context = input
30
+ end
31
+
32
+ end
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'delegate_presenter'
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+
7
+ Bundler.require
8
+
9
+ Dir.glob(['./spec/samples/*.rb', './spec/support/*.rb']) do |file|
10
+ require file
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delegate_presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &82177300 !ruby/object:Gem::Requirement
16
+ requirement: &72643760 !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: :development
23
23
  prerelease: false
24
- version_requirements: *82177300
24
+ version_requirements: *72643760
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &82177090 !ruby/object:Gem::Requirement
27
+ requirement: &72643400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *82177090
35
+ version_requirements: *72643400
36
36
  description: Presenters are easier with delegators, and other friends
37
37
  email:
38
38
  - rwilcox@wilcoxd.com
@@ -41,6 +41,7 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
  files:
43
43
  - .gitignore
44
+ - CHANGELOG.markdown
44
45
  - Gemfile
45
46
  - README.markdown
46
47
  - Rakefile
@@ -50,6 +51,7 @@ files:
50
51
  - lib/delegate_presenter/present.rb
51
52
  - lib/delegate_presenter/version.rb
52
53
  - spec/delegate_presenter_base_spec.rb
54
+ - spec/samples/application_controller.rb
53
55
  - spec/spec_helper.rb
54
56
  homepage: http://github.com/rwilcox/delegate_presenter
55
57
  licenses: []