default_value_for 3.5.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +48 -21
- data/Rakefile +1 -9
- data/default_value_for.gemspec +7 -6
- data/test.rb +28 -8
- metadata +31 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e4ad2e33ddca870f51f8a1c4aef12281b2cc0fe679834eaebb33f6691434431
|
4
|
+
data.tar.gz: 862a55e06fbca022177db11193eca2572174891375510010a7e6e074a870e9e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d24e7cded2995ee32877835bc8ff4eb8eccad4ee68447d5c66c869932d61fe6ef50807b88a14e64f93f8dcd9d4cb6b85fd1730f490e2266c9ffc16ca0a57054c
|
7
|
+
data.tar.gz: '050180f430ef62cdf88390ebe32adad62b862dbc6f7000c82ae73a286a571f6159035c258f1488864fe5f9507524c33124dd4aa633ae55210194eca0abc866f6'
|
data/README.md
CHANGED
@@ -20,21 +20,35 @@ u.last_seen # => Mon Sep 22 17:28:38 +0200 2008
|
|
20
20
|
|
21
21
|
## Installation
|
22
22
|
|
23
|
-
### Rails
|
23
|
+
### On Rails 6.1+ / Ruby 3.0+
|
24
24
|
|
25
|
-
The
|
25
|
+
The objective is to support all supported rubies and rails versions. If you need to support end of life versions, please see the sections below and use a prior release.
|
26
26
|
|
27
27
|
Add it to your Gemfile:
|
28
28
|
|
29
29
|
```ruby
|
30
|
-
gem "default_value_for", "~>
|
30
|
+
gem "default_value_for", "~> 4.0"
|
31
31
|
```
|
32
32
|
|
33
|
-
|
33
|
+
Version 4.0.0 supports rails 6.1+ and ruby 3.0+. It adds support for rails 7.2. It drops support for rubies less than 3.0 and rails less than 6.1. This is the reason for the major version bump as it drops support for many combinations of older ruby and rails versions.
|
34
|
+
|
35
|
+
### On Rails 3.2 - 6.0 / Ruby 1.9.3 - 2.7
|
36
|
+
|
37
|
+
To use default_value_for with older versions of Ruby and Rails, you must use the previous stable release, 3.6.0 or others in the 3.x.y release. This version works with Rails 3.0, 3.1, and 3.2; and Ruby 1.8.7 and higher. It **does not** work with Rails 4.
|
38
|
+
|
39
|
+
default_value_for (3.x+) is compatible with Rails 3.2-6.1, and Ruby 1.9.3 and higher.
|
40
|
+
|
41
|
+
Note:
|
42
|
+
* Version 3.5.0: Added basic rails 7.0 support
|
43
|
+
* Version 3.6.0: Added basic rails 7.1 support
|
34
44
|
|
35
|
-
|
45
|
+
Add it to your Gemfile:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
gem "default_value_for", "~> 3.0"
|
49
|
+
```
|
36
50
|
|
37
|
-
## Rails 3.0 - 3.1 / Ruby 1.9.3 and lower
|
51
|
+
## On Rails 3.0 - 3.1 / Ruby 1.9.3 and lower
|
38
52
|
|
39
53
|
To use default_value_for with older versions of Ruby and Rails, you must use the previous stable release, 2.0.3. This version works with Rails 3.0, 3.1, and 3.2; and Ruby 1.8.7 and higher. It **does not** work with Rails 4.
|
40
54
|
|
@@ -42,7 +56,7 @@ To use default_value_for with older versions of Ruby and Rails, you must use the
|
|
42
56
|
gem "default_value_for", "~> 2.0.3"
|
43
57
|
```
|
44
58
|
|
45
|
-
### Rails 2
|
59
|
+
### On Rails 2
|
46
60
|
|
47
61
|
To use default_value_for with Rails 2.x you must use an older version:
|
48
62
|
|
@@ -346,25 +360,30 @@ class User < ActiveRecord::Base
|
|
346
360
|
end
|
347
361
|
```
|
348
362
|
|
349
|
-
We recommend you to
|
363
|
+
We recommend you to use Module#prepend in models where you use `default_value_for`:
|
350
364
|
|
351
365
|
```ruby
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
366
|
+
module UserCustomInitialize
|
367
|
+
def initialize
|
368
|
+
# Do your pre-initialize work
|
369
|
+
super
|
370
|
+
# Do any post-initialize work
|
357
371
|
end
|
372
|
+
end
|
373
|
+
```
|
358
374
|
|
359
|
-
|
375
|
+
```ruby
|
376
|
+
class User < ActiveRecord::Base
|
377
|
+
default_value_for :age, 20
|
378
|
+
prepend UserCustomInitialize
|
360
379
|
end
|
361
380
|
```
|
362
381
|
|
363
|
-
Also,
|
382
|
+
Also, take the following precautions:
|
364
383
|
|
365
|
-
*
|
384
|
+
* Make sure you always call super in your prepended method.
|
366
385
|
|
367
|
-
*
|
386
|
+
* It's generally safer to call super first and do your customizations afterwards to ensure everything is setup first.
|
368
387
|
|
369
388
|
If your default value is accidentally similar to default_value_for's options hash wrap your default value like this:
|
370
389
|
|
@@ -537,13 +556,21 @@ That's an awful lot of code. Using `default_value_for` is easier, don't you thin
|
|
537
556
|
|
538
557
|
### What about other plugins?
|
539
558
|
|
540
|
-
|
559
|
+
There are other ways to accomplish similar results as `default_value_for`.
|
560
|
+
|
561
|
+
From Rails:
|
562
|
+
* [attribute default](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html#method-i-attribute)
|
563
|
+
* [schema migration column defaults](https://edgeguides.rubyonrails.org/active_record_migrations.html)
|
541
564
|
|
542
|
-
|
565
|
+
Other plugins:
|
566
|
+
* [Defaults](https://github.com/fnando/defaults)
|
567
|
+
* [attribute-defaults](https://github.com/bsm/attribute-defaults)
|
568
|
+
* [has_defaults](https://github.com/makandra/has_defaults)
|
543
569
|
|
544
|
-
|
570
|
+
Each of these has limitations and should be evaluated based on your use cases. Two areas where `default_value_for` excels is deriving
|
571
|
+
defaults based on the current object or determining the behavior for `nil` values.
|
545
572
|
|
546
|
-
|
573
|
+
If there are other options or uses cases where you've found this gem or another one is useful, please open a pull request to update this page.
|
547
574
|
|
548
575
|
I've taken my time to thoroughly document default_value_for's behavior.
|
549
576
|
|
data/Rakefile
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
task :default => :test
|
2
2
|
|
3
|
-
require 'bundler'
|
4
3
|
require "bundler/gem_tasks"
|
5
4
|
|
6
5
|
desc "Run unit tests."
|
@@ -9,16 +8,9 @@ task :test do
|
|
9
8
|
end
|
10
9
|
|
11
10
|
rails_versions = %w(
|
12
|
-
3.2
|
13
|
-
4.0
|
14
|
-
4.1
|
15
|
-
4.2
|
16
|
-
5.0
|
17
|
-
5.1
|
18
|
-
5.2
|
19
|
-
6.0
|
20
11
|
6.1
|
21
12
|
7.0
|
13
|
+
7.1
|
22
14
|
)
|
23
15
|
|
24
16
|
rails_versions.each do |version|
|
data/default_value_for.gemspec
CHANGED
@@ -1,23 +1,24 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{default_value_for}
|
3
|
-
s.version = "
|
3
|
+
s.version = "4.0.0"
|
4
4
|
s.summary = %q{Provides a way to specify default values for ActiveRecord models}
|
5
5
|
s.description = %q{The default_value_for plugin allows one to define default values for ActiveRecord models in a declarative manner}
|
6
6
|
s.email = %q{software-signing@phusion.nl}
|
7
7
|
s.homepage = %q{https://github.com/FooBarWidget/default_value_for}
|
8
8
|
s.authors = ["Hongli Lai"]
|
9
9
|
s.license = 'MIT'
|
10
|
-
s.required_ruby_version = '>=
|
10
|
+
s.required_ruby_version = '>= 3.0.0'
|
11
11
|
s.files = ['default_value_for.gemspec',
|
12
12
|
'LICENSE.TXT', 'Rakefile', 'README.md', 'test.rb',
|
13
13
|
'init.rb',
|
14
14
|
'lib/default_value_for.rb',
|
15
15
|
'lib/default_value_for/railtie.rb'
|
16
16
|
]
|
17
|
-
s.add_dependency 'activerecord', '>=
|
18
|
-
s.add_development_dependency 'actionpack', '>=
|
19
|
-
s.add_development_dependency 'railties', '>=
|
20
|
-
s.add_development_dependency 'minitest', '>=
|
17
|
+
s.add_dependency 'activerecord', '>= 6.1', '< 7.3'
|
18
|
+
s.add_development_dependency 'actionpack', '>= 6.1', '< 7.3'
|
19
|
+
s.add_development_dependency 'railties', '>= 6.1', '< 7.3'
|
20
|
+
s.add_development_dependency 'minitest', '>= 5.0'
|
21
21
|
s.add_development_dependency 'minitest-around'
|
22
22
|
s.add_development_dependency 'appraisal'
|
23
|
+
s.add_development_dependency 'bundler'
|
23
24
|
end
|
data/test.rb
CHANGED
@@ -60,18 +60,19 @@ if rails_2_4_or_newer && arel_older_than_7_1
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
-
begin
|
64
|
-
TestCaseClass = MiniTest::Test
|
65
|
-
rescue NameError
|
66
|
-
TestCaseClass = MiniTest::Unit::TestCase
|
67
|
-
end
|
68
|
-
|
69
63
|
require 'default_value_for'
|
70
64
|
|
65
|
+
puts "\nTesting with ruby version #{RUBY_VERSION}"
|
71
66
|
puts "\nTesting with Active Record version #{ActiveRecord::VERSION::STRING}"
|
72
67
|
puts "\nTesting with Action Pack version #{ActionPack::VERSION::STRING}\n\n"
|
73
68
|
|
74
|
-
ActiveRecord
|
69
|
+
if ActiveRecord.respond_to?(:default_timezone)
|
70
|
+
ActiveRecord.default_timezone = :local
|
71
|
+
else
|
72
|
+
# Deprecated in rails 7.0, removed in 7.1
|
73
|
+
ActiveRecord::Base.default_timezone = :local
|
74
|
+
end
|
75
|
+
|
75
76
|
ActiveRecord::Base.logger = Logger.new(STDERR)
|
76
77
|
ActiveRecord::Base.logger.level = Logger::WARN
|
77
78
|
|
@@ -101,7 +102,7 @@ if defined?(Rails::Railtie)
|
|
101
102
|
DefaultValueFor.initialize_active_record_extensions
|
102
103
|
end
|
103
104
|
|
104
|
-
class DefaultValuePluginTest <
|
105
|
+
class DefaultValuePluginTest < Minitest::Test
|
105
106
|
def around
|
106
107
|
Object.const_set(:User, Class.new(ActiveRecord::Base))
|
107
108
|
Object.const_set(:Book, Class.new(ActiveRecord::Base))
|
@@ -291,6 +292,25 @@ class DefaultValuePluginTest < TestCaseClass
|
|
291
292
|
assert object.instance_variable_get('@initialized')
|
292
293
|
end
|
293
294
|
|
295
|
+
def test_doesnt_conflict_with_prependeed_initialize_method_in_model_class
|
296
|
+
m = Object.const_set("BookOverride", Module.new)
|
297
|
+
m.define_method(:initialize) do |*args|
|
298
|
+
@initialized = true
|
299
|
+
super(:count => 5678)
|
300
|
+
end
|
301
|
+
|
302
|
+
Book.class_eval do
|
303
|
+
prepend BookOverride
|
304
|
+
|
305
|
+
default_value_for :number, 1234
|
306
|
+
end
|
307
|
+
|
308
|
+
object = Book.new
|
309
|
+
assert_equal 1234, object.number
|
310
|
+
assert_equal 5678, object.count
|
311
|
+
assert object.instance_variable_get('@initialized')
|
312
|
+
end
|
313
|
+
|
294
314
|
def test_model_instance_is_passed_to_the_given_block
|
295
315
|
instance = nil
|
296
316
|
Book.default_value_for :number do |n|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: default_value_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hongli Lai
|
@@ -16,74 +16,74 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '6.1'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '7.
|
22
|
+
version: '7.3'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: '6.1'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '7.
|
32
|
+
version: '7.3'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: actionpack
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
39
|
+
version: '6.1'
|
40
40
|
- - "<"
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: '7.
|
42
|
+
version: '7.3'
|
43
43
|
type: :development
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
47
|
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version:
|
49
|
+
version: '6.1'
|
50
50
|
- - "<"
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version: '7.
|
52
|
+
version: '7.3'
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: railties
|
55
55
|
requirement: !ruby/object:Gem::Requirement
|
56
56
|
requirements:
|
57
57
|
- - ">="
|
58
58
|
- !ruby/object:Gem::Version
|
59
|
-
version:
|
59
|
+
version: '6.1'
|
60
60
|
- - "<"
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version: '7.
|
62
|
+
version: '7.3'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
67
|
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
69
|
+
version: '6.1'
|
70
70
|
- - "<"
|
71
71
|
- !ruby/object:Gem::Version
|
72
|
-
version: '7.
|
72
|
+
version: '7.3'
|
73
73
|
- !ruby/object:Gem::Dependency
|
74
74
|
name: minitest
|
75
75
|
requirement: !ruby/object:Gem::Requirement
|
76
76
|
requirements:
|
77
77
|
- - ">="
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
version: '
|
79
|
+
version: '5.0'
|
80
80
|
type: :development
|
81
81
|
prerelease: false
|
82
82
|
version_requirements: !ruby/object:Gem::Requirement
|
83
83
|
requirements:
|
84
84
|
- - ">="
|
85
85
|
- !ruby/object:Gem::Version
|
86
|
-
version: '
|
86
|
+
version: '5.0'
|
87
87
|
- !ruby/object:Gem::Dependency
|
88
88
|
name: minitest-around
|
89
89
|
requirement: !ruby/object:Gem::Requirement
|
@@ -112,6 +112,20 @@ dependencies:
|
|
112
112
|
- - ">="
|
113
113
|
- !ruby/object:Gem::Version
|
114
114
|
version: '0'
|
115
|
+
- !ruby/object:Gem::Dependency
|
116
|
+
name: bundler
|
117
|
+
requirement: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
type: :development
|
123
|
+
prerelease: false
|
124
|
+
version_requirements: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
115
129
|
description: The default_value_for plugin allows one to define default values for
|
116
130
|
ActiveRecord models in a declarative manner
|
117
131
|
email: software-signing@phusion.nl
|
@@ -139,14 +153,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
139
153
|
requirements:
|
140
154
|
- - ">="
|
141
155
|
- !ruby/object:Gem::Version
|
142
|
-
version:
|
156
|
+
version: 3.0.0
|
143
157
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
158
|
requirements:
|
145
159
|
- - ">="
|
146
160
|
- !ruby/object:Gem::Version
|
147
161
|
version: '0'
|
148
162
|
requirements: []
|
149
|
-
rubygems_version: 3.
|
163
|
+
rubygems_version: 3.5.11
|
150
164
|
signing_key:
|
151
165
|
specification_version: 4
|
152
166
|
summary: Provides a way to specify default values for ActiveRecord models
|