Module: CarrierWave::CardMount

Includes:
Mount
Defined in:
mod/carrierwave/lib/carrier_wave/card_mount.rb,
mod/carrierwave/lib/carrier_wave/card_mount/helper.rb

Overview

Adapt carrierwave mount to cards. We translate the active record hooks in https://github.com/carrierwaveuploader/carrierwave/blob/v3.0.5/lib/carrierwave/orm/activerecord.rb to card events.

Defined Under Namespace

Modules: Helper

Instance Method Summary collapse

Instance Method Details

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



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
112
113
114
115
116
117
118
119
120
121
122
123
# File 'mod/carrierwave/lib/carrier_wave/card_mount.rb', line 20

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

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    include CarrierWave::CardMount::Helper

    def attachment
      #{column}
    end

    event :store_#{column}_event, :finalize, when: :store_#{column}_event? do
      store_#{column}!
    end

    # remove files only if card has no history
    event :remove_#{column}_event, :finalize, on: :delete, when: :no_history? do
      remove_#{column}!
    end

    event :mark_remove_#{column}_false_event, :finalize, on: :update do
      mark_remove_#{column}_false
    end

    event :reset_previous_changes_for_#{column}_event, :finalize,
          when: :no_history? do
      reset_previous_changes_for_#{column}
    end

    event :remove_previously_stored_#{column}_event, :finalize, on: :update,
          when: :no_history? do
      remove_previously_stored_#{column}
    end

    # don't attempt to store coded images unless ENV specifies it
    def store_#{column}_event?
      !coded? || ENV["STORE_CODED_FILES"]
    end

    def store_attachment!
      set_specific.delete :#{column}
      store_#{column}!
    end

    def attachment_name
      "#{column}".to_sym
    end

    def #{column}=(new_file)
      return if new_file.blank? || identical_file?(new_file)
      self.selected_action_id = Time.now.to_i unless history?
      assign_file(new_file) { super }
    end

    def identical_file? new_file
      return false if new?

      ::File.identical? #{column}.file.to_file, new_file
    rescue StandardError
      false
    end

    def remote_#{column}_url=(url)
      assign_file(url) { super }
    end

    def assign_file file
      db_column = _mounter(:#{column}).serialization_column
      send(:"\#{db_column}_will_change!") unless duplicate?
      if web?
        self.content = file
      else
        send(:"#{column}_will_change!") unless duplicate?
        yield
      end
    end

    def remove_#{column}=(value)
      column = _mounter(:#{column}).serialization_column
      send(:"\#{column}_will_change!")
      super
    end

    def remove_#{column}!
      self.remove_#{column} = true
      write_#{column}_identifier
      self.remove_#{column} = false
      super
    end

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

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

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

#uploader_optionsObject



16
17
18
# File 'mod/carrierwave/lib/carrier_wave/card_mount.rb', line 16

def uploader_options
  Card.uploader_options ||= {}
end

#uploadersObject

Helper # load helper module so it’s available to mounted cards



12
13
14
# File 'mod/carrierwave/lib/carrier_wave/card_mount.rb', line 12

def uploaders
  Card.uploaders ||= {}
end