simple_enum-multiple 0.1.5 → 0.1.6
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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07e1154c47e72d096a9024f7b7a76168f3f64a93
|
4
|
+
data.tar.gz: abb32b7b26d2e5197d33d8489b101e9a65731b72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 741ea605c0d38593a4bec573cd62111cb3224c52b1d2941b1acf21af519eaeab0f1b0eb76b84b5aa421c47090346c607c02878a4c30e568076962a7c67698268
|
7
|
+
data.tar.gz: 0cab34b53652fb823e57f9cb5791d06331acff9b6d23285fdae468adc5afa30bd7ffb045f715da368dc0cf8838a6051a4824252ec3125f219db1a09674f7e99d
|
@@ -18,33 +18,29 @@ module SimpleEnum
|
|
18
18
|
remote_key = self.remote_key = name.singularize.foreign_key
|
19
19
|
|
20
20
|
klass.class_eval do
|
21
|
-
|
21
|
+
define_attribute_method source
|
22
22
|
|
23
|
-
define_method
|
24
|
-
instance_variable_get(:"@#{source}")
|
25
|
-
instance_variable_get(:"@#{source}_was")
|
26
|
-
end
|
27
|
-
|
28
|
-
define_method :"#{source}_was" do
|
29
|
-
instance_variable = instance_variable_get(:"@#{source}_was")
|
23
|
+
define_method source do
|
24
|
+
instance_variable = instance_variable_get(:"@#{source}")
|
30
25
|
return instance_variable if instance_variable
|
31
26
|
sql = table.where(table[foreign_key].eq(self.id))
|
32
27
|
.project(table[remote_key])
|
33
28
|
.to_sql
|
34
29
|
original_cds = ActiveRecord::Base.connection.send(:select, sql).rows.map(&:first).map(&:to_i)
|
35
|
-
instance_variable_set(:"@#{source}
|
30
|
+
instance_variable_set(:"@#{source}", original_cds)
|
36
31
|
end
|
37
32
|
|
38
|
-
define_method source do
|
33
|
+
define_method :"#{source}=" do |current_cds|
|
39
34
|
instance_variable = instance_variable_get(:"@#{source}")
|
40
|
-
|
41
|
-
instance_variable_set(:"@#{source}",
|
35
|
+
send("#{source}_will_change!") unless current_cds == instance_variable
|
36
|
+
instance_variable_set(:"@#{source}", current_cds.select(&:present?).map(&:to_i))
|
42
37
|
end
|
43
38
|
|
44
39
|
define_method :"update_#{source}!" do
|
45
40
|
return unless send(:"#{source}_changed?")
|
46
41
|
original_cds = send(:"#{source}_was")
|
47
|
-
current_cds = send(source)
|
42
|
+
current_cds = send(source)
|
43
|
+
|
48
44
|
|
49
45
|
# if any enum been removed
|
50
46
|
if (original_cds - current_cds).any?
|