couchrest_model 1.0.0.beta7 → 1.0.0.beta8

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.
@@ -23,13 +23,13 @@ describe "Model Attributes" do
23
23
  user.name.should == "will"
24
24
  user.phone.should == "555-5555"
25
25
  end
26
-
26
+
27
27
  it "should recreate from the database properly" do
28
28
  user = NoProtection.new
29
29
  user.name = "will"
30
30
  user.phone = "555-5555"
31
31
  user.save!
32
-
32
+
33
33
  user = NoProtection.get(user.id)
34
34
  user.name.should == "will"
35
35
  user.phone.should == "555-5555"
@@ -43,6 +43,8 @@ describe "Model Attributes" do
43
43
  property :admin, :default => false
44
44
  end
45
45
 
46
+ it { expect { WithAccessible.new(nil) }.to_not raise_error }
47
+
46
48
  it "should recognize accessible properties" do
47
49
  props = WithAccessible.accessible_properties.map { |prop| prop.name}
48
50
  props.should include("name")
@@ -50,7 +52,7 @@ describe "Model Attributes" do
50
52
  end
51
53
 
52
54
  it "should protect non-accessible properties set through new" do
53
- user = WithAccessible.new(:name => "will", :admin => true)
55
+ user = WithAccessible.new(:name => "will", :admin => true)
54
56
 
55
57
  user.name.should == "will"
56
58
  user.admin.should == false
@@ -72,6 +74,8 @@ describe "Model Attributes" do
72
74
  property :admin, :default => false, :protected => true
73
75
  end
74
76
 
77
+ it { expect { WithProtected.new(nil) }.to_not raise_error }
78
+
75
79
  it "should recognize protected properties" do
76
80
  props = WithProtected.protected_properties.map { |prop| prop.name}
77
81
  props.should_not include("name")
@@ -79,7 +83,7 @@ describe "Model Attributes" do
79
83
  end
80
84
 
81
85
  it "should protect non-accessible properties set through new" do
82
- user = WithProtected.new(:name => "will", :admin => true)
86
+ user = WithProtected.new(:name => "will", :admin => true)
83
87
 
84
88
  user.name.should == "will"
85
89
  user.admin.should == false
@@ -94,15 +98,23 @@ describe "Model Attributes" do
94
98
  end
95
99
  end
96
100
 
97
- describe "protected flag" do
98
- class WithBoth < CouchRest::Model::Base
101
+ describe "Model Base", "mixing protected and accessible flags" do
102
+ class WithBothAndUnspecified < CouchRest::Model::Base
99
103
  use_database TEST_SERVER.default_database
100
104
  property :name, :accessible => true
101
105
  property :admin, :default => false, :protected => true
106
+ property :phone, :default => 'unset phone number'
102
107
  end
103
108
 
104
- it "should raise an error when both are set" do
105
- lambda { WithBoth.new }.should raise_error
109
+ it { expect { WithBothAndUnspecified.new }.to_not raise_error }
110
+ it { expect { WithBothAndUnspecified.new(nil) }.to_not raise_error }
111
+
112
+ it 'should assume that any unspecified property is protected by default' do
113
+ user = WithBothAndUnspecified.new(:name => 'will', :admin => true, :phone => '555-1234')
114
+
115
+ user.name.should == 'will'
116
+ user.admin.should == false
117
+ user.phone.should == 'unset phone number'
106
118
  end
107
119
  end
108
120
 
@@ -113,7 +125,7 @@ describe "Model Attributes" do
113
125
  property :admin, :default => false, :protected => true
114
126
  view_by :name
115
127
  end
116
-
128
+
117
129
  before(:each) do
118
130
  @user = WithProtected.new
119
131
  @user.name = "will"
@@ -128,12 +140,12 @@ describe "Model Attributes" do
128
140
 
129
141
  it "Base#get should not strip protected attributes" do
130
142
  reloaded = WithProtected.get( @user.id )
131
- verify_attrs reloaded
143
+ verify_attrs reloaded
132
144
  end
133
145
 
134
146
  it "Base#get! should not strip protected attributes" do
135
147
  reloaded = WithProtected.get!( @user.id )
136
- verify_attrs reloaded
148
+ verify_attrs reloaded
137
149
  end
138
150
 
139
151
  it "Base#all should not strip protected attributes" do
@@ -141,13 +153,13 @@ describe "Model Attributes" do
141
153
  docs = WithProtected.all(:key => @user.id)
142
154
  docs.size.should == 1
143
155
  reloaded = docs.first
144
- verify_attrs reloaded
156
+ verify_attrs reloaded
145
157
  end
146
158
 
147
159
  it "views should not strip protected attributes" do
148
160
  docs = WithProtected.by_name(:startkey => "will", :endkey => "will")
149
161
  reloaded = docs.first
150
- verify_attrs reloaded
162
+ verify_attrs reloaded
151
163
  end
152
164
  end
153
165
  end
@@ -21,14 +21,14 @@ begin
21
21
  class ExtendedChild < ExtendedParent
22
22
  end
23
23
 
24
- describe "Using chained inheritance without CouchRest::ExtendedDocument" do
24
+ describe "Using chained inheritance without CouchRest::Model::Base" do
25
25
  it "should preserve inheritable attributes" do
26
26
  PlainParent.foo.should == :bar
27
27
  PlainChild.foo.should == :bar
28
28
  end
29
29
  end
30
30
 
31
- describe "Using chained inheritance with CouchRest::ExtendedDocument" do
31
+ describe "Using chained inheritance with CouchRest::Model::Base" do
32
32
  it "should preserve inheritable attributes" do
33
33
  ExtendedParent.foo.should == :bar
34
34
  ExtendedChild.foo.should == :bar
@@ -11,36 +11,36 @@ require File.join(FIXTURE_PATH, 'more', 'course')
11
11
 
12
12
 
13
13
  describe "Model properties" do
14
-
14
+
15
15
  before(:each) do
16
16
  reset_test_db!
17
17
  @card = Card.new(:first_name => "matt")
18
18
  end
19
-
19
+
20
20
  it "should be accessible from the object" do
21
21
  @card.properties.should be_an_instance_of(Array)
22
22
  @card.properties.map{|p| p.name}.should include("first_name")
23
23
  end
24
-
24
+
25
25
  it "should let you access a property value (getter)" do
26
26
  @card.first_name.should == "matt"
27
27
  end
28
-
28
+
29
29
  it "should let you set a property value (setter)" do
30
30
  @card.last_name = "Aimonetti"
31
31
  @card.last_name.should == "Aimonetti"
32
32
  end
33
-
33
+
34
34
  it "should not let you set a property value if it's read only" do
35
35
  lambda{@card.read_only_value = "test"}.should raise_error
36
36
  end
37
-
37
+
38
38
  it "should let you use an alias for an attribute" do
39
39
  @card.last_name = "Aimonetti"
40
40
  @card.family_name.should == "Aimonetti"
41
41
  @card.family_name.should == @card.last_name
42
42
  end
43
-
43
+
44
44
  it "should let you use an alias for a casted attribute" do
45
45
  @card.cast_alias = Person.new(:name => ["Aimonetti"])
46
46
  @card.cast_alias.name.should == ["Aimonetti"]
@@ -50,7 +50,7 @@ describe "Model properties" do
50
50
  card.calias.name.should == ["Aimonetti"]
51
51
  end
52
52
 
53
-
53
+
54
54
  it "should be auto timestamped" do
55
55
  @card.created_at.should be_nil
56
56
  @card.updated_at.should be_nil
@@ -59,45 +59,57 @@ describe "Model properties" do
59
59
  @card.updated_at.should_not be_nil
60
60
  end
61
61
 
62
- it "should let you use read_attribute method" do
63
- @card.last_name = "Aimonetti"
64
- @card.read_attribute(:last_name).should eql('Aimonetti')
65
- @card.read_attribute('last_name').should eql('Aimonetti')
66
- last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
67
- @card.read_attribute(last_name_prop).should eql('Aimonetti')
68
- end
62
+ describe '#read_attribute' do
63
+ it "should let you use read_attribute method" do
64
+ @card.last_name = "Aimonetti"
65
+ @card.read_attribute(:last_name).should eql('Aimonetti')
66
+ @card.read_attribute('last_name').should eql('Aimonetti')
67
+ last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
68
+ @card.read_attribute(last_name_prop).should eql('Aimonetti')
69
+ end
69
70
 
70
- it "should let you use write_attribute method" do
71
- @card.write_attribute(:last_name, 'Aimonetti 1')
72
- @card.last_name.should eql('Aimonetti 1')
73
- @card.write_attribute('last_name', 'Aimonetti 2')
74
- @card.last_name.should eql('Aimonetti 2')
75
- last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
76
- @card.write_attribute(last_name_prop, 'Aimonetti 3')
77
- @card.last_name.should eql('Aimonetti 3')
71
+ it 'should raise an error if the property does not exist' do
72
+ expect { @card.read_attribute(:this_property_should_not_exist) }.to raise_error(ArgumentError)
73
+ end
78
74
  end
79
75
 
80
- it "should let you use write_attribute on readonly properties" do
81
- lambda {
82
- @card.read_only_value = "foo"
83
- }.should raise_error
84
- @card.write_attribute(:read_only_value, "foo")
85
- @card.read_only_value.should == 'foo'
86
- end
76
+ describe '#write_attribute' do
77
+ it "should let you use write_attribute method" do
78
+ @card.write_attribute(:last_name, 'Aimonetti 1')
79
+ @card.last_name.should eql('Aimonetti 1')
80
+ @card.write_attribute('last_name', 'Aimonetti 2')
81
+ @card.last_name.should eql('Aimonetti 2')
82
+ last_name_prop = @card.properties.find{|p| p.name == 'last_name'}
83
+ @card.write_attribute(last_name_prop, 'Aimonetti 3')
84
+ @card.last_name.should eql('Aimonetti 3')
85
+ end
87
86
 
88
- it "should cast via write_attribute" do
89
- @card.write_attribute(:cast_alias, {:name => ["Sam", "Lown"]})
90
- @card.cast_alias.class.should eql(Person)
91
- @card.cast_alias.name.last.should eql("Lown")
92
- end
87
+ it 'should raise an error if the property does not exist' do
88
+ expect { @card.write_attribute(:this_property_should_not_exist, 823) }.to raise_error(ArgumentError)
89
+ end
90
+
91
+ it "should let you use write_attribute on readonly properties" do
92
+ lambda {
93
+ @card.read_only_value = "foo"
94
+ }.should raise_error
95
+ @card.write_attribute(:read_only_value, "foo")
96
+ @card.read_only_value.should == 'foo'
97
+ end
98
+
99
+ it "should cast via write_attribute" do
100
+ @card.write_attribute(:cast_alias, {:name => ["Sam", "Lown"]})
101
+ @card.cast_alias.class.should eql(Person)
102
+ @card.cast_alias.name.last.should eql("Lown")
103
+ end
93
104
 
94
- it "should not cast via write_attribute if property not casted" do
95
- @card.write_attribute(:first_name, {:name => "Sam"})
96
- @card.first_name.class.should eql(Hash)
97
- @card.first_name[:name].should eql("Sam")
105
+ it "should not cast via write_attribute if property not casted" do
106
+ @card.write_attribute(:first_name, {:name => "Sam"})
107
+ @card.first_name.class.should eql(Hash)
108
+ @card.first_name[:name].should eql("Sam")
109
+ end
98
110
  end
99
111
 
100
-
112
+
101
113
  describe "mass assignment protection" do
102
114
 
103
115
  it "should not store protected attribute using mass assignment" do
@@ -120,12 +132,12 @@ describe "Model properties" do
120
132
  end
121
133
 
122
134
  end
123
-
135
+
124
136
  describe "validation" do
125
137
  before(:each) do
126
138
  @invoice = Invoice.new(:client_name => "matt", :employee_name => "Chris", :location => "San Diego, CA")
127
139
  end
128
-
140
+
129
141
  it "should be able to be validated" do
130
142
  @card.valid?.should == true
131
143
  end
@@ -136,7 +148,7 @@ describe "Model properties" do
136
148
  @card.errors.should_not be_empty
137
149
  @card.errors[:first_name].should == ["can't be blank"]
138
150
  end
139
-
151
+
140
152
  it "should let you look up errors for a field by a string name" do
141
153
  @card.first_name = nil
142
154
  @card.should_not be_valid
@@ -150,13 +162,13 @@ describe "Model properties" do
150
162
  @invoice.errors[:client_name].should == ["can't be blank"]
151
163
  @invoice.errors[:employee_name].should_not be_empty
152
164
  end
153
-
165
+
154
166
  it "should let you set an error message" do
155
167
  @invoice.location = nil
156
168
  @invoice.valid?
157
169
  @invoice.errors[:location].should == ["Hey stupid!, you forgot the location"]
158
170
  end
159
-
171
+
160
172
  it "should validate before saving" do
161
173
  @invoice.location = nil
162
174
  @invoice.should_not be_valid
@@ -164,7 +176,7 @@ describe "Model properties" do
164
176
  @invoice.should be_new
165
177
  end
166
178
  end
167
-
179
+
168
180
  describe "casting" do
169
181
  before(:each) do
170
182
  @course = Course.new(:title => 'Relaxation')
@@ -188,14 +200,14 @@ describe "Model properties" do
188
200
  @course['started_on'].should be_an_instance_of(Date)
189
201
  end
190
202
  end
191
-
203
+
192
204
  describe "when type primitive is a String" do
193
205
  it "keeps string value unchanged" do
194
206
  value = "1.0"
195
207
  @course.title = value
196
208
  @course['title'].should equal(value)
197
209
  end
198
-
210
+
199
211
  it "it casts to string representation of the value" do
200
212
  @course.title = 1.0
201
213
  @course['title'].should eql("1.0")
@@ -584,7 +596,7 @@ describe "Model properties" do
584
596
  @course['klass'].should eql('NoClass')
585
597
  end
586
598
  end
587
-
599
+
588
600
  describe 'when type primitive is a Boolean' do
589
601
 
590
602
  [ true, 'true', 'TRUE', '1', 1, 't', 'T' ].each do |value|
@@ -609,7 +621,7 @@ describe "Model properties" do
609
621
  end
610
622
 
611
623
  it "should respond to requests with ? modifier" do
612
- @course.active = nil
624
+ @course.active = nil
613
625
  @course.active?.should be_false
614
626
  @course.active = false
615
627
  @course.active?.should be_false
@@ -618,7 +630,7 @@ describe "Model properties" do
618
630
  end
619
631
 
620
632
  it "should respond to requests with ? modifier on TrueClass" do
621
- @course.very_active = nil
633
+ @course.very_active = nil
622
634
  @course.very_active?.should be_false
623
635
  @course.very_active = false
624
636
  @course.very_active?.should be_false
@@ -631,7 +643,7 @@ describe "Model properties" do
631
643
  end
632
644
 
633
645
  describe "properties of array of casted models" do
634
-
646
+
635
647
  before(:each) do
636
648
  @course = Course.new :title => 'Test Course'
637
649
  end
@@ -691,13 +703,13 @@ describe "a casted model retrieved from the database" do
691
703
  @cat.save
692
704
  @cat = Cat.get(@cat.id)
693
705
  end
694
-
706
+
695
707
  describe "as a casted property" do
696
708
  it "should already be casted_by its parent" do
697
709
  @cat.favorite_toy.casted_by.should === @cat
698
710
  end
699
711
  end
700
-
712
+
701
713
  describe "from a casted collection" do
702
714
  it "should already be casted_by its parent" do
703
715
  @cat.toys[0].casted_by.should === @cat
@@ -789,7 +801,7 @@ describe "Property Class" do
789
801
  it "should set parent as casted_by object in CastedArray" do
790
802
  property = CouchRest::Model::Property.new(:test, [Object])
791
803
  parent = mock("FooObject")
792
- property.cast(parent, ["2010-06-01", "2010-06-02"]).casted_by.should eql(parent)
804
+ property.cast(parent, ["2010-06-01", "2010-06-02"]).casted_by.should eql(parent)
793
805
  end
794
806
 
795
807
  it "should set casted_by on new value" do
@@ -57,6 +57,7 @@ describe "Validations" do
57
57
  @obj.class.should_receive('view').and_return({'rows' => [ ]})
58
58
  @obj.valid?
59
59
  end
60
+
60
61
  end
61
62
 
62
63
  context "with a proxy parameter" do
@@ -65,6 +66,17 @@ describe "Validations" do
65
66
  proxy = @obj.should_receive('proxy').and_return(@obj.class)
66
67
  @obj.valid?.should be_true
67
68
  end
69
+
70
+ it "should allow specific view" do
71
+ @obj = WithUniqueValidationProxy.new(:title => 'test 7')
72
+ @obj.class.should_not_receive('view_by')
73
+ proxy = mock('Proxy')
74
+ @obj.should_receive('proxy').and_return(proxy)
75
+ proxy.should_receive('has_view?').and_return(true)
76
+ proxy.should_receive('view').and_return({'rows' => [ ]})
77
+ @obj.valid?
78
+ end
79
+
68
80
  end
69
81
 
70
82
 
@@ -0,0 +1,6 @@
1
+ class Client < CouchRest::Model::Base
2
+ use_database DB
3
+
4
+ property :name
5
+ property :tax_code
6
+ end
@@ -0,0 +1,9 @@
1
+ class SaleEntry < CouchRest::Model::Base
2
+ use_database DB
3
+
4
+ property :description
5
+ property :price
6
+
7
+ view_by :description
8
+
9
+ end
@@ -0,0 +1,13 @@
1
+ require File.join(FIXTURE_PATH, 'more', 'client')
2
+ require File.join(FIXTURE_PATH, 'more', 'sale_entry')
3
+ class SaleInvoice < CouchRest::Model::Base
4
+ use_database DB
5
+
6
+ belongs_to :client
7
+ belongs_to :alternate_client, :class_name => 'Client', :foreign_key => 'alt_client_id'
8
+
9
+ collection_of :entries, :class_name => 'SaleEntry'
10
+
11
+ property :date, Date
12
+ property :price, Integer
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchrest_model
3
3
  version: !ruby/object:Gem::Version
4
- hash: -1848230057
4
+ hash: -1848230060
5
5
  prerelease: true
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
9
  - 0
10
- - beta7
11
- version: 1.0.0.beta7
10
+ - beta8
11
+ version: 1.0.0.beta8
12
12
  platform: ruby
13
13
  authors:
14
14
  - J. Chris Anderson
@@ -20,7 +20,7 @@ autorequire:
20
20
  bindir: bin
21
21
  cert_chain: []
22
22
 
23
- date: 2010-06-21 00:00:00 -03:00
23
+ date: 2010-08-22 00:00:00 -03:00
24
24
  default_executable:
25
25
  dependencies:
26
26
  - !ruby/object:Gem::Dependency
@@ -29,15 +29,14 @@ dependencies:
29
29
  requirement: &id001 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
- - - ">="
32
+ - - ~>
33
33
  - !ruby/object:Gem::Version
34
- hash: 31098193
34
+ hash: 23
35
35
  segments:
36
36
  - 1
37
37
  - 0
38
38
  - 0
39
- - beta
40
- version: 1.0.0.beta
39
+ version: 1.0.0
41
40
  type: :runtime
42
41
  version_requirements: *id001
43
42
  - !ruby/object:Gem::Dependency
@@ -46,7 +45,7 @@ dependencies:
46
45
  requirement: &id002 !ruby/object:Gem::Requirement
47
46
  none: false
48
47
  requirements:
49
- - - ">="
48
+ - - ~>
50
49
  - !ruby/object:Gem::Version
51
50
  hash: 17
52
51
  segments:
@@ -61,7 +60,7 @@ dependencies:
61
60
  requirement: &id003 !ruby/object:Gem::Requirement
62
61
  none: false
63
62
  requirements:
64
- - - ">="
63
+ - - ~>
65
64
  - !ruby/object:Gem::Version
66
65
  hash: 9
67
66
  segments:
@@ -77,7 +76,7 @@ dependencies:
77
76
  requirement: &id004 !ruby/object:Gem::Requirement
78
77
  none: false
79
78
  requirements:
80
- - - ">="
79
+ - - ~>
81
80
  - !ruby/object:Gem::Version
82
81
  hash: -1848230024
83
82
  segments:
@@ -88,6 +87,22 @@ dependencies:
88
87
  version: 3.0.0.beta4
89
88
  type: :runtime
90
89
  version_requirements: *id004
90
+ - !ruby/object:Gem::Dependency
91
+ name: tzinfo
92
+ prerelease: false
93
+ requirement: &id005 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ hash: 63
99
+ segments:
100
+ - 0
101
+ - 3
102
+ - 22
103
+ version: 0.3.22
104
+ type: :runtime
105
+ version_requirements: *id005
91
106
  description: CouchRest Model provides aditional features to the standard CouchRest Document class such as properties, view designs, associations, callbacks, typecasting and validations.
92
107
  email: jchris@apache.org
93
108
  executables: []
@@ -103,7 +118,6 @@ files:
103
118
  - README.md
104
119
  - Rakefile
105
120
  - THANKS.md
106
- - examples/model/example.rb
107
121
  - history.txt
108
122
  - lib/couchrest/model.rb
109
123
  - lib/couchrest/model/associations.rb
@@ -130,7 +144,11 @@ files:
130
144
  - lib/couchrest/model/validations/locale/en.yml
131
145
  - lib/couchrest/model/validations/uniqueness.rb
132
146
  - lib/couchrest/model/views.rb
147
+ - lib/couchrest/railtie.rb
133
148
  - lib/couchrest_model.rb
149
+ - lib/rails/generators/couchrest_model.rb
150
+ - lib/rails/generators/couchrest_model/model/model_generator.rb
151
+ - lib/rails/generators/couchrest_model/model/templates/model.rb
134
152
  - spec/couchrest/assocations_spec.rb
135
153
  - spec/couchrest/attachment_spec.rb
136
154
  - spec/couchrest/attribute_protection_spec.rb
@@ -151,11 +169,14 @@ files:
151
169
  - spec/fixtures/more/article.rb
152
170
  - spec/fixtures/more/card.rb
153
171
  - spec/fixtures/more/cat.rb
172
+ - spec/fixtures/more/client.rb
154
173
  - spec/fixtures/more/course.rb
155
174
  - spec/fixtures/more/event.rb
156
175
  - spec/fixtures/more/invoice.rb
157
176
  - spec/fixtures/more/person.rb
158
177
  - spec/fixtures/more/question.rb
178
+ - spec/fixtures/more/sale_entry.rb
179
+ - spec/fixtures/more/sale_invoice.rb
159
180
  - spec/fixtures/more/service.rb
160
181
  - spec/fixtures/more/user.rb
161
182
  - spec/fixtures/views/lib.js
@@ -165,8 +186,6 @@ files:
165
186
  - spec/fixtures/views/test_view/test-reduce.js
166
187
  - spec/spec.opts
167
188
  - spec/spec_helper.rb
168
- - utils/remap.rb
169
- - utils/subset.rb
170
189
  has_rdoc: true
171
190
  homepage: http://github.com/couchrest/couchrest_model
172
191
  licenses: []
@@ -221,12 +240,14 @@ test_files:
221
240
  - spec/fixtures/more/article.rb
222
241
  - spec/fixtures/more/card.rb
223
242
  - spec/fixtures/more/cat.rb
243
+ - spec/fixtures/more/client.rb
224
244
  - spec/fixtures/more/course.rb
225
245
  - spec/fixtures/more/event.rb
226
246
  - spec/fixtures/more/invoice.rb
227
247
  - spec/fixtures/more/person.rb
228
248
  - spec/fixtures/more/question.rb
249
+ - spec/fixtures/more/sale_entry.rb
250
+ - spec/fixtures/more/sale_invoice.rb
229
251
  - spec/fixtures/more/service.rb
230
252
  - spec/fixtures/more/user.rb
231
253
  - spec/spec_helper.rb
232
- - examples/model/example.rb