draper 0.7.4 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/Gemfile +2 -3
- data/Readme.markdown +2 -1
- data/lib/draper.rb +1 -1
- data/lib/draper/base.rb +1 -1
- data/lib/draper/system.rb +1 -1
- data/lib/draper/version.rb +1 -1
- data/lib/draper/view_context.rb +11 -0
- data/spec/base_spec.rb +8 -2
- data/spec/generators/draper/decorator/decorator_generator_spec.rb +28 -0
- data/spec/samples/application_controller.rb +28 -6
- data/spec/spec_helper.rb +1 -2
- data/spec/view_context_spec.rb +25 -0
- metadata +7 -3
- data/lib/draper/all_helpers.rb +0 -50
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
source :rubygems
|
2
2
|
|
3
3
|
gem 'rake'
|
4
|
-
gem 'rspec', '~> 2.0
|
4
|
+
gem 'rspec', '~> 2.0'
|
5
5
|
gem 'activesupport', '~> 3.0.10'
|
6
6
|
gem 'actionpack', "~> 3.0.10", :require => 'action_view'
|
7
|
+
gem 'ammeter', '~> 0.1.3', :require => 'ammeter/init'
|
7
8
|
gem 'guard'
|
8
9
|
gem 'guard-rspec'
|
9
10
|
gem 'launchy'
|
10
|
-
gem 'rcov', :platforms => [:mri_18]
|
11
|
-
gem 'cover_me', '>= 1.0.0.rc6', :platforms => [:mri_19]
|
12
11
|
gem 'yard'
|
13
12
|
|
14
13
|
gemspec
|
data/Readme.markdown
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Draper: View Models for Rails
|
2
2
|
|
3
|
+
![TravisCI Build Status](https://secure.travis-ci.org/jcasimir/draper.png)
|
4
|
+
|
3
5
|
## Quick Start
|
4
6
|
|
5
7
|
1. Add `gem 'draper'` to your `Gemfile` and `bundle`
|
@@ -319,7 +321,6 @@ end
|
|
319
321
|
* Add YARD documentation to source
|
320
322
|
* Add a section about contributing
|
321
323
|
* Generators
|
322
|
-
* Test coverage for generators (help!)
|
323
324
|
* Implement hook so generating a controller/scaffold generates a decorator
|
324
325
|
* Add generators for...
|
325
326
|
* `draper:model`: Model + Decorator
|
data/lib/draper.rb
CHANGED
data/lib/draper/base.rb
CHANGED
data/lib/draper/system.rb
CHANGED
data/lib/draper/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -2,9 +2,16 @@ require 'spec_helper'
|
|
2
2
|
require 'draper'
|
3
3
|
|
4
4
|
describe Draper::Base do
|
5
|
+
before(:each){ ApplicationController.new.set_current_view_context }
|
5
6
|
subject{ Draper::Base.new(source) }
|
6
7
|
let(:source){ Product.new }
|
7
8
|
|
9
|
+
context(".helpers") do
|
10
|
+
it "should have a valid view_context" do
|
11
|
+
subject.helpers.should be
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
8
15
|
context(".lazy_helpers") do
|
9
16
|
it "makes Rails helpers available without using the h. proxy" do
|
10
17
|
Draper::Base.lazy_helpers
|
@@ -43,10 +50,9 @@ describe Draper::Base do
|
|
43
50
|
|
44
51
|
context("selecting methods") do
|
45
52
|
it "echos the methods of the wrapped class except default exclusions" do
|
46
|
-
pending "Fine on 1.9 but fails on 1.8 due to differences in implementation of respond_to and method_missing. Help?"
|
47
53
|
source.methods.each do |method|
|
48
54
|
unless Draper::Base::DEFAULT_DENIED.include?(method)
|
49
|
-
subject.should respond_to(method)
|
55
|
+
subject.should respond_to(method.to_sym)
|
50
56
|
end
|
51
57
|
end
|
52
58
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Generators are not automatically loaded by Rails
|
4
|
+
require 'generators/draper/decorator/decorator_generator'
|
5
|
+
|
6
|
+
describe Draper::DecoratorGenerator do
|
7
|
+
# Tell the generator where to put its output (what it thinks of as Rails.root)
|
8
|
+
destination File.expand_path("../../../../../tmp", __FILE__)
|
9
|
+
|
10
|
+
before { prepare_destination }
|
11
|
+
|
12
|
+
describe 'no arguments' do
|
13
|
+
before { run_generator %w(products) }
|
14
|
+
|
15
|
+
describe 'app/decorators/application_decorator.rb' do
|
16
|
+
subject { file('app/decorators/application_decorator.rb') }
|
17
|
+
it { should exist }
|
18
|
+
it { should contain "class ApplicationDecorator < Draper::Base" }
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'app/decorators/products_decorator.rb' do
|
22
|
+
subject { file('app/decorators/products_decorator.rb') }
|
23
|
+
it { should exist }
|
24
|
+
it { should contain "class ProductsDecorator < ApplicationDecorator" }
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -1,10 +1,32 @@
|
|
1
|
-
|
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
|
+
class ApplicationController < ActionController::Base
|
2
14
|
extend ActionView::Helpers
|
3
15
|
extend ActionView::Helpers::TagHelper
|
4
16
|
extend ActionView::Helpers::UrlHelper
|
5
|
-
extend ApplicationHelper
|
6
|
-
|
7
|
-
def
|
8
|
-
|
17
|
+
extend ApplicationHelper
|
18
|
+
|
19
|
+
def view_context
|
20
|
+
@view_context ||= ApplicationController
|
9
21
|
end
|
10
|
-
|
22
|
+
|
23
|
+
def view_context=(input)
|
24
|
+
@view_context = input
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.hello
|
28
|
+
"Hello!"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Draper::System.setup
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'draper'
|
3
|
+
|
4
|
+
describe Draper::ViewContext do
|
5
|
+
let (:app_controller) do
|
6
|
+
ApplicationController
|
7
|
+
end
|
8
|
+
|
9
|
+
let (:app_controller_instance) do
|
10
|
+
app_controller.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it "implements #set_current_view_context" do
|
14
|
+
app_controller_instance.should respond_to(:set_current_view_context)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "calls #before_filter with #set_current_view_context" do
|
18
|
+
app_controller.before_filters.should include(:set_current_view_context)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "raises an exception if the view_context is fetched without being set" do
|
22
|
+
Thread.current[:current_view_context] = nil
|
23
|
+
expect {app_controller.current_view_context}.should raise_exception(Exception)
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: draper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
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: 2011-10-
|
12
|
+
date: 2011-10-08 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Draper reimagines the role of helpers in the view layer of a Rails application,
|
15
15
|
allowing an object-oriented approach rather than procedural.
|
@@ -49,12 +49,12 @@ files:
|
|
49
49
|
- doc/top-level-namespace.html
|
50
50
|
- draper.gemspec
|
51
51
|
- lib/draper.rb
|
52
|
-
- lib/draper/all_helpers.rb
|
53
52
|
- lib/draper/base.rb
|
54
53
|
- lib/draper/lazy_helpers.rb
|
55
54
|
- lib/draper/model_support.rb
|
56
55
|
- lib/draper/system.rb
|
57
56
|
- lib/draper/version.rb
|
57
|
+
- lib/draper/view_context.rb
|
58
58
|
- lib/generators/draper/decorator/USAGE
|
59
59
|
- lib/generators/draper/decorator/decorator_generator.rb
|
60
60
|
- lib/generators/draper/decorator/templates/application_decorator.rb
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- lib/generators/rails/decorator_generator.rb
|
63
63
|
- spec/base_spec.rb
|
64
64
|
- spec/draper/model_support_spec.rb
|
65
|
+
- spec/generators/draper/decorator/decorator_generator_spec.rb
|
65
66
|
- spec/samples/active_record.rb
|
66
67
|
- spec/samples/application_controller.rb
|
67
68
|
- spec/samples/application_helper.rb
|
@@ -71,6 +72,7 @@ files:
|
|
71
72
|
- spec/samples/product.rb
|
72
73
|
- spec/samples/product_decorator.rb
|
73
74
|
- spec/spec_helper.rb
|
75
|
+
- spec/view_context_spec.rb
|
74
76
|
homepage: http://github.com/jcasimir/draper
|
75
77
|
licenses: []
|
76
78
|
post_install_message:
|
@@ -98,6 +100,7 @@ summary: Decorator pattern implmentation for Rails.
|
|
98
100
|
test_files:
|
99
101
|
- spec/base_spec.rb
|
100
102
|
- spec/draper/model_support_spec.rb
|
103
|
+
- spec/generators/draper/decorator/decorator_generator_spec.rb
|
101
104
|
- spec/samples/active_record.rb
|
102
105
|
- spec/samples/application_controller.rb
|
103
106
|
- spec/samples/application_helper.rb
|
@@ -107,3 +110,4 @@ test_files:
|
|
107
110
|
- spec/samples/product.rb
|
108
111
|
- spec/samples/product_decorator.rb
|
109
112
|
- spec/spec_helper.rb
|
113
|
+
- spec/view_context_spec.rb
|
data/lib/draper/all_helpers.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
module Draper
|
2
|
-
module AllHelpers
|
3
|
-
# Most of the black magic here thanks to Xavier Shay (@xshay)
|
4
|
-
# Provide access to helper methods from outside controllers and views,
|
5
|
-
# such as in Presenter objects. Rails provides ActionController::Base.helpers,
|
6
|
-
# but this does not include any of our application helpers.
|
7
|
-
def all_helpers
|
8
|
-
@all_helpers_proxy ||= begin
|
9
|
-
# Start with just the rails helpers. This is the same method used
|
10
|
-
# by ActionController::Base.helpers
|
11
|
-
# proxy = ActionView::Base.new.extend(_helpers)
|
12
|
-
proxy = ActionController::Base.helpers
|
13
|
-
|
14
|
-
# url_for depends on _routes method being defined
|
15
|
-
proxy.instance_eval do
|
16
|
-
def _routes
|
17
|
-
Rails.application.routes
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# Import all named path methods
|
22
|
-
proxy.extend(Rails.application.routes.named_routes.module)
|
23
|
-
|
24
|
-
# Load all our application helpers to extend
|
25
|
-
modules_for_helpers([:all]).each do |mod|
|
26
|
-
proxy.extend(mod)
|
27
|
-
end
|
28
|
-
|
29
|
-
proxy.instance_eval do
|
30
|
-
def controller
|
31
|
-
#Object.controller
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
proxy.instance_eval do
|
36
|
-
# A hack since this proxy doesn't pick up default_url_options from anywhere
|
37
|
-
def url_for(*args)
|
38
|
-
if args.last.is_a?(Hash) && !args.last[:only_path]
|
39
|
-
args = args.dup
|
40
|
-
args << args.pop.merge('host' => ActionMailer::Base.default_url_options[:host])
|
41
|
-
end
|
42
|
-
super(*args)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
proxy
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|