defaulter 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/lib/defaulter/has_default.rb +9 -6
- data/lib/defaulter/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -6,7 +6,7 @@ For example, a `User` model can have many `Email` models using ActiveRecords `ha
|
|
6
6
|
|
7
7
|
## Compatibility
|
8
8
|
|
9
|
-
|
9
|
+
Besides Rails 4 beta, defaulter now supports Rails 3.2. It **may work** on lower rails versions, but your mileage may vary.
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
@@ -88,7 +88,7 @@ I threw this gem together in about half an hour, there is scope for improvement.
|
|
88
88
|
2. Ability the configure the name of the default column, by passing it with the `has_default` call like so: `has_default :email, default_column: :primordial`
|
89
89
|
3. Dynamically generated utitlity instance methods like `default_email` and `default_address`
|
90
90
|
4. Optionally, prevent default records from being destroyed
|
91
|
-
5. Backport to Rails 3.2
|
91
|
+
5. ~~Backport to Rails 3.2~~ (v0.0.9)
|
92
92
|
|
93
93
|
## License
|
94
94
|
|
@@ -1,7 +1,11 @@
|
|
1
1
|
module Defaulter
|
2
2
|
module HasDefault
|
3
3
|
def has_default(model, *args)
|
4
|
-
options
|
4
|
+
options = args.extract_options!
|
5
|
+
|
6
|
+
def load_method
|
7
|
+
@@load_method ||= (Gem::Version.new('4.0.0beta') >= Gem::Version.new(Rails.version)) ? :all : :load
|
8
|
+
end
|
5
9
|
|
6
10
|
has_many model.to_s.pluralize.to_sym, options do
|
7
11
|
|
@@ -10,7 +14,7 @@ module Defaulter
|
|
10
14
|
end
|
11
15
|
|
12
16
|
def default=(record)
|
13
|
-
records =
|
17
|
+
records = self.send(load_method)
|
14
18
|
|
15
19
|
if records.include?(record)
|
16
20
|
ActiveRecord::Base.transaction do
|
@@ -24,12 +28,11 @@ module Defaulter
|
|
24
28
|
end
|
25
29
|
|
26
30
|
def << (models)
|
27
|
-
primes
|
31
|
+
primes = nil
|
28
32
|
|
29
33
|
case models.class.name
|
30
34
|
when 'Array'
|
31
|
-
|
32
|
-
models.first.prime = true if self.load.empty? && primes.empty? && !models.blank?
|
35
|
+
models.first.prime = true if self.send(load_method).empty? && primes.empty? && !models.blank?
|
33
36
|
primes = models.select { |m| m.prime? }
|
34
37
|
model = models.delete(primes.last)
|
35
38
|
|
@@ -44,7 +47,7 @@ module Defaulter
|
|
44
47
|
end
|
45
48
|
else
|
46
49
|
unless models.blank?
|
47
|
-
loaded = !
|
50
|
+
loaded = !self.send(load_method).empty?
|
48
51
|
models.prime = true unless loaded
|
49
52
|
|
50
53
|
ActiveRecord::Base.transaction do
|
data/lib/defaulter/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: defaulter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
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: 2013-01-
|
12
|
+
date: 2013-01-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 3.2.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 3.2.0
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: sqlite3
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|