imyou 1.2.0 → 1.3.0

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: 2ea37cab3c8ce4f35113a47972b4936655a977040adee8ea2bd6b59f01ad5168
4
- data.tar.gz: 7de5c7103f8e133355f0c08ad0fa6e7056e33c9101930c7326a965aa09083022
3
+ metadata.gz: 05a7537c8208e72b170c4f7e4288ab682c166e155065754986d8bae046620af4
4
+ data.tar.gz: 6bbd47526c77292554d5245ff49b06ae10ee3a4cf6b652e6256d3ce31ae4c39b
5
5
  SHA512:
6
- metadata.gz: 1ddaf339b9c10bfb339280fd05eb4dc827b46ca6f163f3031a7e95e292ff8eea6624f5660f605f6ba3827f22a95bb60f8fc1348ec118843f3a2560059ca12861
7
- data.tar.gz: 3f7a465be25dd666cab0aa6a3e427f9ed49b836f3329c3ab5ae96771a156b1e3334b5fd1d1ab9c445abdabcafa368bfe03ad435e07cea2fa6d94b182f5436dff
6
+ metadata.gz: 873bbe9b3c56b04c3d77474029de273352347de2e5156ac674cb9b81427b43ad83312bdd30312a3490487579005882c5929da35ee863742b9a5eb447b7412c7b
7
+ data.tar.gz: b78f199e55baa6924ef404ef7198e9e9d0d727d61150f631a2240d9776f3bd58c077c191f6cb03f4cf89bfd18f643d6a4afda11be95885462c76fa026e10be79
data/README.md CHANGED
@@ -60,7 +60,7 @@ end
60
60
  @book.nicknames = %w(red green blue)
61
61
  @book.nicknames # => ['red', 'green', 'blue']
62
62
 
63
- # eager_load(LEFT OUTER JOIN)
63
+ # preload
64
64
  User.with_nicknames
65
65
 
66
66
  # Search users by nickname.
@@ -8,6 +8,11 @@ module Imyou
8
8
  class_eval do
9
9
 
10
10
  has_many :imyou_nicknames, -> { order(id: :asc) }, class_name: 'Imyou::Nickname', as: :model, dependent: :destroy
11
+
12
+ accepts_nested_attributes_for :imyou_nicknames,
13
+ allow_destroy: true,
14
+ reject_if: ->(attributes) { attributes['name'].blank? }
15
+
11
16
  scope :with_nicknames, -> { preload(:imyou_nicknames) }
12
17
 
13
18
  scope :match_by_nickname, ->(nickname, with_name_column: true) do
@@ -81,7 +86,11 @@ module Imyou
81
86
  end
82
87
 
83
88
  def nicknames
84
- self.imyou_nicknames.pluck(:name)
89
+ if new_record?
90
+ self.imyou_nicknames.map(&:name)
91
+ else
92
+ self.imyou_nicknames.pluck(:name)
93
+ end
85
94
  end
86
95
 
87
96
  def remove_all_nicknames
@@ -89,16 +98,32 @@ module Imyou
89
98
  end
90
99
 
91
100
  def add_nickname(nickname)
92
- self.imyou_nicknames.create!(name: nickname)
101
+ if new_record?
102
+ self.imyou_nicknames.build(name: nickname)
103
+ else
104
+ self.imyou_nicknames.find_or_create_by(name: nickname)
105
+ end
93
106
  end
94
107
 
95
108
  def remove_nickname(nickname)
96
- self.imyou_nicknames.find_by(name: nickname)&.destroy!
109
+ if new_record?
110
+ array = self.imyou_nicknames.to_a.delete_if do |imyou_nickname|
111
+ imyou_nickname.name == nickname
112
+ end
113
+ self.imyou_nicknames.replace(array)
114
+ true
115
+ else
116
+ self.imyou_nicknames.find_by(name: nickname)&.destroy!
117
+ end
97
118
  end
98
119
 
99
120
  def nicknames=(new_nicknames)
100
- if new_nicknames.blank?
101
- self.imyou_nicknames.delete_all
121
+ if new_record?
122
+ new_nicknames&.each do |new_nickname|
123
+ self.imyou_nicknames.build(name: new_nickname)
124
+ end
125
+ elsif new_nicknames.blank?
126
+ self.remove_all_nicknames
102
127
  else
103
128
  self.imyou_nicknames.where.not(name: new_nicknames).delete_all
104
129
  new_nicknames.each do |new_nickname|
@@ -4,11 +4,9 @@ module Imyou
4
4
  belongs_to :model, polymorphic: true
5
5
 
6
6
  scope :by_type, -> (klass) { where(model_type: klass.name) }
7
- scope :default_order, -> { order(created_at: :asc) }
7
+ scope :default_order, -> { order(id: :asc) }
8
8
  default_scope { default_order }
9
9
 
10
- validates :model_id, presence: true
11
- validates :model_type, presence: true
12
10
  validates :name, presence: true
13
11
  validates_uniqueness_of :name, scope: [ :model_id, :model_type ]
14
12
  end
@@ -1,3 +1,3 @@
1
1
  module Imyou
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imyou
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - patorash