Module: CarrierWave::CardMount

Includes:
Mount
Defined in:
mod/05_standard/lib/carrier_wave/cardmount.rb

Instance Method Summary collapse

Instance Method Details

#mount_uploader(column, uploader = nil, options = {}, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'mod/05_standard/lib/carrier_wave/cardmount.rb', line 15

def mount_uploader column, uploader=nil, options={}, &block
  options[:mount_on] ||= :db_content
  super

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    event :store_#{column}_event, :finalize, on: :save do
      store_#{column}!
    end

    # remove files only if card has no history
    event :remove_#{column}_event, :finalize,
          on: :delete, when: proc { |c| !c.history? } do
      remove_#{column}!
    end
    event :mark_remove_#{column}_false_event, :finalize,
          on: :update do
      mark_remove_#{column}_false
    end
    event :store_previous_model_for_#{column}_event, :store,
          on: :update, when: proc { |c| !c.history? } do
      store_previous_model_for_#{column}
    end
    event :remove_previously_stored_#{column}_event, :store,
          on: :update, when: proc { |c| !c.history?} do
      if @previous_model_for_#{column}
        @previous_model_for_#{column}.include_set_modules
      end
      remove_previously_stored_#{column}
    end

    def attachment
      #{column}
    end

    def store_attachment!
      store_#{column}!
    end

    def attachment_name
      "#{column}".to_sym
    end

    def read_uploader *args
      read_attribute *args
    end

    def write_uploader *args
      write_attribute *args
    end

    def #{column}=(new_file)
      return if new_file.blank?
      send(:"#{column}_will_change!")
      db_column = _mounter(:#{column}).serialization_column
      send(:"\#{db_column}_will_change!")
      super
    end

    def remote_#{column}_url=(url)
      send(:"#{column}_will_change!")
      db_column = _mounter(:#{column}).serialization_column
      send(:"\#{db_column}_will_change!")
      super
    end

    def remove_#{column}!
      super
      _mounter(:#{column}).remove = true
      _mounter(:#{column}).write_identifier
    end

    def #{column}_will_change!
      @#{column}_changed = true
    end

    def #{column}_changed?
      @#{column}_changed
    end

    def serializable_hash(options=nil)
      hash = {}

      except = options && options[:except] &&
               Array.wrap(options[:except]).map(&:to_s)
      only   = options && options[:only]   &&
               Array.wrap(options[:only]).map(&:to_s)

      self.class.uploaders.each do |column, uploader|
        if (!only && !except) || (only && only.include?(column.to_s)) ||
           (!only && except && !except.include?(column.to_s))
          hash[column.to_s] = _mounter(column).uploader.serializable_hash
        end
      end
      super(options).merge(hash)
    end
  RUBY
end

#uploader_optionsObject



11
12
13
# File 'mod/05_standard/lib/carrier_wave/cardmount.rb', line 11

def uploader_options
  Card.uploader_options ||= {}
end

#uploadersObject



7
8
9
# File 'mod/05_standard/lib/carrier_wave/cardmount.rb', line 7

def uploaders
  Card.uploaders ||= {}
end