Module: HasModerated::CarrierWave::ClassMethods

Defined in:
lib/has_moderated/carrier_wave.rb

Instance Method Summary collapse

Instance Method Details

#has_moderated_carrierwave_field(field_names) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/has_moderated/carrier_wave.rb', line 32

def has_moderated_carrierwave_field field_names
  base = self
  base.send :include, InstanceMethods

  cattr_accessor :moderated_carrierwave_fields

  field_names = [field_names] unless field_names.kind_of? Array

  field_names.each do |field_name|
    field_name = field_name.to_s
    self.moderated_carrierwave_fields ||= []
    self.moderated_carrierwave_fields.push(field_name)

    base.send :define_method, :"#{field_name}_tmp_file=" do |tmp_filename|
      if tmp_filename.is_a? String
        if @has_moderated_preview
          write_attribute field_name.to_s, tmp_filename
        else
          tmp_filename =~ /\/([^\/]+\/[^\/]+)\z/
          send(field_name).retrieve_from_cache!($1)
        end
      else
        send(:"#{field_name}=", tmp_filename)
      end
    end
    base.send :define_method, "store_#{field_name}_with_moderation!" do
      is_moderated = self.class.respond_to?(:moderated_attributes) &&
        self.class.moderated_attributes.include?(field_name)
      if !is_moderated || self.moderation_disabled || !self.send("#{field_name}_changed?")
        self.send("store_#{field_name}_without_moderation!")
      else
        self.create_moderation_with_hooks!({
          :attributes => {
            "#{field_name}_tmp_file" => self.send(field_name).file.file
          }
        })
      end
    end

    base.send :define_method, "write_#{field_name}_identifier_with_moderation" do
      is_moderated = self.class.respond_to?(:moderated_attributes) &&
        self.class.moderated_attributes.include?(field_name)
      if !@has_moderated_preview &&
        (!is_moderated || self.moderation_disabled || !self.send("#{field_name}_changed?"))
        self.send("write_#{field_name}_identifier_without_moderation")
      end
    end

    base.alias_method_chain :get_moderation_attributes, :carrierwave unless base.instance_methods.include?("get_moderation_attributes_without_carrierwave")
    base.alias_method_chain "store_#{field_name}!", :moderation
    base.alias_method_chain "write_#{field_name}_identifier", :moderation
  end
end

#moderatable_discard(moderation, options) ⇒ Object

use class method because we only operate on hash parameters, not with a real record here we can delete the photo from tmp



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/has_moderated/carrier_wave.rb', line 88

def moderatable_discard(moderation, options)
  return if options[:preview_mode]
  value = moderation.parsed_data

  moderated_carrierwave_fields.each do |field_name|
    if value.kind_of? Hash
      if value.has_key?(:create) && value[:create].has_key?(:attributes)
        value = value[:create]
      end
      if value.has_key?(:attributes) && value[:attributes].has_key?("#{field_name}_tmp_file")
        value = value[:attributes]["#{field_name}_tmp_file"]
        if value.present?
          HasModerated::CarrierWave::photo_tmp_delete(value)
        end
      else
        return # we dont want to process anything else than the above
      end
    end
  end
end