has_moderated 1.1.5 → 1.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/has_moderated/associations/base.rb +5 -6
- data/lib/has_moderated/associations/has_one.rb +4 -4
- data/lib/has_moderated/carrier_wave.rb +45 -25
- data/lib/has_moderated/moderated_create.rb +3 -3
- data/lib/has_moderated/moderation_model.rb +4 -4
- data/lib/has_moderated/moderation_preview.rb +11 -11
- data/lib/has_moderated/version.rb +1 -1
- data/test/dummy/log/test.log +34042 -0
- data/test/dummy/public/uploads/tmp/{20121024-1607-1557-8676 → 20121031-1115-2165-7602}/test.jpg +0 -0
- data/test/dummy/public/uploads/tmp/{20121024-1607-1557-8676 → 20121031-1115-2165-7602}/thumb_test.jpg +0 -0
- data/test/dummy/spec/models/photo_spec.rb +113 -1
- data/test/dummy/spec/support/photos.rb +3 -3
- metadata +8 -8
@@ -69,7 +69,7 @@ module HasModerated
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
def self.apply_add_association(to, reflection, attrs)
|
72
|
+
def self.apply_add_association(to, reflection, attrs, save_opts = Hash.new)
|
73
73
|
preview_mode = to.instance_variable_get(:@has_moderated_preview)
|
74
74
|
klass = reflection.class_name.constantize
|
75
75
|
fk = HasModerated::ActiveRecordHelpers::foreign_key(reflection)
|
@@ -100,14 +100,13 @@ module HasModerated
|
|
100
100
|
arec.send(key.to_s+"=", val)
|
101
101
|
end
|
102
102
|
# recursive, used for has_many :through
|
103
|
-
apply(arec, attrs, preview_mode) if attrs[:associations].present?
|
103
|
+
apply(arec, attrs, save_opts, preview_mode) if attrs[:associations].present?
|
104
104
|
else
|
105
105
|
raise "don't know how to parse #{attrs.class}"
|
106
106
|
end
|
107
107
|
if arec
|
108
108
|
try_without_moderation(arec) do
|
109
|
-
arec.save(
|
110
|
-
# TODO: validations? sup?
|
109
|
+
arec.save(save_opts)
|
111
110
|
end
|
112
111
|
if reflection.collection?
|
113
112
|
rec = rec.reload
|
@@ -134,7 +133,7 @@ module HasModerated
|
|
134
133
|
end
|
135
134
|
|
136
135
|
# add/delete associations to a record
|
137
|
-
def self.apply(record, data, preview_mode = false)
|
136
|
+
def self.apply(record, data, save_opts = Hash.new, preview_mode = false)
|
138
137
|
record.instance_variable_set(:@has_moderated_preview, true) if preview_mode
|
139
138
|
associations = data[:associations]
|
140
139
|
delete_associations = data[:delete_associations]
|
@@ -143,7 +142,7 @@ module HasModerated
|
|
143
142
|
reflection = record.class.reflections[assoc_name.to_sym]
|
144
143
|
|
145
144
|
assoc_records.each do |attrs|
|
146
|
-
apply_add_association(record, reflection, attrs) if attrs.present?
|
145
|
+
apply_add_association(record, reflection, attrs, save_opts) if attrs.present?
|
147
146
|
end
|
148
147
|
end
|
149
148
|
|
@@ -16,7 +16,7 @@ module HasModerated
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
module ClassMethods
|
21
21
|
protected
|
22
22
|
def has_moderated_has_one_association(reflection)
|
@@ -41,13 +41,13 @@ module HasModerated
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
module AssociationHelpers
|
46
46
|
def self.add_assoc_to_record(*args)
|
47
47
|
# same as HasMany
|
48
48
|
HasModerated::Associations::Collection::AssociationHelpers::add_assoc_to_record(*args)
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
def self.delete_assoc_from_record(from, assoc_id, reflection)
|
52
52
|
from.send("#{reflection.name}=", nil)
|
53
53
|
from.save # TODO necessary?
|
@@ -55,4 +55,4 @@ module HasModerated
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
58
|
-
end
|
58
|
+
end
|
@@ -1,4 +1,40 @@
|
|
1
1
|
require 'fileutils'
|
2
|
+
require 'carrierwave'
|
3
|
+
module ::CarrierWave
|
4
|
+
class HasModeratedTempFile < SanitizedFile
|
5
|
+
def initialize(file)
|
6
|
+
super(file)
|
7
|
+
end
|
8
|
+
|
9
|
+
def move_to(*args)
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def copy_to(*args)
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def delete(*args)
|
18
|
+
false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module Storage
|
23
|
+
class File
|
24
|
+
def retrieve_with_moderation_preview!(identifier)
|
25
|
+
tmp_path = Pathname.new(Rails.public_path).join(@uploader.class.store_dir).join("tmp").to_s
|
26
|
+
|
27
|
+
if identifier.try(:start_with?, tmp_path)
|
28
|
+
::CarrierWave::HasModeratedTempFile.new(identifier)
|
29
|
+
else
|
30
|
+
retrieve_without_moderation_preview!(identifier)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
alias_method_chain :retrieve!, :moderation_preview
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
2
38
|
module HasModerated
|
3
39
|
module CarrierWave
|
4
40
|
|
@@ -38,34 +74,18 @@ module HasModerated
|
|
38
74
|
self.moderated_carrierwave_fields ||= []
|
39
75
|
self.moderated_carrierwave_fields.push(field_name)
|
40
76
|
|
41
|
-
base.send :define_method, "#{field_name}_tmp_file=" do |tmp_filename|
|
42
|
-
if
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
define_method :"#{field_name}_with_preview" do |*args, &block|
|
49
|
-
uploader = send(:"#{field_name}_without_preview", *args, &block)
|
50
|
-
unless uploader.frozen?
|
51
|
-
uploader.instance_variable_set(:@file,
|
52
|
-
::CarrierWave::SanitizedFile.new(
|
53
|
-
File.open(tmp_filename, "rb")))
|
54
|
-
uploader.freeze
|
55
|
-
end
|
56
|
-
uploader
|
57
|
-
end
|
58
|
-
define_method :"#{field_name}?" do
|
59
|
-
true
|
60
|
-
end
|
61
|
-
define_method :"#{field_name}_url" do
|
62
|
-
send("#{field_name}").url
|
63
|
-
end
|
64
|
-
alias_method_chain :"#{field_name}", :preview
|
77
|
+
base.send :define_method, :"#{field_name}_tmp_file=" do |tmp_filename|
|
78
|
+
if tmp_filename.is_a? String
|
79
|
+
if @has_moderated_preview
|
80
|
+
write_attribute field_name.to_s, tmp_filename
|
81
|
+
else
|
82
|
+
tmp_filename =~ /\/([^\/]+\/[^\/]+)\z/
|
83
|
+
send(field_name).retrieve_from_cache!($1)
|
65
84
|
end
|
85
|
+
else
|
86
|
+
send(:"#{field_name}=", tmp_filename)
|
66
87
|
end
|
67
88
|
end
|
68
|
-
|
69
89
|
base.send :define_method, "store_#{field_name}_with_moderation!" do
|
70
90
|
is_moderated = self.class.respond_to?(:moderated_attributes) &&
|
71
91
|
self.class.moderated_attributes.include?(field_name)
|
@@ -16,7 +16,7 @@ module HasModerated
|
|
16
16
|
end
|
17
17
|
|
18
18
|
module ApplyModeration
|
19
|
-
def self.apply(klass, value, preview_mode = false)
|
19
|
+
def self.apply(klass, value, save_opts = Hash.new, preview_mode = false)
|
20
20
|
rec = nil
|
21
21
|
if value[:create].present?
|
22
22
|
# create the main record
|
@@ -26,8 +26,8 @@ module HasModerated
|
|
26
26
|
attrs && attrs.each_pair do |key, val|
|
27
27
|
rec.send(key.to_s+"=", val) unless key.to_s == 'id'
|
28
28
|
end
|
29
|
-
Moderation.without_moderation { rec.save(
|
30
|
-
HasModerated::Associations::Base::ApplyModeration::apply(rec, value[:create], preview_mode)
|
29
|
+
Moderation.without_moderation { rec.save(save_opts) }
|
30
|
+
HasModerated::Associations::Base::ApplyModeration::apply(rec, value[:create], save_opts, preview_mode)
|
31
31
|
end
|
32
32
|
rec
|
33
33
|
end
|
@@ -23,14 +23,14 @@ module HasModerated
|
|
23
23
|
@parsed_data ||= YAML::load(data)
|
24
24
|
end
|
25
25
|
|
26
|
-
def apply(preview_mode = false)
|
26
|
+
def apply(save_opts = Hash.new, preview_mode = false)
|
27
27
|
if create?
|
28
|
-
record = HasModerated::ModeratedCreate::ApplyModeration::apply(moderatable_type.constantize, parsed_data, preview_mode)
|
28
|
+
record = HasModerated::ModeratedCreate::ApplyModeration::apply(moderatable_type.constantize, parsed_data, save_opts, preview_mode)
|
29
29
|
else
|
30
30
|
record = moderatable
|
31
31
|
if record
|
32
32
|
record = HasModerated::ModeratedAttributes::ApplyModeration::apply(record, parsed_data, preview_mode)
|
33
|
-
record = HasModerated::Associations::Base::ApplyModeration::apply(record, parsed_data, preview_mode)
|
33
|
+
record = HasModerated::Associations::Base::ApplyModeration::apply(record, parsed_data, save_opts, preview_mode)
|
34
34
|
record = HasModerated::ModeratedDestroy::ApplyModeration::apply(record, parsed_data)
|
35
35
|
end
|
36
36
|
end
|
@@ -48,7 +48,7 @@ module HasModerated
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def accept!(save_opts = Hash.new, preview_mode = false)
|
51
|
-
record = apply(preview_mode)
|
51
|
+
record = apply(save_opts, preview_mode)
|
52
52
|
accept_changes(record, save_opts)
|
53
53
|
self.destroy(:preview_mode => preview_mode)
|
54
54
|
record
|
@@ -5,7 +5,7 @@ module HasModerated
|
|
5
5
|
def update_moderation
|
6
6
|
if @based_on_moderation.present?
|
7
7
|
data = @based_on_moderation.parsed_data
|
8
|
-
|
8
|
+
|
9
9
|
# attributes
|
10
10
|
data[:attributes] ||= Hash.new
|
11
11
|
@has_moderated_fake_attributes.each_pair do |key, value|
|
@@ -13,13 +13,13 @@ module HasModerated
|
|
13
13
|
data[:attributes][key.to_s] = value
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
@based_on_moderation.data = data
|
18
18
|
@based_on_moderation.save
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
module FakeRecord
|
24
24
|
def fake!(based_on_model, based_on_moderation)
|
25
25
|
@based_on_model = based_on_model
|
@@ -27,7 +27,7 @@ module HasModerated
|
|
27
27
|
freeze
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def self.resolve_record(record, cache)
|
32
32
|
return nil if record.blank?
|
33
33
|
idx = cache.index(record)
|
@@ -38,20 +38,20 @@ module HasModerated
|
|
38
38
|
cache.last
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def self.from_live(record, moderation = nil, saveable = false, object_cache = [])
|
43
43
|
return nil if record.blank?
|
44
44
|
obj = record
|
45
45
|
eigenclass = (class << obj ; include FakeRecord ; self ; end)
|
46
46
|
obj.fake!(record.class, moderation)
|
47
|
-
|
47
|
+
|
48
48
|
# attributes
|
49
49
|
changed_attributes = Hash.new
|
50
50
|
record.previous_changes.each_pair do |attr_name, values|
|
51
51
|
changed_attributes[attr_name] = values[0]
|
52
52
|
end
|
53
53
|
obj.instance_variable_set(:@changed_attributes, changed_attributes)
|
54
|
-
|
54
|
+
|
55
55
|
# saveable
|
56
56
|
if saveable
|
57
57
|
obj.instance_variable_set(:@attributes_initial, obj.instance_variable_get(:@attributes).dup)
|
@@ -63,7 +63,7 @@ module HasModerated
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
# associations
|
68
68
|
object_cache.push(obj)
|
69
69
|
has_moderated_fake_associations = HashWithIndifferentAccess.new
|
@@ -74,15 +74,15 @@ module HasModerated
|
|
74
74
|
has_moderated_fake_associations[reflection.name] = record.send(reflection.name).map{|r| resolve_record(r, object_cache)}
|
75
75
|
end
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
obj.instance_variable_set(:@has_moderated_fake_associations, has_moderated_fake_associations.freeze)
|
79
79
|
eigenclass.class_eval do
|
80
80
|
def association(name)
|
81
81
|
OpenStruct.new(:reader => @has_moderated_fake_associations[name].freeze)
|
82
82
|
end
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
85
|
obj
|
86
86
|
end
|
87
87
|
end
|
88
|
-
end
|
88
|
+
end
|