/
presenter_spec.rb
171 lines (131 loc) · 5 KB
/
presenter_spec.rb
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require 'spec_helper'
class Musician # pretend that this is an AR model
attr_accessor :id, :name, :rating, :errors, :birthplace_id, :alive
def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
def attributes; {:id => self.id, :name => self.name, :rating => self.rating, :alive => self.alive}; end
def reload; self; end
end
class Birthplace # another make-believe AR model
attr_accessor :id, :city, :state
def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
def attributes; {:id => self.id, :city => self.city, :state => self.state}; end
def reload; self; end
end
class Album # and yet another make-believe AR model
attr_accessor :id, :name
def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
def attributes; {:id => self.id, :name => self.name}; end
def reload; self; end
end
class AlbumTrack
attr_accessor :id, :title
def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
def attributes; {:id => self.id, :title => self.name}; end
def reload; self; end
end
module MyApi
class Birthplace
include Faceted::Presenter
presents :birthplace
field :city
field :state
end
class Musician
include Faceted::Presenter
presents :musician
field :name
field :rating
field :birthplace_id
field :alive
end
class Album
include Faceted::Presenter
presents :album, :find_by => :name
field :name
end
class AlbumTrack
include Faceted::Presenter
presents :album_track
field :title
end
describe Musician do
before do
@ar_musician = ::Musician.new(:id => 1, :name => 'Johnny Cash', :rating => 'Good', :alive => false)
::Musician.stub(:where) { [@ar_musician] }
@ar_birthplace = ::Birthplace.new(:id => 1, :city => 'Kingsland', :state => 'Arkansas')
::Birthplace.stub(:where) { [@ar_birthplace] }
end
describe 'initialized with an instantiated object' do
let(:musician_presenter) { MyApi::Musician.from(@ar_musician) }
it 'accepts an object' do
musician_presenter.send(:object).should == @ar_musician
end
it 'initializes with the attributes of the object' do
musician_presenter.name.should == 'Johnny Cash'
end
end
describe 'initialized with a presented object' do
describe 'inherits values from its AR counterpart' do
it 'normal values' do
musician = MyApi::Musician.new(:id => 1)
musician.name.should == 'Johnny Cash'
end
it 'boolean values' do
musician = MyApi::Musician.new(:id => 1)
musician.alive.should be_false
musician.alive.should_not be_nil
end
it 'excludes fields' do
musician = MyApi::Musician.new(:id => 1, :excludes => [:rating])
musician.schema_fields.include?(:rating).should be_false
end
it 'excludes relations' do
musician = MyApi::Musician.new(:id => 1, :excludes => [:birthplace])
musician.schema_fields.include?(:birthplace).should be_false
end
end
it 'overwrites values from its AR counterpart' do
musician = MyApi::Musician.new(:id => 1, :rating => 'Great')
musician.rating.should == 'Great'
end
describe 'saves its counterpart' do
it 'successfully' do
musician = MyApi::Musician.new(:id => 1)
@ar_musician.should_receive(:save) { true }
musician.save.should be_true
end
it 'handling failure' do
musician = MyApi::Musician.new(:id => 1)
@ar_musician.should_receive(:save) { false }
musician.save.should be_false
end
it 'failing and populating its errors' do
musician = MyApi::Musician.new(:id => 1)
@ar_musician.should_receive(:save) { false }
@ar_musician.stub_chain(:errors, :full_messages) { ["Something went wrong", "Terribly wrong"] }
musician.save
musician.errors.count.should == 2
musician.errors.last.should == "Terribly wrong"
end
end
end
describe 'with an associated object' do
it 'initializes the associated object in the correct namespace' do
musician = MyApi::Musician.new(:id => 1, :birthplace_id => 1)
musician.birthplace.city.should == 'Kingsland'
end
it 'initializes the associated object finding by a specified key' do
@ar_album = ::Album.new(:id => 1, :name => 'Greatest Hits')
::Album.stub(:where) { [@ar_album] }
album = MyApi::Album.new(:name => 'Greatest Hits')
album.id.should == 1
end
it 'does not choke on associated objects with underscores in their names' do
@ar_album_track = ::AlbumTrack.new(:id => 1, :title => 'The Gambler')
::AlbumTrack.stub(:where) { [@ar_album_track] }
track = MyApi::AlbumTrack.new(:id => 1)
track.album_track.should == @ar_album_track
end
end
end
end