is_graffitiable 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/VERSION.yml +1 -1
  2. data/lib/is_graffitiable.rb +32 -13
  3. metadata +3 -3
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 1
3
- :patch: 1
3
+ :patch: 2
4
4
  :major: 0
@@ -13,8 +13,9 @@ module IsGraffitiable
13
13
 
14
14
  module ClassMethods
15
15
  def is_graffitiable
16
- has_many :graffitis, :as => :graffitiable
17
-
16
+ has_many :graffitis, :as => :graffitiable, :dependent => :destroy
17
+
18
+ before_validation :initialize_graffitis, :validate_graffitis
18
19
  after_create :save_graffitis
19
20
  after_update :save_graffitis
20
21
 
@@ -31,27 +32,45 @@ module IsGraffitiable
31
32
  @graffiti_map
32
33
  end
33
34
 
34
- def save_graffitis
35
+ def initialize_graffitis
35
36
  return unless @graffiti_map
36
-
37
37
  new_graffiti_map = @graffiti_map.clone
38
- updated_graffiti_map = {}
39
-
38
+ @new_graffitis = []
39
+
40
40
  graffitis.each do |graffiti|
41
41
  new_graffiti_map.delete(graffiti.name)
42
42
  end
43
43
 
44
- old_graffitis = graffitis.reject{|graffiti| @graffiti_map.keys.include?(graffiti.name)}
45
-
44
+ @old_graffitis = graffitis.reject{|graffiti| @graffiti_map.keys.include?(graffiti.name)}
45
+
46
+ new_graffiti_map.each do |key,value|
47
+ @new_graffitis << Graffiti.create(:name => key, :value => value)
48
+ end
49
+ end
50
+
51
+ def validate_graffitis
52
+ return true unless @graffiti_map
53
+
54
+ @new_graffitis.each do |graffiti|
55
+ unless graffiti.valid?
56
+ errors.add("Graffiti", "is invalid")
57
+ return false
58
+ end
59
+ end
60
+ return true
61
+ end
62
+
63
+ def save_graffitis
64
+ return unless @graffiti_map
65
+
46
66
  self.class.transaction do
47
- if old_graffitis.any?
48
- old_graffitis.each(&:destroy)
67
+ if @old_graffitis.any?
68
+ @old_graffitis.each(&:destroy)
49
69
  end
50
- new_graffiti_map.each do |key,value|
51
- graffitis << Graffiti.create(:name => key, :value => value)
70
+ @new_graffitis.each do |graffiti|
71
+ graffitis << graffiti
52
72
  end
53
73
  end
54
-
55
74
  true
56
75
  end
57
76
  end # InstanceMethods
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: is_graffitiable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Darren Dao