csv_record 1.7.0 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -15
- data/lib/csv_record/callbacks.rb +41 -3
- data/lib/csv_record/version.rb +1 -1
- data/lib/csv_record/writer.rb +10 -13
- data/test/csv_record/callbacks_test.rb +27 -1
- data/test/csv_record/validation_test.rb +21 -2
- data/test/models/callback_test_class.rb +32 -10
- metadata +2 -2
data/README.md
CHANGED
@@ -57,7 +57,7 @@ car.new_record? # checks if the record is new
|
|
57
57
|
```
|
58
58
|
|
59
59
|
##Retrieving
|
60
|
-
Records can be queried through the following
|
60
|
+
Records can be queried through the following methods:
|
61
61
|
|
62
62
|
```ruby
|
63
63
|
Car.all # retrieves all saved records
|
@@ -113,9 +113,9 @@ Extending the previous example, you can use the `has_many` method to establish t
|
|
113
113
|
class Company
|
114
114
|
include CsvRecord::Document
|
115
115
|
|
116
|
-
has_many :cars
|
117
|
-
|
118
116
|
attr_accessor :name
|
117
|
+
|
118
|
+
has_many :cars
|
119
119
|
end
|
120
120
|
|
121
121
|
company = Company.create :name => 'Chutz'
|
@@ -132,34 +132,37 @@ Callbacks can be used to execute code on predetermined moments.
|
|
132
132
|
|
133
133
|
####Usage
|
134
134
|
```ruby
|
135
|
-
after_create do
|
136
|
-
|
135
|
+
after_create do
|
136
|
+
self.do_something
|
137
137
|
end
|
138
138
|
```
|
139
|
-
`
|
139
|
+
`self` refers to the instance you are in
|
140
140
|
|
141
141
|
###Avaiable Callbacks
|
142
142
|
Here is a list with all the available callbacks, listed in the same order in which they will get called during the respective operations:
|
143
143
|
|
144
144
|
####Creating an Object
|
145
|
-
*
|
145
|
+
* after_initialize
|
146
146
|
* before_validation
|
147
147
|
* after_validation
|
148
|
+
* before_create
|
148
149
|
* after_create
|
149
150
|
|
150
151
|
####Updating an Object
|
151
152
|
* before_validation
|
152
153
|
* after_validation
|
154
|
+
* before_update
|
155
|
+
* after_update
|
153
156
|
|
154
157
|
##Validations
|
155
158
|
|
156
159
|
Helpers available:
|
157
160
|
|
158
|
-
`validates_presence_of`: Ensures if the specified
|
161
|
+
`validates_presence_of`: Ensures if the specified attribute(s) were filled
|
159
162
|
|
160
163
|
`validates_uniqueness_of`: Ensures that the specified attribute(s) are unique within the database
|
161
164
|
|
162
|
-
`validate`: Uses custom
|
165
|
+
`validate`: Uses custom method(s) to validate the model
|
163
166
|
|
164
167
|
```ruby
|
165
168
|
class Company
|
@@ -178,14 +181,11 @@ class Company
|
|
178
181
|
end
|
179
182
|
|
180
183
|
company = Company.create
|
181
|
-
company.save
|
182
|
-
# => false
|
184
|
+
company.save # => false
|
183
185
|
|
184
186
|
company = Company.create
|
185
|
-
company.valid?
|
186
|
-
# =>
|
187
|
-
company.invalid?
|
188
|
-
# => true
|
187
|
+
company.valid? # => false
|
188
|
+
company.invalid? # => true
|
189
189
|
|
190
190
|
```
|
191
191
|
|
data/lib/csv_record/callbacks.rb
CHANGED
@@ -4,10 +4,12 @@ module CsvRecord
|
|
4
4
|
:after_initialize,
|
5
5
|
:before_validation,
|
6
6
|
:after_validation,
|
7
|
-
:before_create,
|
8
7
|
:before_save,
|
8
|
+
:after_save,
|
9
|
+
:before_create,
|
9
10
|
:after_create,
|
10
|
-
:
|
11
|
+
:before_update,
|
12
|
+
:after_update
|
11
13
|
].freeze
|
12
14
|
|
13
15
|
module ClassMethods
|
@@ -27,11 +29,47 @@ module CsvRecord
|
|
27
29
|
if self.class.const_defined? const_variable
|
28
30
|
callbacks_collection = self.class.const_get("#{callback}_callbacks".upcase)
|
29
31
|
callbacks_collection.each do |callback_proc|
|
30
|
-
|
32
|
+
self.instance_eval &callback_proc
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
37
|
+
|
38
|
+
[:build, :initialize].each do |initialize_method|
|
39
|
+
define_method initialize_method do |*args|
|
40
|
+
result = super(*args)
|
41
|
+
self.run_after_initialize_callbacks
|
42
|
+
result
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid?
|
47
|
+
self.run_before_validation_callbacks
|
48
|
+
is_valid = super
|
49
|
+
self.run_after_validation_callbacks if is_valid
|
50
|
+
is_valid
|
51
|
+
end
|
52
|
+
|
53
|
+
def save(*args)
|
54
|
+
self.run_before_save_callbacks
|
55
|
+
is_saved = super
|
56
|
+
self.run_after_save_callbacks if is_saved
|
57
|
+
is_saved
|
58
|
+
end
|
59
|
+
|
60
|
+
def append_registry
|
61
|
+
self.run_before_create_callbacks
|
62
|
+
is_saved = super
|
63
|
+
self.run_after_create_callbacks if is_saved
|
64
|
+
is_saved
|
65
|
+
end
|
66
|
+
|
67
|
+
def update_registry
|
68
|
+
self.run_before_update_callbacks
|
69
|
+
saved = super
|
70
|
+
self.run_after_update_callbacks if saved
|
71
|
+
saved
|
72
|
+
end
|
35
73
|
end
|
36
74
|
|
37
75
|
def self.included(receiver)
|
data/lib/csv_record/version.rb
CHANGED
data/lib/csv_record/writer.rb
CHANGED
@@ -3,9 +3,7 @@ module CsvRecord
|
|
3
3
|
module ClassMethods
|
4
4
|
def __create__(attributes={})
|
5
5
|
instance = self.build attributes
|
6
|
-
instance.run_before_create_callbacks
|
7
6
|
result = instance.save
|
8
|
-
instance.run_after_create_callbacks if result
|
9
7
|
instance
|
10
8
|
end
|
11
9
|
|
@@ -25,16 +23,12 @@ module CsvRecord
|
|
25
23
|
receiver.send :attr_accessor, :id
|
26
24
|
end
|
27
25
|
|
28
|
-
def __save__
|
29
|
-
|
30
|
-
|
31
|
-
if self.valid?
|
32
|
-
result = self.new_record? ? self.append_registry : self.update_registry
|
26
|
+
def __save__(validate=true)
|
27
|
+
if (not validate) || self.valid?
|
28
|
+
self.new_record? ? self.append_registry : self.update_registry
|
33
29
|
else
|
34
|
-
|
30
|
+
false
|
35
31
|
end
|
36
|
-
self.run_after_validation_callbacks
|
37
|
-
result
|
38
32
|
end
|
39
33
|
|
40
34
|
def new_record?
|
@@ -43,14 +37,17 @@ module CsvRecord
|
|
43
37
|
|
44
38
|
def __update_attribute__(field, value)
|
45
39
|
self.public_send "#{field}=", value
|
46
|
-
self.save
|
40
|
+
self.save false
|
47
41
|
end
|
48
42
|
|
49
|
-
def __update_attributes__(params={})
|
43
|
+
def __update_attributes__(params={validate: true})
|
44
|
+
validate = params[:validate]
|
45
|
+
params.delete(:validate)
|
46
|
+
|
50
47
|
params.each do |field, value|
|
51
48
|
self.public_send "#{field}=", value
|
52
49
|
end
|
53
|
-
self.save
|
50
|
+
self.save validate
|
54
51
|
end
|
55
52
|
|
56
53
|
def __destroy__
|
@@ -24,10 +24,20 @@ describe CsvRecord::Callbacks do
|
|
24
24
|
it ('run before_save callbacks') { klass.must_respond_to(:run_before_save_callbacks) }
|
25
25
|
it ('run after_save callbacks') { klass.must_respond_to(:run_after_save_callbacks) }
|
26
26
|
it ('run after_initialize callbacks') { klass.must_respond_to(:run_after_initialize_callbacks) }
|
27
|
+
it ('run before_update callbacks') { klass.must_respond_to(:run_before_update_callbacks) }
|
28
|
+
it ('run after_update callbacks') { klass.must_respond_to(:run_after_update_callbacks) }
|
27
29
|
end
|
28
30
|
|
29
31
|
describe 'Checking the callbacks execution' do
|
30
32
|
let (:object_created) { CallbackTestClass.create }
|
33
|
+
let (:object_updated) do
|
34
|
+
object_created.update_attribute :sample_field, 'something'
|
35
|
+
object_created
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'after_initialize' do
|
39
|
+
CallbackTestClass.new.after_initialize_called.must_equal true
|
40
|
+
end
|
31
41
|
|
32
42
|
it 'before_create' do
|
33
43
|
object_created.before_create_called.must_equal true
|
@@ -42,7 +52,23 @@ describe CsvRecord::Callbacks do
|
|
42
52
|
end
|
43
53
|
|
44
54
|
it 'after_validation' do
|
45
|
-
object_created.
|
55
|
+
object_created.after_validation_called.must_equal true
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'before_update' do
|
59
|
+
object_updated.before_update_called.must_equal true
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'after_update' do
|
63
|
+
object_updated.after_update_called.must_equal true
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'before_save' do
|
67
|
+
object_created.before_save_called.must_equal true
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'after_update' do
|
71
|
+
object_created.after_save_called.must_equal true
|
46
72
|
end
|
47
73
|
end
|
48
74
|
end
|
@@ -4,6 +4,8 @@ require_relative '../models/jedi'
|
|
4
4
|
require_relative '../models/jedi_order'
|
5
5
|
|
6
6
|
describe CsvRecord::Validations do
|
7
|
+
let (:invalid_jedi) { Jedi.new }
|
8
|
+
|
7
9
|
describe 'initializing class methods' do
|
8
10
|
it ('responds to validates_presence_of') { Jedi.must_respond_to :validates_presence_of }
|
9
11
|
it ('responds to validates_uniqueness_of') { Jedi.must_respond_to :validates_uniqueness_of }
|
@@ -51,8 +53,6 @@ describe CsvRecord::Validations do
|
|
51
53
|
end
|
52
54
|
|
53
55
|
describe 'default methods' do
|
54
|
-
let (:invalid_jedi) { Jedi.new }
|
55
|
-
|
56
56
|
describe 'invalid?' do
|
57
57
|
it 'invalid object' do
|
58
58
|
invalid_jedi.invalid?.must_equal true
|
@@ -107,4 +107,23 @@ describe CsvRecord::Validations do
|
|
107
107
|
yoda.custom_validator_checker_with_block.must_equal true
|
108
108
|
end
|
109
109
|
end
|
110
|
+
|
111
|
+
describe 'skip validations' do
|
112
|
+
it 'on save' do
|
113
|
+
invalid_jedi.save(false)
|
114
|
+
invalid_jedi.new_record?.must_equal false
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'on update_attributes' do
|
118
|
+
yoda.save
|
119
|
+
yoda.update_attributes name: nil, validate: false
|
120
|
+
yoda.name.must_be_nil
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'update_attribute by default skips validations' do
|
124
|
+
yoda.save
|
125
|
+
yoda.update_attribute :name, nil
|
126
|
+
Jedi.last.name.must_be_nil
|
127
|
+
end
|
128
|
+
end
|
110
129
|
end
|
@@ -1,25 +1,47 @@
|
|
1
1
|
class CallbackTestClass
|
2
2
|
include CsvRecord::Document
|
3
3
|
|
4
|
-
|
5
|
-
end
|
4
|
+
attr_accessor :sample_field
|
6
5
|
|
6
|
+
attr_accessor :after_initialize_called
|
7
7
|
attr_accessor :before_create_called, :after_create_called
|
8
8
|
attr_accessor :before_validation_called, :after_validation_called
|
9
|
+
attr_accessor :before_update_called, :after_update_called
|
10
|
+
attr_accessor :before_save_called, :after_save_called
|
11
|
+
|
12
|
+
after_initialize do
|
13
|
+
self.after_initialize_called = true
|
14
|
+
end
|
15
|
+
|
16
|
+
before_create do
|
17
|
+
self.before_create_called = true
|
18
|
+
end
|
19
|
+
|
20
|
+
after_create do
|
21
|
+
self.after_create_called = true
|
22
|
+
end
|
23
|
+
|
24
|
+
before_validation do
|
25
|
+
self.before_validation_called = true
|
26
|
+
end
|
27
|
+
|
28
|
+
after_validation do
|
29
|
+
self.after_validation_called = true
|
30
|
+
end
|
9
31
|
|
10
|
-
|
11
|
-
|
32
|
+
before_update do
|
33
|
+
self.before_update_called = true
|
12
34
|
end
|
13
35
|
|
14
|
-
|
15
|
-
|
36
|
+
after_update do
|
37
|
+
self.after_update_called = true
|
16
38
|
end
|
17
39
|
|
18
|
-
|
19
|
-
|
40
|
+
before_save do
|
41
|
+
self.before_save_called = true
|
20
42
|
end
|
21
43
|
|
22
|
-
|
23
|
-
|
44
|
+
after_save do
|
45
|
+
self.after_save_called = true
|
24
46
|
end
|
25
47
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.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: 2012-
|
12
|
+
date: 2012-12-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|