set_as_primary 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +19 -13
- data/lib/set_as_primary.rb +16 -13
- data/lib/set_as_primary/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '09252a0d5cef2ec2dbf697837059b9604bad6617f82e2938fb95b0e7ea54749e'
|
4
|
+
data.tar.gz: 8bed32971351c098816889638e39307b44692445fb5593a6941b28010344ea26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ff0a54d3af341c41ba937311a449ebb3adf582390b4dee9880aa36e548d56433c1314836e0246680b9e8611b9643085a390f48ab9962edbe5aa31501adb5ecd
|
7
|
+
data.tar.gz: b4f24ce736c8f261252e48e342896a13a0710a36fc2abf17fb3b170e4291f945ad47dc900cb0ee0c2ae839d7af0f3358c4657cf21806f1d4ae1d07178ee7ae18
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# SetAsPrimary
|
2
2
|
|
3
3
|
The simplest way to handle the primary or default flag (:white_check_mark:) to
|
4
|
-
your models.
|
4
|
+
your Rails models.
|
5
5
|
|
6
6
|
Supports PostgreSQL, MySQL, and SQLite.
|
7
7
|
|
@@ -29,9 +29,11 @@ Or install it yourself as:
|
|
29
29
|
## Usage
|
30
30
|
|
31
31
|
In your Rails application, you might have models like EmailAddress, PhoneNumber,
|
32
|
-
Address, etc., which belong to the User/Person model. There,
|
33
|
-
set a primary email address, primary phone number, or default
|
34
|
-
and this gem helps you to do that.
|
32
|
+
Address, etc., which belong to the User/Person model or polymorphic model. There,
|
33
|
+
you might need to set a primary email address, primary phone number, or default
|
34
|
+
address for a user, and this gem helps you to do that.
|
35
|
+
|
36
|
+
It also supports single model with no association's context.
|
35
37
|
|
36
38
|
Examples:
|
37
39
|
|
@@ -54,14 +56,21 @@ class Address < ApplicationRecord
|
|
54
56
|
|
55
57
|
set_as_primary :primary, owner_key: :owner
|
56
58
|
end
|
59
|
+
|
60
|
+
# Single model with no owner/association's context.
|
61
|
+
class Post < ApplicationRecord
|
62
|
+
include SetAsPrimary
|
63
|
+
|
64
|
+
set_as_primary
|
65
|
+
end
|
57
66
|
```
|
58
67
|
|
59
|
-
You need to include `SetAsPrimary` module in your model where you want to handle
|
60
|
-
|
61
|
-
key `owner_key` to
|
68
|
+
You need to include `SetAsPrimary` module in your model where you want to handle the primary flag.
|
69
|
+
Then to `set_as_primary` class helper method, pass your primary flag attribute. You might need to pass
|
70
|
+
association key `owner_key` if you wan to consider owner's (association's) context.
|
62
71
|
|
63
|
-
Default primary flag attribute is `primary`, and you can use another too
|
64
|
-
make sure that flag should be a boolean column type
|
72
|
+
**NOTE:** Default primary flag attribute is `primary`, and you can use another one too like `default` but
|
73
|
+
make sure that flag should be present in the table and should be a boolean column type.
|
65
74
|
|
66
75
|
#### Migration
|
67
76
|
|
@@ -105,10 +114,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
105
114
|
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
106
115
|
prompt that will allow you to experiment.
|
107
116
|
|
108
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
109
|
-
release a new version, update the version number in `version.rb`, and then run
|
110
|
-
`bundle exec rake release`, which will create a git tag for the version, push git
|
111
|
-
commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
117
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
112
118
|
|
113
119
|
## Contributing
|
114
120
|
|
data/lib/set_as_primary.rb
CHANGED
@@ -15,7 +15,12 @@ module SetAsPrimary
|
|
15
15
|
instance_eval do
|
16
16
|
class_attribute :_primary_flag_attribute, :_owner_key, :_force_primary
|
17
17
|
|
18
|
-
def set_as_primary(primary_flag_attribute = :primary, options)
|
18
|
+
def set_as_primary(primary_flag_attribute = :primary, options = {})
|
19
|
+
if primary_flag_attribute.is_a?(Hash)
|
20
|
+
options = primary_flag_attribute
|
21
|
+
primary_flag_attribute = :primary
|
22
|
+
end
|
23
|
+
|
19
24
|
configuration = { owner_key: nil, force_primary: true }
|
20
25
|
|
21
26
|
configuration.update(options) if options.is_a?(Hash)
|
@@ -35,11 +40,7 @@ module SetAsPrimary
|
|
35
40
|
|
36
41
|
owner_key = configuration[:owner_key]
|
37
42
|
|
38
|
-
if owner_key.nil?
|
39
|
-
raise SetAsPrimary::Error, "Please provide `owner_key` option."
|
40
|
-
end
|
41
|
-
|
42
|
-
if reflect_on_association(owner_key).nil?
|
43
|
+
if owner_key.present? && reflect_on_association(owner_key).nil?
|
43
44
|
raise ActiveRecord::AssociationNotFoundError.new(self, owner_key)
|
44
45
|
end
|
45
46
|
end
|
@@ -50,7 +51,7 @@ module SetAsPrimary
|
|
50
51
|
def unset_old_primary
|
51
52
|
return unless self.public_send(self.class._primary_flag_attribute)
|
52
53
|
|
53
|
-
scope = self.class.where(scope_options)
|
54
|
+
scope = self.class.where(scope_options) if scope_options.present?
|
54
55
|
|
55
56
|
scope = scope.where("id != ?", id) unless new_record?
|
56
57
|
|
@@ -66,12 +67,14 @@ module SetAsPrimary
|
|
66
67
|
end
|
67
68
|
|
68
69
|
def scope_options
|
69
|
-
if self.class.
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
70
|
+
return nil if self.class._owner_key.nil?
|
71
|
+
|
72
|
+
@scope_option ||= if self.class.reflect_on_association(self.class._owner_key).options[:polymorphic]
|
73
|
+
polymorphic_condition_options
|
74
|
+
else
|
75
|
+
owner_id = "#{self.class._owner_key}_id".to_sym
|
76
|
+
{ owner_id => self.public_send(owner_id) }
|
77
|
+
end
|
75
78
|
end
|
76
79
|
|
77
80
|
def polymorphic_condition_options
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: set_as_primary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Santosh Wadghule
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -174,6 +174,7 @@ files:
|
|
174
174
|
- ".gitignore"
|
175
175
|
- ".rubocop.yml"
|
176
176
|
- ".travis.yml"
|
177
|
+
- CHANGELOG.md
|
177
178
|
- Gemfile
|
178
179
|
- LICENSE.txt
|
179
180
|
- README.md
|