yeti 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/yeti/editor.rb +18 -20
- data/lib/yeti/version.rb +1 -1
- data/lib/yeti/viewer.rb +21 -0
- data/lib/yeti.rb +1 -0
- data/spec/yeti/editor_spec.rb +91 -62
- data/spec/yeti/viewer_spec.rb +40 -0
- metadata +6 -9
data/lib/yeti/editor.rb
CHANGED
@@ -6,32 +6,32 @@ module Yeti
|
|
6
6
|
attr_reader :context
|
7
7
|
delegate :id, :to_param, :persisted?, to: :edited
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
@given_id = given_id
|
9
|
+
def self.from_id(context, id)
|
10
|
+
new context, (find_by_id id if id)
|
12
11
|
end
|
13
12
|
|
14
|
-
def
|
15
|
-
|
16
|
-
self,
|
17
|
-
untranslated: self.class.untranslated?
|
18
|
-
)
|
13
|
+
def self.find_by_id(id)
|
14
|
+
raise NotImplementedError, "#{inspect}.find_by_id"
|
19
15
|
end
|
20
16
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
17
|
+
def self.new_object
|
18
|
+
raise NotImplementedError, "#{inspect}.new_object"
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(context, edited=nil)
|
22
|
+
@context = context
|
23
|
+
@edited = edited
|
27
24
|
end
|
28
25
|
|
29
|
-
def
|
30
|
-
|
26
|
+
def edited
|
27
|
+
@edited ||= self.class.new_object
|
31
28
|
end
|
32
29
|
|
33
|
-
def
|
34
|
-
|
30
|
+
def errors
|
31
|
+
@errors ||= ::Yeti::Errors.new(
|
32
|
+
self,
|
33
|
+
untranslated: self.class.untranslated?
|
34
|
+
)
|
35
35
|
end
|
36
36
|
|
37
37
|
def update_attributes(attrs)
|
@@ -79,8 +79,6 @@ module Yeti
|
|
79
79
|
|
80
80
|
private
|
81
81
|
|
82
|
-
attr_reader :given_id
|
83
|
-
|
84
82
|
def self.attribute(name, opts={})
|
85
83
|
opts[:attribute_name] = name
|
86
84
|
opts[:from] = :edited unless opts.has_key? :from
|
data/lib/yeti/version.rb
CHANGED
data/lib/yeti/viewer.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Yeti
|
2
|
+
class Viewer
|
3
|
+
|
4
|
+
attr_reader :context, :decorated
|
5
|
+
delegate :id, :to_param, :persisted?, to: :decorated
|
6
|
+
|
7
|
+
def self.from_id(context, id)
|
8
|
+
new context, (find_by_id id if id)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.find_by_id(id)
|
12
|
+
raise NotImplementedError, "#{inspect}.find_by_id"
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(context, decorated)
|
16
|
+
@context = context
|
17
|
+
@decorated = decorated
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/yeti.rb
CHANGED
data/spec/yeti/editor_spec.rb
CHANGED
@@ -1,61 +1,95 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Yeti::Editor do
|
3
|
+
describe ::Yeti::Editor do
|
4
4
|
let(:context){ mock :context }
|
5
|
-
subject{
|
6
|
-
it "
|
7
|
-
|
5
|
+
subject{ described_class.new context }
|
6
|
+
it ".new_object is virtual" do
|
7
|
+
lambda do
|
8
|
+
described_class.new_object
|
9
|
+
end.should raise_error NotImplementedError, "Yeti::Editor.new_object"
|
8
10
|
end
|
9
|
-
it "
|
10
|
-
lambda
|
11
|
+
it ".find_by_id is virtual" do
|
12
|
+
lambda do
|
13
|
+
described_class.find_by_id 1
|
14
|
+
end.should raise_error NotImplementedError, "Yeti::Editor.find_by_id"
|
11
15
|
end
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
16
|
+
describe "initialization" do
|
17
|
+
let(:new_object){ mock :new_object }
|
18
|
+
let(:existing_object){ mock :existing_object }
|
19
|
+
context "with context only" do
|
20
|
+
before{ described_class.stub(:new_object).and_return new_object }
|
21
|
+
it "keeps given context" do
|
22
|
+
subject.context.should be context
|
23
|
+
end
|
24
|
+
it "#persist! is virtual" do
|
25
|
+
lambda do
|
26
|
+
subject.persist!
|
27
|
+
end.should raise_error NotImplementedError, "Yeti::Editor#persist!"
|
28
|
+
end
|
29
|
+
it "uses .new_object to initialize edited object" do
|
30
|
+
subject.edited.should == new_object
|
31
|
+
end
|
32
|
+
it "delegates id to edited object" do
|
33
|
+
should delegates(:id).to :new_object
|
34
|
+
end
|
35
|
+
it "delegates to_param to edited object" do
|
36
|
+
should delegates(:to_param).to :new_object
|
37
|
+
end
|
38
|
+
it "delegates persisted? edited object" do
|
39
|
+
should delegates(:persisted?).to :new_object
|
40
|
+
end
|
36
41
|
end
|
37
|
-
|
38
|
-
|
39
|
-
|
42
|
+
context "initialize with context and object to edit" do
|
43
|
+
subject{ described_class.new context, existing_object }
|
44
|
+
it "keeps given context" do
|
45
|
+
subject.context.should be context
|
46
|
+
end
|
47
|
+
it "#persist! is virtual" do
|
48
|
+
lambda do
|
49
|
+
subject.persist!
|
50
|
+
end.should raise_error NotImplementedError, "Yeti::Editor#persist!"
|
51
|
+
end
|
52
|
+
it "delegates id to edited object" do
|
53
|
+
should delegates(:id).to :existing_object
|
54
|
+
end
|
55
|
+
it "delegates to_param to edited object" do
|
56
|
+
should delegates(:to_param).to :existing_object
|
57
|
+
end
|
58
|
+
it "delegates persisted? edited object" do
|
59
|
+
should delegates(:persisted?).to :existing_object
|
60
|
+
end
|
40
61
|
end
|
41
62
|
end
|
42
|
-
|
43
|
-
let(:
|
44
|
-
|
45
|
-
|
46
|
-
|
63
|
+
describe ".from_id(context, given_id)" do
|
64
|
+
let(:new_object){ mock :new_object }
|
65
|
+
let(:existing_object){ mock :existing_object, id: 1 }
|
66
|
+
subject{ described_class.from_id context, given_id }
|
67
|
+
context "when given_id is nil" do
|
68
|
+
let(:given_id){ nil }
|
69
|
+
it "uses .new_object to generate object to edit" do
|
70
|
+
described_class.should_receive(:new_object).and_return new_object
|
71
|
+
subject.edited.should be new_object
|
72
|
+
end
|
47
73
|
end
|
48
|
-
|
49
|
-
|
50
|
-
|
74
|
+
context "when given_id is not nil" do
|
75
|
+
let(:given_id){ "1" }
|
76
|
+
it "uses .find_by_id to find object to edit" do
|
77
|
+
described_class.should_receive(:find_by_id).with("1").and_return do
|
78
|
+
existing_object
|
79
|
+
end
|
80
|
+
subject.edited.should be existing_object
|
81
|
+
subject.id.should be 1
|
82
|
+
end
|
51
83
|
end
|
52
84
|
end
|
53
85
|
context "when not valid" do
|
54
|
-
before{ subject.stub(:valid?).and_return false }
|
55
86
|
it "#save returns false" do
|
87
|
+
subject.should_receive(:valid?).and_return false
|
88
|
+
subject.should_not_receive :persist!
|
56
89
|
subject.save.should be false
|
57
90
|
end
|
58
91
|
it "#save(validate: false) calls persist! then returns true" do
|
92
|
+
subject.should_not_receive :valid?
|
59
93
|
subject.should_receive :persist!
|
60
94
|
subject.save(validate: false).should be true
|
61
95
|
end
|
@@ -67,19 +101,19 @@ describe Yeti::Editor do
|
|
67
101
|
end
|
68
102
|
end
|
69
103
|
context "editor of one record" do
|
70
|
-
let :
|
71
|
-
Class.new Yeti::Editor do
|
104
|
+
let :described_class do
|
105
|
+
Class.new ::Yeti::Editor do
|
72
106
|
attribute :name
|
73
107
|
validates_presence_of :name
|
74
108
|
def self.name
|
75
109
|
"ObjectEditor"
|
76
110
|
end
|
111
|
+
def self.new_object
|
112
|
+
Struct.new(:id, :name).new nil, nil
|
113
|
+
end
|
77
114
|
end
|
78
115
|
end
|
79
116
|
context "new record" do
|
80
|
-
subject{ editor_class.new context, nil }
|
81
|
-
let(:new_record){ mock :new_record, name: nil, id: nil }
|
82
|
-
before{ subject.stub(:new_object).and_return new_record }
|
83
117
|
its(:id){ should be_nil }
|
84
118
|
its(:name){ should be_nil }
|
85
119
|
it "#name= converts input to string" do
|
@@ -120,7 +154,7 @@ describe Yeti::Editor do
|
|
120
154
|
subject.should_not be_without_error
|
121
155
|
end
|
122
156
|
it "can return untranslated error messages" do
|
123
|
-
|
157
|
+
described_class.class_eval do
|
124
158
|
dont_translate_error_messages
|
125
159
|
end
|
126
160
|
subject.valid?
|
@@ -158,9 +192,8 @@ describe Yeti::Editor do
|
|
158
192
|
end
|
159
193
|
end
|
160
194
|
context "existing record" do
|
161
|
-
|
162
|
-
|
163
|
-
before{ subject.stub(:find_by_id).with(1).and_return existing_record }
|
195
|
+
let(:existing_object){ mock :existing_object, id: 1, name: "Anthony" }
|
196
|
+
subject{ described_class.new context, existing_object }
|
164
197
|
it("gets id from record"){ subject.id.should be 1 }
|
165
198
|
it "gets name from record" do
|
166
199
|
subject.name.should == "Anthony"
|
@@ -193,26 +226,23 @@ describe Yeti::Editor do
|
|
193
226
|
end
|
194
227
|
end
|
195
228
|
context "editor of multiple records" do
|
196
|
-
let :
|
197
|
-
|
229
|
+
let(:existing_object){ Struct.new(:id, :name).new 1, "Anthony" }
|
230
|
+
subject do
|
231
|
+
Class.new described_class do
|
198
232
|
attribute :name
|
199
233
|
attribute :description, from: :related
|
200
234
|
attribute :password, from: nil
|
201
235
|
attribute :timestamp, from: ".timestamp_str"
|
202
236
|
attribute :related_id, from: "related.id"
|
203
237
|
attribute :invalid
|
204
|
-
def find_by_id(id)
|
205
|
-
Struct.new(:id, :name).new(id, "Anthony")
|
206
|
-
end
|
207
238
|
def related
|
208
239
|
Struct.new(:id, :description).new 2, "Business man"
|
209
240
|
end
|
210
241
|
def timestamp_str
|
211
242
|
"2001-01-01"
|
212
243
|
end
|
213
|
-
end
|
244
|
+
end.new context, existing_object
|
214
245
|
end
|
215
|
-
subject{ editor_class.new context, 1 }
|
216
246
|
it "attribute default value comes from edited" do
|
217
247
|
subject.id.should == 1
|
218
248
|
subject.name.should == "Anthony"
|
@@ -234,14 +264,13 @@ describe Yeti::Editor do
|
|
234
264
|
end
|
235
265
|
end
|
236
266
|
describe "#mandatory?" do
|
237
|
-
|
238
|
-
Class.new
|
267
|
+
subject do
|
268
|
+
Class.new described_class do
|
239
269
|
validates_presence_of :name
|
240
270
|
attribute :name
|
241
271
|
attribute :password
|
242
|
-
end
|
272
|
+
end.new context
|
243
273
|
end
|
244
|
-
subject{ editor_class.new context }
|
245
274
|
it "is true for an attribute with validates_presence_of" do
|
246
275
|
subject.mandatory?(:name).should be true
|
247
276
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ::Yeti::Viewer do
|
4
|
+
let(:context){ mock :context }
|
5
|
+
subject{ described_class.new context }
|
6
|
+
it ".find_by_id is virtual" do
|
7
|
+
lambda do
|
8
|
+
described_class.find_by_id 1
|
9
|
+
end.should raise_error NotImplementedError, "Yeti::Viewer.find_by_id"
|
10
|
+
end
|
11
|
+
describe "initialization" do
|
12
|
+
let(:existing_object){ mock :existing_object }
|
13
|
+
subject{ described_class.new context, existing_object }
|
14
|
+
it "keeps given context" do
|
15
|
+
subject.context.should be context
|
16
|
+
end
|
17
|
+
it "keeps given object to decorate" do
|
18
|
+
subject.decorated.should be existing_object
|
19
|
+
end
|
20
|
+
it "delegates id to decorated object" do
|
21
|
+
should delegates(:id).to :existing_object
|
22
|
+
end
|
23
|
+
it "delegates to_param to decorated object" do
|
24
|
+
should delegates(:to_param).to :existing_object
|
25
|
+
end
|
26
|
+
it "delegates persisted? decorated object" do
|
27
|
+
should delegates(:persisted?).to :existing_object
|
28
|
+
end
|
29
|
+
end
|
30
|
+
describe ".from_id(context, given_id)" do
|
31
|
+
let(:existing_object){ mock :existing_object }
|
32
|
+
subject{ described_class.from_id context, "1" }
|
33
|
+
it "uses .find_by_id to find object to edit" do
|
34
|
+
described_class.should_receive(:find_by_id).with("1").and_return do
|
35
|
+
existing_object
|
36
|
+
end
|
37
|
+
subject.decorated.should be existing_object
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yeti
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.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: 2012-12-
|
12
|
+
date: 2012-12-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -93,11 +93,13 @@ files:
|
|
93
93
|
- lib/yeti/errors.rb
|
94
94
|
- lib/yeti/search.rb
|
95
95
|
- lib/yeti/version.rb
|
96
|
+
- lib/yeti/viewer.rb
|
96
97
|
- spec/spec_helper.rb
|
97
98
|
- spec/support/matchers.rb
|
98
99
|
- spec/yeti/context_spec.rb
|
99
100
|
- spec/yeti/editor_spec.rb
|
100
101
|
- spec/yeti/search_spec.rb
|
102
|
+
- spec/yeti/viewer_spec.rb
|
101
103
|
- spec/yeti_spec.rb
|
102
104
|
- yeti.gemspec
|
103
105
|
homepage: ''
|
@@ -112,21 +114,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
114
|
- - ! '>='
|
113
115
|
- !ruby/object:Gem::Version
|
114
116
|
version: '0'
|
115
|
-
segments:
|
116
|
-
- 0
|
117
|
-
hash: 3787282194085503627
|
118
117
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
118
|
none: false
|
120
119
|
requirements:
|
121
120
|
- - ! '>='
|
122
121
|
- !ruby/object:Gem::Version
|
123
122
|
version: '0'
|
124
|
-
segments:
|
125
|
-
- 0
|
126
|
-
hash: 3787282194085503627
|
127
123
|
requirements: []
|
128
124
|
rubyforge_project:
|
129
|
-
rubygems_version: 1.8.
|
125
|
+
rubygems_version: 1.8.24
|
130
126
|
signing_key:
|
131
127
|
specification_version: 3
|
132
128
|
summary: Editor pattern simplifies edition of multiple objects at once using ActiveModel
|
@@ -136,4 +132,5 @@ test_files:
|
|
136
132
|
- spec/yeti/context_spec.rb
|
137
133
|
- spec/yeti/editor_spec.rb
|
138
134
|
- spec/yeti/search_spec.rb
|
135
|
+
- spec/yeti/viewer_spec.rb
|
139
136
|
- spec/yeti_spec.rb
|