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 +4 -4
- data/README.md +1 -1
- data/lib/imyou/models.rb +30 -5
- data/lib/imyou/nickname.rb +1 -3
- data/lib/imyou/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05a7537c8208e72b170c4f7e4288ab682c166e155065754986d8bae046620af4
|
4
|
+
data.tar.gz: 6bbd47526c77292554d5245ff49b06ae10ee3a4cf6b652e6256d3ce31ae4c39b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 873bbe9b3c56b04c3d77474029de273352347de2e5156ac674cb9b81427b43ad83312bdd30312a3490487579005882c5929da35ee863742b9a5eb447b7412c7b
|
7
|
+
data.tar.gz: b78f199e55baa6924ef404ef7198e9e9d0d727d61150f631a2240d9776f3bd58c077c191f6cb03f4cf89bfd18f643d6a4afda11be95885462c76fa026e10be79
|
data/README.md
CHANGED
data/lib/imyou/models.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
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
|
101
|
-
|
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|
|
data/lib/imyou/nickname.rb
CHANGED
@@ -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(
|
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
|
data/lib/imyou/version.rb
CHANGED