garnish 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +27 -0
- data/Gemfile +6 -0
- data/Guardfile +13 -3
- data/README.rdoc +100 -2
- data/garnish.gemspec +1 -1
- data/lib/garnish.rb +5 -0
- data/lib/garnish/converter.rb +11 -17
- data/lib/garnish/model_adapters/abstract_adapter.rb +13 -0
- data/lib/garnish/model_adapters/active_record_adapter.rb +11 -0
- data/lib/garnish/model_adapters/default_adapter.rb +12 -0
- data/lib/garnish/model_adapters/mongoid_adapter.rb +11 -0
- data/lib/garnish/presenter.rb +5 -30
- data/lib/garnish/presenter/relationships.rb +11 -16
- data/lib/garnish/responder.rb +1 -4
- data/lib/garnish/version.rb +1 -1
- data/spec/README.rdoc +10 -0
- data/spec/garnish/controller_spec.rb +8 -5
- data/spec/garnish/converter_spec.rb +19 -41
- data/spec/garnish/presenter/relationships_spec.rb +11 -23
- data/spec/garnish/presenter_spec.rb +13 -65
- data/spec/garnish/responder_spec.rb +29 -0
- data/spec/spec_helper.rb +27 -12
- metadata +75 -41
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
0.0.3 (February 1, 2012)
|
2
|
+
|
3
|
+
* Version bump so I can push new version to rubygems
|
4
|
+
|
5
|
+
0.0.2 (February 1, 2012)
|
6
|
+
|
7
|
+
* Added spec/README.rdoc
|
8
|
+
* Added CHANGELOG.rdoc
|
9
|
+
* Actually wrote some docs for README.rdoc
|
10
|
+
* Version Bump to 0.0.2
|
11
|
+
|
12
|
+
0.0.2 (January 28, 2011)
|
13
|
+
|
14
|
+
* Almost a complete re-write from the original Decorator wrapping method to
|
15
|
+
the new presenter inclusion method.
|
16
|
+
* Updated all specs to adhere to new extension method.
|
17
|
+
|
18
|
+
0.0.1 (November 03, 2011)
|
19
|
+
|
20
|
+
* Initial Release
|
21
|
+
|
22
|
+
0.0.1 (October 25, 2011)
|
23
|
+
|
24
|
+
* Initial Commit
|
25
|
+
|
26
|
+
|
27
|
+
|
data/Gemfile
CHANGED
data/Guardfile
CHANGED
@@ -4,12 +4,22 @@ guard 'bundler' do
|
|
4
4
|
# watch(/^.+\.gemspec/)
|
5
5
|
end
|
6
6
|
|
7
|
-
guard '
|
7
|
+
guard 'spin' do
|
8
|
+
# uses the .rspec file
|
9
|
+
# --colour --fail-fast --format documentation --tag ~slow
|
10
|
+
watch(%r{^spec/.+_spec\.rb})
|
11
|
+
watch(%r{^app/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
|
12
|
+
watch(%r{^app/(.+)\.haml}) { |m| "spec/#{m[1]}.haml_spec.rb" }
|
13
|
+
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
14
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/requests/#{m[1]}_spec.rb"] }
|
8
15
|
watch('Gemfile')
|
9
16
|
watch('Gemfile.lock')
|
10
17
|
watch('spec/spec_helper.rb')
|
18
|
+
watch('Guardfile')
|
11
19
|
end
|
12
20
|
|
13
|
-
guard 'rspec', :cli => "--drb", :version => 2, :rvm => ['1.9.3'] do
|
14
|
-
watch(%r{^
|
21
|
+
guard 'rspec', :cli => "--drb --color --order random", :version => 2, :rvm => ['1.9.3'] do
|
22
|
+
watch(%r{^spec/.+_spec\.rb$})
|
23
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
24
|
+
watch('spec/spec_helper.rb') { "spec" }
|
15
25
|
end
|
data/README.rdoc
CHANGED
@@ -1,2 +1,100 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
= Garnish {<img src="http://travis-ci.org/brianp/garnish.png" />}[http://travis-ci.org/brianp/garnish]
|
2
|
+
|
3
|
+
RDocs[http://rdoc.info/projects/brianp/garnish]
|
4
|
+
|
5
|
+
Garnish is a Presenter / Decorator pattern for models in Rails. It gives a home to presntation level view logic that normally litters helpers and view files. Using it can help you:
|
6
|
+
|
7
|
+
* Keep helper files and views clean of if/else statements and other logic
|
8
|
+
* Better define an interface for views to interact with models
|
9
|
+
* Maintain a very familiar OOP pattern within your project directory
|
10
|
+
* Keep you controller code clean by never having to instantiate the presenters
|
11
|
+
* Handle Decorating / Presenting standard Rails relationships
|
12
|
+
|
13
|
+
== Installation
|
14
|
+
|
15
|
+
In <b>Rails 3</b>, add this to your Gemfile and run the +bundle+ command.
|
16
|
+
|
17
|
+
gem "garnish"
|
18
|
+
|
19
|
+
Alternatively, you can install it as a plugin.
|
20
|
+
|
21
|
+
rails plugin install git://github.com/brianp/garnish.git
|
22
|
+
|
23
|
+
|
24
|
+
== Getting Started
|
25
|
+
|
26
|
+
A lot of magic happens with Garnish in an attempt to make things incredibly simple for you.
|
27
|
+
|
28
|
+
=== 1. Define You Presenters (Generators coming soon!)
|
29
|
+
|
30
|
+
Create a folder in app/ called presenters. ex <tt>app/presenters/</tt>
|
31
|
+
|
32
|
+
Create a presenter matching the name of your model in your presenters folder. ex <tt>app/presenters/user_presenter.rb</tt>
|
33
|
+
|
34
|
+
Define the user_presenter
|
35
|
+
|
36
|
+
module UserPresenter
|
37
|
+
include Garnish::Presenter
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
=== 2. Add methods to your presenter
|
42
|
+
|
43
|
+
Define method in your presenter as regular instance methods
|
44
|
+
|
45
|
+
module UserPresenter
|
46
|
+
include Garnish::Presenter
|
47
|
+
|
48
|
+
def greeting
|
49
|
+
if last_login.nil?
|
50
|
+
"Welcome #{name}, can we offer you a tour?"
|
51
|
+
else
|
52
|
+
"Welcome back #{name}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
=== 3. Add the <tt>garnish</tt> call
|
59
|
+
|
60
|
+
In <tt>app/controllers/application_controller.rb</tt> add the garnish call.
|
61
|
+
|
62
|
+
class ApplicationController < ActionController::Base
|
63
|
+
protect_from_forgery
|
64
|
+
|
65
|
+
garnish
|
66
|
+
end
|
67
|
+
|
68
|
+
Or If you don't want to use Garnish application wide simply add the call into a specific controller.
|
69
|
+
|
70
|
+
=== 4. Use respond_with
|
71
|
+
|
72
|
+
The only change you need to make to start using your presenters now is to use respond_with inside any controller that has been garnished
|
73
|
+
|
74
|
+
<tt>app/controllers/user_controller.rb</tt>
|
75
|
+
|
76
|
+
def show
|
77
|
+
@user = User.find(params[:id])
|
78
|
+
|
79
|
+
respond_with @user do |format|
|
80
|
+
format.html # show.html.erb
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
Garnish will now find and load presenters for any instance variables you set inside your action. *NOT* just the ivar passed to the respond_with block.
|
85
|
+
|
86
|
+
== 5. Start using your presenter methods
|
87
|
+
|
88
|
+
<tt>app/views/users/show.html.erb</tt>
|
89
|
+
|
90
|
+
<span><%= @user.greeting %></span>
|
91
|
+
|
92
|
+
== Questions or Problems?
|
93
|
+
|
94
|
+
If you have any issues with Garnish which you cannot find the solution to, please add an {issue on GitHub}[https://github.com/brianp/garnish/issues] or fork the project and send a pull request.
|
95
|
+
|
96
|
+
To get the specs running you should call +bundle+ and then +rake+. See the {spec/README}[https://github.com/brianp/garnish/blob/master/spec/README.rdoc] for more information.
|
97
|
+
|
98
|
+
== Special Thanks
|
99
|
+
|
100
|
+
Garnish was inspired by draper[https://github.com/jcasimir/draper/] and the RailsCast Pro episode #287 Presenters from Scratch. See the CHANGELOG[https://github.com/brianp/garnish/blob/master/CHANGELOG.rdoc] for the full list.
|
data/garnish.gemspec
CHANGED
data/lib/garnish.rb
CHANGED
@@ -4,3 +4,8 @@ require 'garnish/responder'
|
|
4
4
|
require 'garnish/controller'
|
5
5
|
require 'garnish/presenter/relationships'
|
6
6
|
require 'garnish/presenter'
|
7
|
+
|
8
|
+
require 'garnish/model_adapters/abstract_adapter'
|
9
|
+
require 'garnish/model_adapters/default_adapter'
|
10
|
+
require 'garnish/model_adapters/mongoid_adapter' if defined? Mongoid
|
11
|
+
require 'garnish/model_adapters/active_record_adapter' if defined? ActiveRecord
|
data/lib/garnish/converter.rb
CHANGED
@@ -2,16 +2,14 @@ module Garnish
|
|
2
2
|
module Converter
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
module
|
6
|
-
|
5
|
+
module InstanceMethods
|
6
|
+
|
7
|
+
def module_exists?(class_name)
|
7
8
|
klass = Module.const_get(class_name)
|
8
|
-
return klass.is_a?(
|
9
|
+
return klass.is_a?(Module)
|
9
10
|
rescue NameError
|
10
11
|
return false
|
11
12
|
end
|
12
|
-
end
|
13
|
-
|
14
|
-
module InstanceMethods
|
15
13
|
|
16
14
|
def convert(record, view = nil)
|
17
15
|
view ||= self.template
|
@@ -24,22 +22,18 @@ module Garnish
|
|
24
22
|
|
25
23
|
presenter_name = "#{klass.to_s}Presenter"
|
26
24
|
|
27
|
-
if
|
25
|
+
if module_exists?(presenter_name.to_sym)
|
28
26
|
if record.respond_to?(:each)
|
29
|
-
|
27
|
+
record.map do |v|
|
28
|
+
v.extend(presenter_name.constantize)
|
29
|
+
v.template = view
|
30
|
+
end
|
30
31
|
else
|
31
|
-
|
32
|
+
record.extend(presenter_name.constantize)
|
33
|
+
record.template = view
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
35
|
-
presenter
|
36
|
-
end
|
37
|
-
|
38
|
-
def class_exists?(class_name)
|
39
|
-
klass = Module.const_get(class_name)
|
40
|
-
return klass.is_a?(Class)
|
41
|
-
rescue NameError
|
42
|
-
return false
|
43
37
|
end
|
44
38
|
end
|
45
39
|
|
data/lib/garnish/presenter.rb
CHANGED
@@ -1,48 +1,23 @@
|
|
1
1
|
module Garnish
|
2
2
|
module Presenter
|
3
3
|
extend ActiveSupport::Concern
|
4
|
-
include Garnish::Presenter::Relationships
|
5
4
|
|
6
5
|
included do
|
7
|
-
attr_accessor :record
|
8
6
|
attr_accessor :template
|
9
|
-
attr_accessor :record_class
|
10
7
|
end
|
11
8
|
|
12
|
-
module
|
13
|
-
def
|
14
|
-
|
15
|
-
self.template = t
|
16
|
-
self.record_class = r.class
|
17
|
-
end
|
18
|
-
|
19
|
-
def respond_to?(method, include_private = false)
|
20
|
-
if self.methods.include?(method)
|
21
|
-
super
|
22
|
-
else
|
23
|
-
self.record.respond_to?(method)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
protected
|
28
|
-
|
29
|
-
def method_missing(method, *args, &block)
|
30
|
-
begin
|
31
|
-
# Check the record being presented first
|
32
|
-
# If method doesn't exists check the template for helper convenience
|
33
|
-
self.record.send(method, *args, &block)
|
34
|
-
rescue NoMethodError
|
35
|
-
self.template.send(method, *args, &block)
|
36
|
-
end
|
9
|
+
module ClassMethods
|
10
|
+
def extended(base)
|
11
|
+
base.extend Garnish::Presenter::Relationships
|
37
12
|
end
|
38
13
|
end
|
39
14
|
|
40
|
-
module
|
15
|
+
module InstanceMethods
|
41
16
|
|
42
17
|
protected
|
43
18
|
|
44
19
|
def method_missing(method, *args, &block)
|
45
|
-
|
20
|
+
self.template.send(method, *args, &block) unless @template.blank?
|
46
21
|
end
|
47
22
|
end
|
48
23
|
|
@@ -1,28 +1,23 @@
|
|
1
1
|
module Garnish
|
2
2
|
module Presenter
|
3
3
|
module Relationships
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
include Garnish::Converter
|
6
4
|
|
7
|
-
|
8
|
-
|
5
|
+
def self.extended(base)
|
6
|
+
base.extend Garnish::Converter::InstanceMethods
|
7
|
+
base.class_eval do
|
8
|
+
relationships = Garnish::ModelAdapters::AbstractAdapter.adapter_class.defined_relationships(self)
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
relationships.map do |key|
|
11
|
+
alias_method "#{key}_orig".to_sym, key.to_sym
|
12
|
+
define_method "#{key}" do |opts = nil, *rest|
|
13
|
+
records = self.send("#{key}_orig")
|
14
|
+
convert(records)
|
15
|
+
records
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
module ClassMethods
|
20
|
-
def record_class
|
21
|
-
str = self.to_s
|
22
|
-
str.slice!("Presenter")
|
23
|
-
str.constantize
|
24
|
-
end
|
25
|
-
end
|
26
21
|
end
|
27
22
|
end
|
28
23
|
end
|
data/lib/garnish/responder.rb
CHANGED
@@ -6,10 +6,7 @@ module Garnish
|
|
6
6
|
|
7
7
|
vars.each do |var|
|
8
8
|
record = controller.instance_variable_get(var.to_s)
|
9
|
-
|
10
|
-
presenter = convert(record, controller.view_context)
|
11
|
-
|
12
|
-
controller.instance_variable_set(var.to_s, presenter)
|
9
|
+
convert(record, controller.view_context)
|
13
10
|
end
|
14
11
|
|
15
12
|
super
|
data/lib/garnish/version.rb
CHANGED
data/spec/README.rdoc
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
= Garnish Specs
|
2
|
+
|
3
|
+
== Running the specs
|
4
|
+
|
5
|
+
To run the specs first run the +bundle+ command to install the necessary gems and the +rake+ command to run the specs.
|
6
|
+
|
7
|
+
bundle
|
8
|
+
rake
|
9
|
+
|
10
|
+
The specs currently require Ruby 1.9.2+ support for 1.8.7 may come if required.
|
@@ -4,14 +4,17 @@ describe "A Garnished Controller" do
|
|
4
4
|
|
5
5
|
subject { TestController }
|
6
6
|
|
7
|
-
it "should have the
|
8
|
-
subject.
|
7
|
+
it "should have the Garnish::Controller module already included" do
|
8
|
+
subject.included_modules.include?(Garnish::Controller).should be_true
|
9
9
|
end
|
10
10
|
|
11
|
-
it "should set the responder to Garnish::Responder" do
|
11
|
+
it "that calls the garnish marco should set the responder to Garnish::Responder" do
|
12
12
|
subject.garnish
|
13
13
|
subject.responder.should equal Garnish::Responder
|
14
14
|
end
|
15
|
-
end
|
16
|
-
|
17
15
|
|
16
|
+
it "should call respond_to with :html" do
|
17
|
+
subject.should_receive(:respond_to).with(:html)
|
18
|
+
subject.garnish
|
19
|
+
end
|
20
|
+
end
|
@@ -4,59 +4,37 @@ describe "A Garnish Converter" do
|
|
4
4
|
|
5
5
|
context "instance" do
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@test_presenter = TestClassPresenter.new(@test_class, @template)
|
11
|
-
end
|
12
|
-
|
13
|
-
subject { @test_presenter }
|
14
|
-
|
15
|
-
it "should define class_exists?" do
|
16
|
-
subject.respond_to?(:class_exists?).should be_true
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should return true from class_exists if class exists" do
|
20
|
-
subject.class_exists?(:TestClassPresenter).should be_true
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should return false from class_exists if class doesn't exists" do
|
24
|
-
subject.class_exists?(:BadClass).should be_false
|
25
|
-
end
|
7
|
+
let(:template) { stub }
|
8
|
+
let(:test_class) { TestClass.new }
|
9
|
+
let(:responder) { TestResponder.new }
|
26
10
|
|
27
11
|
context "with a template" do
|
28
|
-
it "should take a record variable and
|
29
|
-
|
30
|
-
|
31
|
-
|
12
|
+
it "should take a record variable and include the presenter module" do
|
13
|
+
responder.convert(test_class, template)
|
14
|
+
test_class.eigenclass.included_modules.should include TestClassPresenter
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should take an array of records and include the presenter module" do
|
18
|
+
records = [test_class, TestClass.new, TestClass.new]
|
19
|
+
responder.convert(records)
|
20
|
+
records.map { |r| r.eigenclass.included_modules.should include TestClassPresenter }
|
32
21
|
end
|
33
22
|
end
|
34
23
|
|
35
24
|
context "without a template" do
|
36
|
-
it "should take a record variable and
|
37
|
-
|
38
|
-
|
39
|
-
presenter.record.should equal @test_class
|
40
|
-
presenter.template.should equal @template
|
25
|
+
it "should take a record variable and include the presenter module" do
|
26
|
+
responder.convert(test_class)
|
27
|
+
test_class.eigenclass.included_modules.should include TestClassPresenter
|
41
28
|
end
|
42
|
-
end end
|
43
|
-
|
44
|
-
context "class" do
|
45
|
-
subject { TestClassPresenter }
|
46
|
-
|
47
|
-
it "should define class_exists?" do
|
48
|
-
subject.respond_to?(:class_exists?).should be_true
|
49
29
|
end
|
50
30
|
|
51
|
-
it "should return true from
|
52
|
-
|
31
|
+
it "should return true from module_exists if module exists" do
|
32
|
+
responder.module_exists?(:TestClassPresenter).should be_true
|
53
33
|
end
|
54
34
|
|
55
|
-
it "should return false from
|
56
|
-
|
35
|
+
it "should return false from module_exists if module doesn't exists" do
|
36
|
+
responder.module_exists?(:BadClass).should be_false
|
57
37
|
end
|
58
38
|
end
|
59
39
|
|
60
40
|
end
|
61
|
-
|
62
|
-
|
@@ -2,33 +2,21 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "A Presenter Relationship" do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
before(:each) do
|
8
|
-
@test_class = TestClass.new
|
9
|
-
@template = Class.new
|
10
|
-
@test_presenter = TestClassPresenter.new(@test_class, @template)
|
11
|
-
end
|
12
|
-
|
13
|
-
subject { @test_presenter }
|
14
|
-
|
15
|
-
it "should have the records relationships defined" do
|
16
|
-
subject.respond_to?(:users).should be_true
|
17
|
-
end
|
5
|
+
let(:test_class) { TestClass.new }
|
6
|
+
before { test_class.extend Garnish::Presenter::Relationships }
|
18
7
|
|
8
|
+
it "should respond to the original method name of the relationships" do
|
9
|
+
test_class.stub(:template => nil)
|
10
|
+
test_class.users
|
19
11
|
end
|
20
12
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
subject.record_class.should equal TestClass
|
26
|
-
end
|
13
|
+
it "should call convert on the records of the relationship" do
|
14
|
+
test_class.should_receive(:convert)
|
15
|
+
test_class.users
|
16
|
+
end
|
27
17
|
|
28
|
-
|
29
|
-
|
30
|
-
end
|
18
|
+
it "should alias the relationship methods" do
|
19
|
+
test_class.respond_to?(:users_orig).should be_true
|
31
20
|
end
|
32
21
|
|
33
22
|
end
|
34
|
-
|
@@ -1,75 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "A Presenter" do
|
4
|
-
context "should initialize" do
|
5
|
-
it "with record and no template" do
|
6
|
-
TestClassPresenter.new(TestClass.new, nil).should be_true
|
7
|
-
end
|
3
|
+
describe "A Garnish Presenter" do
|
8
4
|
|
9
|
-
|
10
|
-
|
11
|
-
@test_class = TestClass.new
|
12
|
-
@template = Class.new
|
13
|
-
@test_presenter = TestClassPresenter.new(@test_class, @template)
|
14
|
-
end
|
5
|
+
let(:test_class) { TestClass.new }
|
6
|
+
before { TestClass.send :include, TestClassPresenter }
|
15
7
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
subject.record.should equal @test_class
|
20
|
-
end
|
21
|
-
|
22
|
-
it "the template field" do
|
23
|
-
subject.template.should equal @template
|
24
|
-
end
|
25
|
-
|
26
|
-
it "the record_class field" do
|
27
|
-
subject.record_class.should equal @test_class.class
|
28
|
-
end
|
29
|
-
end
|
8
|
+
it "should add the template attribute accessor" do
|
9
|
+
test_class.template = 42
|
10
|
+
test_class.template.should eq 42
|
30
11
|
end
|
31
12
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
TestClass.should_receive(:test_method).and_raise(NoMethodError)
|
37
|
-
lambda {subject.test_method}.should raise_error
|
38
|
-
end
|
13
|
+
it "should send missing methods to the view context" do
|
14
|
+
test_class.template = stub(:template)
|
15
|
+
test_class.template.should_receive(:this_call)
|
16
|
+
test_class.this_call
|
39
17
|
end
|
40
18
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
@template = Class.new
|
45
|
-
@test_presenter = TestClassPresenter.new(@test_class, @template)
|
46
|
-
end
|
47
|
-
|
48
|
-
subject { @test_presenter }
|
49
|
-
|
50
|
-
context "delegate" do
|
51
|
-
it "method missing to record if method is missing" do
|
52
|
-
@test_class.should_receive(:test_method).and_return("Success!")
|
53
|
-
subject.test_method.should match "Success!"
|
54
|
-
end
|
55
|
-
|
56
|
-
it "method missing to template if record lacks method" do
|
57
|
-
@template.should_receive(:test_method).and_return("Success!")
|
58
|
-
subject.test_method.should match "Success!"
|
59
|
-
end
|
60
|
-
|
61
|
-
it "respond_to? to record if method doesn't exist" do
|
62
|
-
@test_class.should_receive(:respond_to?).with(:test_method).and_return(true)
|
63
|
-
subject.respond_to?(:test_method).should be_true
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
context "not delegate" do
|
68
|
-
it "respond_to? when methods exisits" do
|
69
|
-
@test_class.should_not_receive(:respond_to?)
|
70
|
-
subject.respond_to?(:respond_to?).should be_true
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
19
|
+
it "should include Garnish::Presenter::Relationships when included" do
|
20
|
+
test_class.extend TestClassPresenter
|
21
|
+
test_class.eigenclass.included_modules.should include Garnish::Presenter::Relationships
|
74
22
|
end
|
75
23
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "The Garnished Responder" do
|
4
|
+
|
5
|
+
let(:responder) { Garnish::Responder.new(controller, resources) }
|
6
|
+
let(:formats) { stub(:formats, :first => nil) }
|
7
|
+
let(:controller) { stub(:controller, :request => nil, :formats => formats, :view_context => nil) }
|
8
|
+
let(:resources) { stub(:resources, :last => nil) }
|
9
|
+
|
10
|
+
it "should have the Garnish::Converter module included" do
|
11
|
+
Garnish::Responder.included_modules.include?(Garnish::Converter).should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with params" do
|
15
|
+
before { responder.stub(:template => nil) }
|
16
|
+
|
17
|
+
it "should call default render" do
|
18
|
+
responder.should_receive(:default_render)
|
19
|
+
responder.to_html
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should call convert" do
|
23
|
+
responder.stub(:default_render => nil)
|
24
|
+
responder.should_receive(:convert).at_least(:once)
|
25
|
+
responder.to_html
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
data/spec/spec_helper.rb
CHANGED
@@ -7,31 +7,46 @@ require 'active_support/all'
|
|
7
7
|
require 'action_controller'
|
8
8
|
require 'garnish'
|
9
9
|
|
10
|
-
#
|
11
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
10
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
12
11
|
|
13
|
-
|
14
|
-
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.mock_with :rspec
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
16
|
+
config.filter_run :focus => true
|
17
|
+
config.run_all_when_everything_filtered = true
|
18
|
+
config.before(:all) do
|
19
|
+
require 'garnish'
|
19
20
|
end
|
20
|
-
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
+
class TestClass
|
24
|
+
def eigenclass
|
25
|
+
(class << self; self end)
|
26
|
+
end
|
23
27
|
|
24
|
-
|
28
|
+
def users
|
29
|
+
[]
|
30
|
+
end
|
25
31
|
|
26
|
-
class TestClass
|
27
32
|
def self.relations
|
28
33
|
{:users => []}
|
29
34
|
end
|
35
|
+
|
36
|
+
def self.reflections
|
37
|
+
{:users => []}
|
38
|
+
end
|
30
39
|
end
|
31
40
|
|
32
|
-
|
41
|
+
module TestClassPresenter
|
33
42
|
include Garnish::Presenter
|
34
43
|
end
|
35
44
|
|
36
45
|
class TestController < ActionController::Base
|
37
46
|
end
|
47
|
+
|
48
|
+
class TestResponder
|
49
|
+
include Garnish::Converter
|
50
|
+
|
51
|
+
attr_accessor :template
|
52
|
+
end
|
metadata
CHANGED
@@ -1,48 +1,65 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: garnish
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- brianp
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
|
18
|
+
date: 2012-02-01 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activesupport
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
22
|
-
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 7
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
- 0
|
33
|
+
version: 3.0.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
23
38
|
prerelease: false
|
24
|
-
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: rails
|
27
|
-
requirement: &70236155352360 !ruby/object:Gem::Requirement
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
28
40
|
none: false
|
29
|
-
requirements:
|
30
|
-
- -
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
33
48
|
type: :development
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
Pattern
|
38
|
-
email:
|
49
|
+
version_requirements: *id002
|
50
|
+
description: Provides an easy to use and transparent system for implementing the Decorator Pattern
|
51
|
+
email:
|
39
52
|
- brian.o.pearce@gmail.com
|
40
53
|
executables: []
|
54
|
+
|
41
55
|
extensions: []
|
56
|
+
|
42
57
|
extra_rdoc_files: []
|
43
|
-
|
58
|
+
|
59
|
+
files:
|
44
60
|
- .gitignore
|
45
61
|
- .travis.yml
|
62
|
+
- CHANGELOG.rdoc
|
46
63
|
- Gemfile
|
47
64
|
- Guardfile
|
48
65
|
- README.rdoc
|
@@ -51,42 +68,59 @@ files:
|
|
51
68
|
- lib/garnish.rb
|
52
69
|
- lib/garnish/controller.rb
|
53
70
|
- lib/garnish/converter.rb
|
71
|
+
- lib/garnish/model_adapters/abstract_adapter.rb
|
72
|
+
- lib/garnish/model_adapters/active_record_adapter.rb
|
73
|
+
- lib/garnish/model_adapters/default_adapter.rb
|
74
|
+
- lib/garnish/model_adapters/mongoid_adapter.rb
|
54
75
|
- lib/garnish/presenter.rb
|
55
76
|
- lib/garnish/presenter/relationships.rb
|
56
77
|
- lib/garnish/responder.rb
|
57
78
|
- lib/garnish/version.rb
|
79
|
+
- spec/README.rdoc
|
58
80
|
- spec/garnish/controller_spec.rb
|
59
81
|
- spec/garnish/converter_spec.rb
|
60
82
|
- spec/garnish/presenter/relationships_spec.rb
|
61
83
|
- spec/garnish/presenter_spec.rb
|
84
|
+
- spec/garnish/responder_spec.rb
|
62
85
|
- spec/spec_helper.rb
|
63
|
-
homepage:
|
86
|
+
homepage: ""
|
64
87
|
licenses: []
|
88
|
+
|
65
89
|
post_install_message:
|
66
90
|
rdoc_options: []
|
67
|
-
|
91
|
+
|
92
|
+
require_paths:
|
68
93
|
- lib
|
69
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
95
|
none: false
|
71
|
-
requirements:
|
72
|
-
- -
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
|
75
|
-
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
104
|
none: false
|
77
|
-
requirements:
|
78
|
-
- -
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
version: "0"
|
81
112
|
requirements: []
|
113
|
+
|
82
114
|
rubyforge_project: garnish
|
83
115
|
rubygems_version: 1.8.10
|
84
116
|
signing_key:
|
85
117
|
specification_version: 3
|
86
118
|
summary: Provides the decorator design pattern
|
87
|
-
test_files:
|
119
|
+
test_files:
|
120
|
+
- spec/README.rdoc
|
88
121
|
- spec/garnish/controller_spec.rb
|
89
122
|
- spec/garnish/converter_spec.rb
|
90
123
|
- spec/garnish/presenter/relationships_spec.rb
|
91
124
|
- spec/garnish/presenter_spec.rb
|
125
|
+
- spec/garnish/responder_spec.rb
|
92
126
|
- spec/spec_helper.rb
|