has_moderated 0.0.3 → 0.0.4

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.
@@ -2,8 +2,14 @@ class Moderation < ActiveRecord::Base
2
2
  belongs_to :moderatable, :polymorphic => true
3
3
 
4
4
  def accept
5
+ # case: moderated destruction
6
+ if attr_name == '-' && attr_value.class == String && attr_value == "destroy"
7
+ moderatable.has_moderated_updating = true
8
+ moderatable.destroy
9
+ moderatable.has_moderated_updating = false
10
+ self.destroy
5
11
  # case: moderated existance (new record)
6
- if attr_name == '-'
12
+ elsif attr_name == '-'
7
13
  # create the main record
8
14
  rec = moderatable_type.constantize.new
9
15
  loaded_val = YAML::load(attr_value)
@@ -25,20 +31,25 @@ class Moderation < ActiveRecord::Base
25
31
 
26
32
  # all records for this associated model
27
33
  assoc_records.each do |attrs|
28
- arec = m.new # new associated model
29
- attrs.each_pair do |key, val|
30
- arec.send(key.to_s+"=", val) unless key.to_s == 'id'
31
- end
32
- fk = if assoc_details.respond_to?(:foreign_key)
33
- assoc_details.foreign_key
34
- else # version < 3.1
35
- assoc_details.primary_key_name
34
+ if attrs.class == Fixnum # associate to existing record
35
+ arec = m.find_by_id(attrs)
36
+ rec.send(assoc_name.to_s) << arec if arec # add the association, if the record still exists
37
+ else # create a new record
38
+ arec = m.new # new associated model
39
+ attrs.each_pair do |key, val|
40
+ arec.send(key.to_s+"=", val) unless key.to_s == 'id'
41
+ end
42
+ fk = if assoc_details.respond_to?(:foreign_key)
43
+ assoc_details.foreign_key
44
+ else # version < 3.1
45
+ assoc_details.primary_key_name
46
+ end
47
+ arec.send(fk.to_s+"=", rec.id) # set association to the newly created record
48
+ # disable moderation for associated model (if enabled)
49
+ arec.has_moderated_updating = true if arec.respond_to?("has_moderated_updating=")
50
+ arec.save
51
+ arec.has_moderated_updating = false if arec.respond_to?("has_moderated_updating=")
36
52
  end
37
- arec.send(fk.to_s+"=", rec.id) # set association to the newly created record
38
- # disable moderation for associated model (if enabled)
39
- arec.has_moderated_updating = true if arec.respond_to?("has_moderated_updating=")
40
- arec.save
41
- arec.has_moderated_updating = false if arec.respond_to?("has_moderated_updating=")
42
53
  end
43
54
  end
44
55
  self.destroy # destroy this moderation since it has been applied
@@ -1,3 +1,3 @@
1
1
  module HasModerated
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_moderated
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jan Berdajs