defaulter 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -5
- data/lib/defaulter/has_default.rb +5 -2
- data/lib/defaulter/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -4,6 +4,10 @@ Defaulter allows you to mark and maintain default objects in ActiveRecord associ
|
|
4
4
|
|
5
5
|
For example, a `User` model can have many `Email` models using ActiveRecords `has_many` functionality. But which email address do you send mail to? Certainly not all, that's just irritating. Instead, marking a particular record as default and sending mail there is a much better idea. The defaulter gem allows you to achieve that rather simply.
|
6
6
|
|
7
|
+
## Compatibility
|
8
|
+
|
9
|
+
Defaulter works with Rails 4+. Backporting to Rails 3.2 is work in progress.
|
10
|
+
|
7
11
|
## Installation
|
8
12
|
|
9
13
|
### Step 1
|
@@ -11,10 +15,10 @@ For example, a `User` model can have many `Email` models using ActiveRecords `ha
|
|
11
15
|
Put this in your project's Gemfile:
|
12
16
|
|
13
17
|
```ruby
|
14
|
-
gem defaulter
|
18
|
+
gem 'defaulter'
|
15
19
|
```
|
16
20
|
|
17
|
-
Don't forget to run bundle to fetch the gem.
|
21
|
+
Don't forget to run the `bundle` command to fetch the gem.
|
18
22
|
|
19
23
|
### Step 2
|
20
24
|
|
@@ -24,7 +28,7 @@ Next, you'll need to add one column to whichever table where you want to mark de
|
|
24
28
|
t.boolean :prime, null: false, default: false
|
25
29
|
```
|
26
30
|
|
27
|
-
If already have a
|
31
|
+
If already have a migration, you will need to create a new one and add the column like so:
|
28
32
|
|
29
33
|
```ruby
|
30
34
|
add_column :table_name, :prime, null: false, default: false
|
@@ -36,7 +40,7 @@ Now, run the migrations.
|
|
36
40
|
|
37
41
|
### Step 3
|
38
42
|
|
39
|
-
There's
|
43
|
+
There's no *Step 3*. We're ready to roll.
|
40
44
|
|
41
45
|
## Configuration
|
42
46
|
|
@@ -84,7 +88,7 @@ I threw this gem together in about half an hour, there is scope for improvement.
|
|
84
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`
|
85
89
|
3. Dynamically generated utitlity instance methods like `default_email` and `default_address`
|
86
90
|
4. Optionally, prevent default records from being destroyed
|
87
|
-
5.
|
91
|
+
5. Backport to Rails 3.2
|
88
92
|
|
89
93
|
## License
|
90
94
|
|
@@ -28,7 +28,10 @@ module Defaulter
|
|
28
28
|
def << (models)
|
29
29
|
owner = proxy_association.owner
|
30
30
|
reflection = proxy_association.reflection
|
31
|
-
|
31
|
+
primes = models.select { |m| m.prime? }
|
32
|
+
|
33
|
+
models.first.prime = true if self.load.empty? && primes.empty? && !models.blank?
|
34
|
+
model = models.delete(primes.last)
|
32
35
|
|
33
36
|
if model.blank?
|
34
37
|
self.concat(models)
|
@@ -43,4 +46,4 @@ module Defaulter
|
|
43
46
|
end
|
44
47
|
end
|
45
48
|
end
|
46
|
-
end
|
49
|
+
end
|
data/lib/defaulter/version.rb
CHANGED