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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c069b2d95841442e11e813d3f1c1001f71194d1da587481f1512bc120f7f21f9
4
- data.tar.gz: c464b6c1757dfe65c0eb14111bacef115637ab6ee9465011dfd8c61324928190
3
+ metadata.gz: '09252a0d5cef2ec2dbf697837059b9604bad6617f82e2938fb95b0e7ea54749e'
4
+ data.tar.gz: 8bed32971351c098816889638e39307b44692445fb5593a6941b28010344ea26
5
5
  SHA512:
6
- metadata.gz: 4d00b234b9c7abd66efbc3ceb3dd8c87a6d67975bc5d396b5b066f0625c5357fcb87ee8ad18112e3377e42d68498feb7159c112157d2b5c9c93926d2e5d33ba6
7
- data.tar.gz: 4b7b9c9dee010b252439f65bf40e4be048ac1e21adedf6938da78bfc7a69fa6247532b7937b6d547cb9c2743a36ebae8b38f2e34d80ae10dff8c06af0c995f39
6
+ metadata.gz: 7ff0a54d3af341c41ba937311a449ebb3adf582390b4dee9880aa36e548d56433c1314836e0246680b9e8611b9643085a390f48ab9962edbe5aa31501adb5ecd
7
+ data.tar.gz: b4f24ce736c8f261252e48e342896a13a0710a36fc2abf17fb3b170e4291f945ad47dc900cb0ee0c2ae839d7af0f3358c4657cf21806f1d4ae1d07178ee7ae18
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ ## 0.1.1 (2019-12-29)
2
+
3
+ - Added support for single model with no association.
4
+ - Fixed polymorphic association issue as it was not working properly.
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, 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.
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
- the primary flag. Then pass your primary flag attribute with required association
61
- key `owner_key` to class helper method `set_as_primary`.
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 (but
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`. 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).
117
+ To install this gem onto your local machine, run `bundle exec rake install`.
112
118
 
113
119
  ## Contributing
114
120
 
@@ -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.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
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SetAsPrimary
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
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.0
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-25 00:00:00.000000000 Z
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