set_as_primary 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +253 -0
- data/.travis.yml +23 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +12 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/set_as_primary/set_as_primary_generator.rb +29 -0
- data/lib/generators/set_as_primary/templates/migration.rb +6 -0
- data/lib/set_as_primary.rb +85 -0
- data/lib/set_as_primary/version.rb +5 -0
- data/set_as_primary.gemspec +54 -0
- metadata +213 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c069b2d95841442e11e813d3f1c1001f71194d1da587481f1512bc120f7f21f9
|
4
|
+
data.tar.gz: c464b6c1757dfe65c0eb14111bacef115637ab6ee9465011dfd8c61324928190
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d00b234b9c7abd66efbc3ceb3dd8c87a6d67975bc5d396b5b066f0625c5357fcb87ee8ad18112e3377e42d68498feb7159c112157d2b5c9c93926d2e5d33ba6
|
7
|
+
data.tar.gz: 4b7b9c9dee010b252439f65bf40e4be048ac1e21adedf6938da78bfc7a69fa6247532b7937b6d547cb9c2743a36ebae8b38f2e34d80ae10dff8c06af0c995f39
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,253 @@
|
|
1
|
+
# Copied from https://raw.githubusercontent.com/rails/rails/master/.rubocop.yml
|
2
|
+
|
3
|
+
require:
|
4
|
+
- rubocop-performance
|
5
|
+
- rubocop-rails
|
6
|
+
|
7
|
+
AllCops:
|
8
|
+
TargetRubyVersion: 2.5
|
9
|
+
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
|
10
|
+
# to ignore them, so only the ones explicitly set in this file are enabled.
|
11
|
+
DisabledByDefault: true
|
12
|
+
Exclude:
|
13
|
+
- '**/tmp/**/*'
|
14
|
+
- '**/templates/**/*'
|
15
|
+
- '**/vendor/**/*'
|
16
|
+
- 'actionpack/lib/action_dispatch/journey/parser.rb'
|
17
|
+
- 'railties/test/fixtures/tmp/**/*'
|
18
|
+
- 'actionmailbox/test/dummy/**/*'
|
19
|
+
- 'actiontext/test/dummy/**/*'
|
20
|
+
- '**/node_modules/**/*'
|
21
|
+
|
22
|
+
Performance:
|
23
|
+
Exclude:
|
24
|
+
- '**/test/**/*'
|
25
|
+
|
26
|
+
# Prefer assert_not over assert !
|
27
|
+
Rails/AssertNot:
|
28
|
+
Include:
|
29
|
+
- '**/test/**/*'
|
30
|
+
|
31
|
+
# Prefer assert_not_x over refute_x
|
32
|
+
Rails/RefuteMethods:
|
33
|
+
Include:
|
34
|
+
- '**/test/**/*'
|
35
|
+
|
36
|
+
# Prefer &&/|| over and/or.
|
37
|
+
Style/AndOr:
|
38
|
+
Enabled: true
|
39
|
+
|
40
|
+
# Align `when` with `case`.
|
41
|
+
Layout/CaseIndentation:
|
42
|
+
Enabled: true
|
43
|
+
|
44
|
+
# Align comments with method definitions.
|
45
|
+
Layout/CommentIndentation:
|
46
|
+
Enabled: true
|
47
|
+
|
48
|
+
Layout/ElseAlignment:
|
49
|
+
Enabled: true
|
50
|
+
|
51
|
+
# Align `end` with the matching keyword or starting expression except for
|
52
|
+
# assignments, where it should be aligned with the LHS.
|
53
|
+
Layout/EndAlignment:
|
54
|
+
Enabled: true
|
55
|
+
EnforcedStyleAlignWith: variable
|
56
|
+
AutoCorrect: true
|
57
|
+
|
58
|
+
Layout/EmptyLineAfterMagicComment:
|
59
|
+
Enabled: true
|
60
|
+
|
61
|
+
Layout/EmptyLinesAroundAccessModifier:
|
62
|
+
Enabled: true
|
63
|
+
EnforcedStyle: only_before
|
64
|
+
|
65
|
+
Layout/EmptyLinesAroundBlockBody:
|
66
|
+
Enabled: true
|
67
|
+
|
68
|
+
# In a regular class definition, no empty lines around the body.
|
69
|
+
Layout/EmptyLinesAroundClassBody:
|
70
|
+
Enabled: true
|
71
|
+
|
72
|
+
# In a regular method definition, no empty lines around the body.
|
73
|
+
Layout/EmptyLinesAroundMethodBody:
|
74
|
+
Enabled: true
|
75
|
+
|
76
|
+
# In a regular module definition, no empty lines around the body.
|
77
|
+
Layout/EmptyLinesAroundModuleBody:
|
78
|
+
Enabled: true
|
79
|
+
|
80
|
+
# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
|
81
|
+
Style/HashSyntax:
|
82
|
+
Enabled: true
|
83
|
+
|
84
|
+
Layout/IndentFirstArgument:
|
85
|
+
Enabled: true
|
86
|
+
|
87
|
+
# Method definitions after `private` or `protected` isolated calls need one
|
88
|
+
# extra level of indentation.
|
89
|
+
Layout/IndentationConsistency:
|
90
|
+
Enabled: true
|
91
|
+
EnforcedStyle: indented_internal_methods
|
92
|
+
|
93
|
+
# Two spaces, no tabs (for indentation).
|
94
|
+
Layout/IndentationWidth:
|
95
|
+
Enabled: true
|
96
|
+
|
97
|
+
Layout/LeadingCommentSpace:
|
98
|
+
Enabled: true
|
99
|
+
|
100
|
+
Layout/SpaceAfterColon:
|
101
|
+
Enabled: true
|
102
|
+
|
103
|
+
Layout/SpaceAfterComma:
|
104
|
+
Enabled: true
|
105
|
+
|
106
|
+
Layout/SpaceAfterSemicolon:
|
107
|
+
Enabled: true
|
108
|
+
|
109
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
110
|
+
Enabled: true
|
111
|
+
|
112
|
+
Layout/SpaceAroundKeyword:
|
113
|
+
Enabled: true
|
114
|
+
|
115
|
+
Layout/SpaceBeforeComma:
|
116
|
+
Enabled: true
|
117
|
+
|
118
|
+
Layout/SpaceBeforeComment:
|
119
|
+
Enabled: true
|
120
|
+
|
121
|
+
Layout/SpaceBeforeFirstArg:
|
122
|
+
Enabled: true
|
123
|
+
|
124
|
+
Style/DefWithParentheses:
|
125
|
+
Enabled: true
|
126
|
+
|
127
|
+
# Defining a method with parameters needs parentheses.
|
128
|
+
Style/MethodDefParentheses:
|
129
|
+
Enabled: true
|
130
|
+
|
131
|
+
Style/FrozenStringLiteralComment:
|
132
|
+
Enabled: true
|
133
|
+
EnforcedStyle: always
|
134
|
+
Exclude:
|
135
|
+
- 'actionview/test/**/*.builder'
|
136
|
+
- 'actionview/test/**/*.ruby'
|
137
|
+
- 'actionpack/test/**/*.builder'
|
138
|
+
- 'actionpack/test/**/*.ruby'
|
139
|
+
- 'activestorage/db/migrate/**/*.rb'
|
140
|
+
- 'activestorage/db/update_migrate/**/*.rb'
|
141
|
+
- 'actionmailbox/db/migrate/**/*.rb'
|
142
|
+
- 'actiontext/db/migrate/**/*.rb'
|
143
|
+
|
144
|
+
Style/RedundantFreeze:
|
145
|
+
Enabled: true
|
146
|
+
|
147
|
+
# Use `foo {}` not `foo{}`.
|
148
|
+
Layout/SpaceBeforeBlockBraces:
|
149
|
+
Enabled: true
|
150
|
+
|
151
|
+
# Use `foo { bar }` not `foo {bar}`.
|
152
|
+
Layout/SpaceInsideBlockBraces:
|
153
|
+
Enabled: true
|
154
|
+
EnforcedStyleForEmptyBraces: space
|
155
|
+
|
156
|
+
# Use `{ a: 1 }` not `{a:1}`.
|
157
|
+
Layout/SpaceInsideHashLiteralBraces:
|
158
|
+
Enabled: true
|
159
|
+
|
160
|
+
Layout/SpaceInsideParens:
|
161
|
+
Enabled: true
|
162
|
+
|
163
|
+
# Check quotes usage according to lint rule below.
|
164
|
+
Style/StringLiterals:
|
165
|
+
Enabled: true
|
166
|
+
EnforcedStyle: double_quotes
|
167
|
+
|
168
|
+
# Detect hard tabs, no hard tabs.
|
169
|
+
Layout/Tab:
|
170
|
+
Enabled: true
|
171
|
+
|
172
|
+
# Blank lines should not have any spaces.
|
173
|
+
Layout/TrailingBlankLines:
|
174
|
+
Enabled: true
|
175
|
+
|
176
|
+
# No trailing whitespace.
|
177
|
+
Layout/TrailingWhitespace:
|
178
|
+
Enabled: true
|
179
|
+
|
180
|
+
# Use quotes for string literals when they are enough.
|
181
|
+
Style/RedundantPercentQ:
|
182
|
+
Enabled: true
|
183
|
+
|
184
|
+
Lint/AmbiguousOperator:
|
185
|
+
Enabled: true
|
186
|
+
|
187
|
+
Lint/AmbiguousRegexpLiteral:
|
188
|
+
Enabled: true
|
189
|
+
|
190
|
+
Lint/ErbNewArguments:
|
191
|
+
Enabled: true
|
192
|
+
|
193
|
+
# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
|
194
|
+
Lint/RequireParentheses:
|
195
|
+
Enabled: true
|
196
|
+
|
197
|
+
Lint/ShadowingOuterLocalVariable:
|
198
|
+
Enabled: true
|
199
|
+
|
200
|
+
Lint/StringConversionInInterpolation:
|
201
|
+
Enabled: true
|
202
|
+
|
203
|
+
Lint/UriEscapeUnescape:
|
204
|
+
Enabled: true
|
205
|
+
|
206
|
+
Lint/UselessAssignment:
|
207
|
+
Enabled: true
|
208
|
+
|
209
|
+
Lint/DeprecatedClassMethods:
|
210
|
+
Enabled: true
|
211
|
+
|
212
|
+
Style/ParenthesesAroundCondition:
|
213
|
+
Enabled: true
|
214
|
+
|
215
|
+
Style/RedundantBegin:
|
216
|
+
Enabled: true
|
217
|
+
|
218
|
+
Style/RedundantReturn:
|
219
|
+
Enabled: true
|
220
|
+
AllowMultipleReturnValues: true
|
221
|
+
|
222
|
+
Style/Semicolon:
|
223
|
+
Enabled: true
|
224
|
+
AllowAsExpressionSeparator: true
|
225
|
+
|
226
|
+
# Prefer Foo.method over Foo::method
|
227
|
+
Style/ColonMethodCall:
|
228
|
+
Enabled: true
|
229
|
+
|
230
|
+
Style/TrivialAccessors:
|
231
|
+
Enabled: true
|
232
|
+
|
233
|
+
Performance/FlatMap:
|
234
|
+
Enabled: true
|
235
|
+
|
236
|
+
Performance/RedundantMerge:
|
237
|
+
Enabled: true
|
238
|
+
|
239
|
+
Performance/StartWith:
|
240
|
+
Enabled: true
|
241
|
+
|
242
|
+
Performance/EndWith:
|
243
|
+
Enabled: true
|
244
|
+
|
245
|
+
Performance/RegexpMatch:
|
246
|
+
Enabled: true
|
247
|
+
|
248
|
+
Performance/ReverseEach:
|
249
|
+
Enabled: true
|
250
|
+
|
251
|
+
Performance/UnfreezeString:
|
252
|
+
Enabled: true
|
253
|
+
|
data/.travis.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
---
|
2
|
+
sudo: false
|
3
|
+
language: ruby
|
4
|
+
rvm:
|
5
|
+
- 2.5.3
|
6
|
+
gemfile:
|
7
|
+
- Gemfile
|
8
|
+
- test/gemfiles/activerecord52.gemfile
|
9
|
+
- test/gemfiles/activerecord42.gemfile
|
10
|
+
script: bundle exec rake test
|
11
|
+
services:
|
12
|
+
- postgresql
|
13
|
+
- mysql
|
14
|
+
addons:
|
15
|
+
postgresql: 10
|
16
|
+
before_install:
|
17
|
+
- mysqladmin create set_as_primary_test
|
18
|
+
- mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
19
|
+
- createdb set_as_primary_test
|
20
|
+
notifications:
|
21
|
+
email:
|
22
|
+
on_success: never
|
23
|
+
on_failure: change
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in set_as_primary.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "activerecord", "~> 6.0.0"
|
9
|
+
|
10
|
+
group :development do
|
11
|
+
gem "rubocop", ">= 0.47", require: false
|
12
|
+
gem "rubocop-performance", require: false
|
13
|
+
gem "rubocop-rails", require: false
|
14
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Santosh Wadghule
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# SetAsPrimary
|
2
|
+
|
3
|
+
The simplest way to handle the primary or default flag (:white_check_mark:) to
|
4
|
+
your models.
|
5
|
+
|
6
|
+
Supports PostgreSQL, MySQL, and SQLite.
|
7
|
+
|
8
|
+
[Demo Rails application](https://cryptic-lake-90495.herokuapp.com/) |
|
9
|
+
[Demo Rails application GitHub repository](https://github.com/mechanicles/set_as_primary_rails_app)
|
10
|
+
|
11
|
+
[](https://travis-ci.org/mechanicles/set_as_primary)
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'set_as_primary'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install set_as_primary
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
In your Rails application, you might have models like EmailAddress, PhoneNumber,
|
32
|
+
Address, etc., which belong to the User/Person model. There, you might need to
|
33
|
+
set a primary email address, primary phone number, or default address for a user,
|
34
|
+
and this gem helps you to do that.
|
35
|
+
|
36
|
+
Examples:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class User < ApplicationRecord
|
40
|
+
has_many :email_addresses
|
41
|
+
has_many :addresses, as: :owner
|
42
|
+
end
|
43
|
+
|
44
|
+
class EmailAddress < ApplicationRecord
|
45
|
+
include SetAsPrimary
|
46
|
+
belongs_to :user
|
47
|
+
|
48
|
+
set_as_primary :primary, owner_key: :user
|
49
|
+
end
|
50
|
+
|
51
|
+
class Address < ApplicationRecord
|
52
|
+
include SetAsPrimary
|
53
|
+
belongs_to :owner, polymorphic: true
|
54
|
+
|
55
|
+
set_as_primary :primary, owner_key: :owner
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
You need to include `SetAsPrimary` module in your model where you want to handle
|
60
|
+
the primary flag. Then pass your primary flag attribute with required association
|
61
|
+
key `owner_key` to class helper method `set_as_primary`.
|
62
|
+
|
63
|
+
Default primary flag attribute is `primary`, and you can use another too (but
|
64
|
+
make sure that flag should be a boolean column type).
|
65
|
+
|
66
|
+
#### Migration
|
67
|
+
|
68
|
+
If your table does not have the primary flag column, then you can add it by running
|
69
|
+
following command in your rails project:
|
70
|
+
|
71
|
+
```ssh
|
72
|
+
rails generate set_as_primary your_table_name
|
73
|
+
```
|
74
|
+
|
75
|
+
If you run above command for `email_addresses` table, then it creates
|
76
|
+
migration like this:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
class AddPrimaryColumnToEmailAddresses < ActiveRecord::Migration[6.0]
|
80
|
+
def change
|
81
|
+
add_column :email_addresses, :primary, :boolean, default: false, null: false
|
82
|
+
end
|
83
|
+
end
|
84
|
+
```
|
85
|
+
Don't forget to run `rails db:migrate` to create actual column in the table.
|
86
|
+
|
87
|
+
#### force_primary
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class Address < ApplicationRecord
|
91
|
+
include SetAsPrimary
|
92
|
+
belongs_to :user
|
93
|
+
|
94
|
+
set_as_primary :default, owner_key: :user, force_primary: false
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
By default `force_primary` option is set to `true`. If this option is `true`,
|
99
|
+
then it automatically sets record as primary when there is only one record in
|
100
|
+
the table. If you don't want this flow, then set it as `false`.
|
101
|
+
|
102
|
+
## Development
|
103
|
+
|
104
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
105
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
106
|
+
prompt that will allow you to experiment.
|
107
|
+
|
108
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
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).
|
112
|
+
|
113
|
+
## Contributing
|
114
|
+
|
115
|
+
Bug reports and pull requests are welcome on GitHub at
|
116
|
+
https://github.com/mechanicles/set_as_primary. This project is intended to be a
|
117
|
+
safe, welcoming space for collaboration, and contributors are expected to adhere
|
118
|
+
to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
119
|
+
|
120
|
+
## License
|
121
|
+
|
122
|
+
The gem is available as open source under the terms of the
|
123
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "set_as_primary"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/active_record"
|
4
|
+
|
5
|
+
module SetAsPrimary
|
6
|
+
module Generators
|
7
|
+
class SetAsPrimaryGenerator < Rails::Generators::Base
|
8
|
+
include Rails::Generators::Migration
|
9
|
+
|
10
|
+
source_root File.expand_path("templates", __dir__)
|
11
|
+
argument :table_name, type: :string
|
12
|
+
|
13
|
+
desc "Adds a boolean column 'primary' to the given table."
|
14
|
+
|
15
|
+
def copy_migration
|
16
|
+
migration_template "migration.rb", "db/migrate/add_primary_column_to_#{table_name}.rb",
|
17
|
+
migration_version: migration_version
|
18
|
+
end
|
19
|
+
|
20
|
+
def migration_version
|
21
|
+
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.next_migration_number(dirname)
|
25
|
+
ActiveRecord::Generators::Base.next_migration_number(dirname)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "set_as_primary/version"
|
4
|
+
require "active_support/concern"
|
5
|
+
|
6
|
+
module SetAsPrimary
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
before_save :unset_old_primary
|
13
|
+
before_save :force_primary, if: -> { self.class._force_primary }
|
14
|
+
|
15
|
+
instance_eval do
|
16
|
+
class_attribute :_primary_flag_attribute, :_owner_key, :_force_primary
|
17
|
+
|
18
|
+
def set_as_primary(primary_flag_attribute = :primary, options)
|
19
|
+
configuration = { owner_key: nil, force_primary: true }
|
20
|
+
|
21
|
+
configuration.update(options) if options.is_a?(Hash)
|
22
|
+
|
23
|
+
handle_setup_errors(primary_flag_attribute, configuration)
|
24
|
+
|
25
|
+
self._primary_flag_attribute = primary_flag_attribute
|
26
|
+
self._owner_key = configuration[:owner_key]
|
27
|
+
self._force_primary = configuration[:force_primary]
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def handle_setup_errors(primary_flag_attribute, configuration)
|
32
|
+
if !primary_flag_attribute.is_a?(Symbol)
|
33
|
+
raise SetAsPrimary::Error, "Wrong attribute! Please provide attribute in symbol type."
|
34
|
+
end
|
35
|
+
|
36
|
+
owner_key = configuration[:owner_key]
|
37
|
+
|
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
|
+
raise ActiveRecord::AssociationNotFoundError.new(self, owner_key)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def unset_old_primary
|
51
|
+
return unless self.public_send(self.class._primary_flag_attribute)
|
52
|
+
|
53
|
+
scope = self.class.where(scope_options)
|
54
|
+
|
55
|
+
scope = scope.where("id != ?", id) unless new_record?
|
56
|
+
|
57
|
+
scope.update_all(self.class._primary_flag_attribute => false)
|
58
|
+
end
|
59
|
+
|
60
|
+
def force_primary
|
61
|
+
count = self.class.where(scope_options).count
|
62
|
+
|
63
|
+
if (count == 1 && !new_record?) || (count == 0 && new_record?)
|
64
|
+
self.public_send("#{self.class._primary_flag_attribute}=", true)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def scope_options
|
69
|
+
if self.class.reflect_on_association(self._owner_key).options[:polymorphic]
|
70
|
+
polymorphic_condition_options
|
71
|
+
else
|
72
|
+
owner_id = "#{self.class._owner_key}_id".to_sym
|
73
|
+
{ owner_id => self.public_send(owner_id) }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def polymorphic_condition_options
|
78
|
+
owner = self.public_send(self.class._owner_key)
|
79
|
+
|
80
|
+
{
|
81
|
+
"#{self.class._owner_key}_id".to_sym => owner.id,
|
82
|
+
"#{self.class._owner_key}_type".to_sym => owner.class.name
|
83
|
+
}
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "set_as_primary/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "set_as_primary"
|
9
|
+
spec.version = SetAsPrimary::VERSION
|
10
|
+
spec.authors = ["Santosh Wadghule"]
|
11
|
+
spec.email = ["santosh.wadghule@gmail.com"]
|
12
|
+
|
13
|
+
spec.summary = "The simplest way to handle the primary or default flag to your models."
|
14
|
+
spec.homepage = "https://github.com/mechanicles/set_as_primary"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
24
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
25
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.required_ruby_version = ">= 2.2"
|
40
|
+
|
41
|
+
spec.add_dependency "activesupport", ">= 4.2"
|
42
|
+
|
43
|
+
spec.add_development_dependency "bundler"
|
44
|
+
spec.add_development_dependency "rake"
|
45
|
+
spec.add_development_dependency "minitest"
|
46
|
+
spec.add_development_dependency "railties"
|
47
|
+
spec.add_development_dependency "activerecord"
|
48
|
+
spec.add_development_dependency "rb-readline"
|
49
|
+
spec.add_development_dependency "byebug"
|
50
|
+
|
51
|
+
spec.add_development_dependency "pg"
|
52
|
+
spec.add_development_dependency "mysql2"
|
53
|
+
spec.add_development_dependency "sqlite3"
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: set_as_primary
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Santosh Wadghule
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: railties
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activerecord
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rb-readline
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: byebug
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pg
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: mysql2
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: sqlite3
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
description:
|
168
|
+
email:
|
169
|
+
- santosh.wadghule@gmail.com
|
170
|
+
executables: []
|
171
|
+
extensions: []
|
172
|
+
extra_rdoc_files: []
|
173
|
+
files:
|
174
|
+
- ".gitignore"
|
175
|
+
- ".rubocop.yml"
|
176
|
+
- ".travis.yml"
|
177
|
+
- Gemfile
|
178
|
+
- LICENSE.txt
|
179
|
+
- README.md
|
180
|
+
- Rakefile
|
181
|
+
- bin/console
|
182
|
+
- bin/setup
|
183
|
+
- lib/generators/set_as_primary/set_as_primary_generator.rb
|
184
|
+
- lib/generators/set_as_primary/templates/migration.rb
|
185
|
+
- lib/set_as_primary.rb
|
186
|
+
- lib/set_as_primary/version.rb
|
187
|
+
- set_as_primary.gemspec
|
188
|
+
homepage: https://github.com/mechanicles/set_as_primary
|
189
|
+
licenses:
|
190
|
+
- MIT
|
191
|
+
metadata:
|
192
|
+
homepage_uri: https://github.com/mechanicles/set_as_primary
|
193
|
+
post_install_message:
|
194
|
+
rdoc_options: []
|
195
|
+
require_paths:
|
196
|
+
- lib
|
197
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '2.2'
|
202
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
203
|
+
requirements:
|
204
|
+
- - ">="
|
205
|
+
- !ruby/object:Gem::Version
|
206
|
+
version: '0'
|
207
|
+
requirements: []
|
208
|
+
rubyforge_project:
|
209
|
+
rubygems_version: 2.7.6
|
210
|
+
signing_key:
|
211
|
+
specification_version: 4
|
212
|
+
summary: The simplest way to handle the primary or default flag to your models.
|
213
|
+
test_files: []
|