simple_enum-multiple 0.1.5 → 0.1.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e9969e388df1b2c1e70c98b9f694b72d5d2b36c
4
- data.tar.gz: a7af8fb9462ec663495f6b2e1e22f7bc4eb43dff
3
+ metadata.gz: 07e1154c47e72d096a9024f7b7a76168f3f64a93
4
+ data.tar.gz: abb32b7b26d2e5197d33d8489b101e9a65731b72
5
5
  SHA512:
6
- metadata.gz: d0190923aa93a44de691d83a584810665a9b3a6e44de789d8a645b1e4251d781b11e352f19215aaa662424bfd246c734cfc8dc6548554ccdf0ad2a44112de7c3
7
- data.tar.gz: 158173da755c512d0da9b495d310db92f78f8bd52404989d0fa9a44249df9ade746e19ae5bc7e1f1cddf97425fc2fc22d4f8abf10d192237b923b136701cb1e1
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
- attr_accessor source
21
+ define_attribute_method source
22
22
 
23
- define_method :"#{source}_changed?" do
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}_was", original_cds)
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
- return instance_variable if instance_variable
41
- instance_variable_set(:"@#{source}", send(:"#{source}_was").dup)
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).select(&:present?).map(&:to_i)
42
+ current_cds = send(source)
43
+
48
44
 
49
45
  # if any enum been removed
50
46
  if (original_cds - current_cds).any?
@@ -1,5 +1,5 @@
1
1
  module SimpleEnum
2
2
  module Multiple
3
- VERSION = "0.1.5"
3
+ VERSION = "0.1.6"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_enum-multiple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo Li