has_moderated 1.1.4 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/has_moderated/associations/base.rb +7 -2
- data/lib/has_moderated/carrier_wave.rb +44 -19
- data/lib/has_moderated/moderated_attributes.rb +2 -1
- data/lib/has_moderated/moderated_create.rb +3 -2
- data/lib/has_moderated/moderation_model.rb +19 -12
- data/lib/has_moderated/version.rb +1 -1
- data/test/dummy/log/test.log +17281 -0
- data/test/dummy/public/uploads/tmp/20121024-1607-1557-8676/test.jpg +0 -0
- data/test/dummy/public/uploads/tmp/20121024-1607-1557-8676/thumb_test.jpg +0 -0
- data/test/dummy/spec/models/photo_spec.rb +70 -14
- data/test/dummy/spec/models/task_spec.rb +7 -1
- metadata +8 -4
@@ -70,6 +70,7 @@ module HasModerated
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def self.apply_add_association(to, reflection, attrs)
|
73
|
+
preview_mode = to.instance_variable_get(:@has_moderated_preview)
|
73
74
|
klass = reflection.class_name.constantize
|
74
75
|
fk = HasModerated::ActiveRecordHelpers::foreign_key(reflection)
|
75
76
|
|
@@ -86,10 +87,12 @@ module HasModerated
|
|
86
87
|
# PARAM = ID
|
87
88
|
if attrs.class == Fixnum
|
88
89
|
arec = klass.find_by_id(attrs)
|
90
|
+
arec.instance_variable_set(:@has_moderated_preview, preview_mode)
|
89
91
|
add_assoc_to_record(rec, arec, reflection)
|
90
92
|
# PARAM = Hash (create)
|
91
93
|
elsif attrs.kind_of? Hash
|
92
94
|
arec = klass.new
|
95
|
+
arec.instance_variable_set(:@has_moderated_preview, preview_mode)
|
93
96
|
# set foreign key first, may be required sometimes
|
94
97
|
add_assoc_to_record(rec, arec, reflection)
|
95
98
|
attrs.each_pair do |key, val|
|
@@ -97,7 +100,7 @@ module HasModerated
|
|
97
100
|
arec.send(key.to_s+"=", val)
|
98
101
|
end
|
99
102
|
# recursive, used for has_many :through
|
100
|
-
apply(arec, attrs) if attrs[:associations].present?
|
103
|
+
apply(arec, attrs, preview_mode) if attrs[:associations].present?
|
101
104
|
else
|
102
105
|
raise "don't know how to parse #{attrs.class}"
|
103
106
|
end
|
@@ -108,6 +111,7 @@ module HasModerated
|
|
108
111
|
end
|
109
112
|
if reflection.collection?
|
110
113
|
rec = rec.reload
|
114
|
+
rec.instance_variable_set(:@has_moderated_preview, true) if preview_mode
|
111
115
|
#rec.send(reflection.name.to_s) << arec unless rec.send(reflection.name.to_s).include?(arec)
|
112
116
|
else
|
113
117
|
rec.send(reflection.name.to_s + "=", arec)
|
@@ -130,7 +134,8 @@ module HasModerated
|
|
130
134
|
end
|
131
135
|
|
132
136
|
# add/delete associations to a record
|
133
|
-
def self.apply(record, data)
|
137
|
+
def self.apply(record, data, preview_mode = false)
|
138
|
+
record.instance_variable_set(:@has_moderated_preview, true) if preview_mode
|
134
139
|
associations = data[:associations]
|
135
140
|
delete_associations = data[:delete_associations]
|
136
141
|
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'fileutils'
|
2
2
|
module HasModerated
|
3
3
|
module CarrierWave
|
4
|
-
|
4
|
+
|
5
5
|
def self.included(base)
|
6
6
|
base.send :extend, ClassMethods
|
7
|
-
end
|
8
|
-
|
7
|
+
end
|
8
|
+
|
9
9
|
def self.photo_tmp_delete(value)
|
10
10
|
begin
|
11
11
|
dirname = File.expand_path("..", value)
|
@@ -28,21 +28,44 @@ module HasModerated
|
|
28
28
|
def has_moderated_carrierwave_field field_names
|
29
29
|
base = self
|
30
30
|
base.send :include, InstanceMethods
|
31
|
-
|
31
|
+
|
32
32
|
cattr_accessor :moderated_carrierwave_fields
|
33
|
-
|
33
|
+
|
34
34
|
field_names = [field_names] unless field_names.kind_of? Array
|
35
|
-
|
35
|
+
|
36
36
|
field_names.each do |field_name|
|
37
37
|
field_name = field_name.to_s
|
38
38
|
self.moderated_carrierwave_fields ||= []
|
39
39
|
self.moderated_carrierwave_fields.push(field_name)
|
40
|
-
|
41
|
-
base.send :define_method, "#{field_name}_tmp_file=" do |
|
42
|
-
|
43
|
-
|
40
|
+
|
41
|
+
base.send :define_method, "#{field_name}_tmp_file=" do |tmp_filename|
|
42
|
+
if @has_moderated_preview != true
|
43
|
+
self.send("#{field_name}=", File.open(tmp_filename))
|
44
|
+
HasModerated::CarrierWave::photo_tmp_delete(tmp_filename)
|
45
|
+
elsif tmp_filename.present?
|
46
|
+
# preview
|
47
|
+
self.singleton_class.class_eval do
|
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
|
65
|
+
end
|
66
|
+
end
|
44
67
|
end
|
45
|
-
|
68
|
+
|
46
69
|
base.send :define_method, "store_#{field_name}_with_moderation!" do
|
47
70
|
is_moderated = self.class.respond_to?(:moderated_attributes) &&
|
48
71
|
self.class.moderated_attributes.include?(field_name)
|
@@ -56,26 +79,28 @@ module HasModerated
|
|
56
79
|
})
|
57
80
|
end
|
58
81
|
end
|
59
|
-
|
82
|
+
|
60
83
|
base.send :define_method, "write_#{field_name}_identifier_with_moderation" do
|
61
84
|
is_moderated = self.class.respond_to?(:moderated_attributes) &&
|
62
85
|
self.class.moderated_attributes.include?(field_name)
|
63
|
-
if
|
86
|
+
if !@has_moderated_preview &&
|
87
|
+
(!is_moderated || self.moderation_disabled || !self.send("#{field_name}_changed?"))
|
64
88
|
self.send("write_#{field_name}_identifier_without_moderation")
|
65
89
|
end
|
66
90
|
end
|
67
|
-
|
91
|
+
|
68
92
|
base.alias_method_chain :get_moderation_attributes, :carrierwave unless base.instance_methods.include?("get_moderation_attributes_without_carrierwave")
|
69
93
|
base.alias_method_chain "store_#{field_name}!", :moderation
|
70
94
|
base.alias_method_chain "write_#{field_name}_identifier", :moderation
|
71
95
|
end
|
72
96
|
end
|
73
|
-
|
97
|
+
|
74
98
|
# use class method because we only operate on hash parameters, not with a real record
|
75
99
|
# here we can delete the photo from tmp
|
76
|
-
def moderatable_discard(moderation)
|
100
|
+
def moderatable_discard(moderation, options)
|
101
|
+
return if options[:preview_mode]
|
77
102
|
value = moderation.parsed_data
|
78
|
-
|
103
|
+
|
79
104
|
moderated_carrierwave_fields.each do |field_name|
|
80
105
|
if value.kind_of? Hash
|
81
106
|
if value.has_key?(:create) && value[:create].has_key?(:attributes)
|
@@ -93,7 +118,7 @@ module HasModerated
|
|
93
118
|
end
|
94
119
|
end
|
95
120
|
end
|
96
|
-
|
121
|
+
|
97
122
|
module InstanceMethods
|
98
123
|
def get_moderation_attributes_with_carrierwave
|
99
124
|
attrs = get_moderation_attributes_without_carrierwave
|
@@ -106,4 +131,4 @@ module HasModerated
|
|
106
131
|
end
|
107
132
|
end
|
108
133
|
end
|
109
|
-
end
|
134
|
+
end
|
@@ -49,8 +49,9 @@ module HasModerated
|
|
49
49
|
end
|
50
50
|
|
51
51
|
module ApplyModeration
|
52
|
-
def self.apply(rec, value)
|
52
|
+
def self.apply(rec, value, preview_mode = false)
|
53
53
|
if value[:attributes].present?
|
54
|
+
rec.instance_variable_set(:@has_moderated_preview, true) if preview_mode
|
54
55
|
Moderation.without_moderation do
|
55
56
|
value[:attributes].each_pair do |attr_name, attr_value|
|
56
57
|
rec.send(attr_name.to_s+"=", attr_value)
|
@@ -16,17 +16,18 @@ module HasModerated
|
|
16
16
|
end
|
17
17
|
|
18
18
|
module ApplyModeration
|
19
|
-
def self.apply(klass, value)
|
19
|
+
def self.apply(klass, value, preview_mode = false)
|
20
20
|
rec = nil
|
21
21
|
if value[:create].present?
|
22
22
|
# create the main record
|
23
23
|
rec = klass.new
|
24
|
+
rec.instance_variable_set(:@has_moderated_preview, true) if preview_mode
|
24
25
|
attrs = value[:create][:attributes]
|
25
26
|
attrs && attrs.each_pair do |key, val|
|
26
27
|
rec.send(key.to_s+"=", val) unless key.to_s == 'id'
|
27
28
|
end
|
28
29
|
Moderation.without_moderation { rec.save(:validate => false) }
|
29
|
-
HasModerated::Associations::Base::ApplyModeration::apply(rec, value[:create])
|
30
|
+
HasModerated::Associations::Base::ApplyModeration::apply(rec, value[:create], preview_mode)
|
30
31
|
end
|
31
32
|
rec
|
32
33
|
end
|
@@ -23,14 +23,14 @@ module HasModerated
|
|
23
23
|
@parsed_data ||= YAML::load(data)
|
24
24
|
end
|
25
25
|
|
26
|
-
def apply
|
26
|
+
def apply(preview_mode = false)
|
27
27
|
if create?
|
28
|
-
record = HasModerated::ModeratedCreate::ApplyModeration::apply(moderatable_type.constantize, parsed_data)
|
28
|
+
record = HasModerated::ModeratedCreate::ApplyModeration::apply(moderatable_type.constantize, parsed_data, preview_mode)
|
29
29
|
else
|
30
30
|
record = moderatable
|
31
31
|
if record
|
32
|
-
record = HasModerated::ModeratedAttributes::ApplyModeration::apply(record, parsed_data)
|
33
|
-
record = HasModerated::Associations::Base::ApplyModeration::apply(record, parsed_data)
|
32
|
+
record = HasModerated::ModeratedAttributes::ApplyModeration::apply(record, parsed_data, preview_mode)
|
33
|
+
record = HasModerated::Associations::Base::ApplyModeration::apply(record, parsed_data, preview_mode)
|
34
34
|
record = HasModerated::ModeratedDestroy::ApplyModeration::apply(record, parsed_data)
|
35
35
|
end
|
36
36
|
end
|
@@ -47,26 +47,27 @@ module HasModerated
|
|
47
47
|
record
|
48
48
|
end
|
49
49
|
|
50
|
-
def accept!(save_opts = Hash.new)
|
51
|
-
record = apply
|
50
|
+
def accept!(save_opts = Hash.new, preview_mode = false)
|
51
|
+
record = apply(preview_mode)
|
52
52
|
accept_changes(record, save_opts)
|
53
|
-
self.destroy
|
53
|
+
self.destroy(:preview_mode => preview_mode)
|
54
54
|
record
|
55
55
|
end
|
56
56
|
|
57
|
-
def accept(save_opts = Hash.new)
|
57
|
+
def accept(save_opts = Hash.new, preview_mode = false)
|
58
58
|
begin
|
59
|
-
accept!(save_opts)
|
59
|
+
accept!(save_opts, preview_mode)
|
60
60
|
true
|
61
61
|
rescue
|
62
62
|
false
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
def destroy_with_moderation_callbacks
|
66
|
+
def destroy_with_moderation_callbacks(*args)
|
67
|
+
options = args.first || Hash.new
|
67
68
|
if moderatable_type
|
68
69
|
klass = moderatable_type.constantize
|
69
|
-
klass.moderatable_discard(self) if klass.respond_to?(:moderatable_discard)
|
70
|
+
klass.moderatable_discard(self, options) if klass.respond_to?(:moderatable_discard)
|
70
71
|
end
|
71
72
|
destroy_without_moderation_callbacks
|
72
73
|
end
|
@@ -76,8 +77,14 @@ module HasModerated
|
|
76
77
|
end
|
77
78
|
|
78
79
|
def live_preview
|
80
|
+
# absolutely no point to preview a destroy moderation
|
81
|
+
if destroy? && parsed_data.keys.count == 1
|
82
|
+
yield(nil)
|
83
|
+
return nil
|
84
|
+
end
|
85
|
+
|
79
86
|
self.transaction do
|
80
|
-
record = accept!(:perform_validation => false)
|
87
|
+
record = accept!({ :perform_validation => false }, true)
|
81
88
|
yield(record)
|
82
89
|
raise ActiveRecord::Rollback
|
83
90
|
end
|