Module: CarrierWave::CardMount

Includes:
Mount
Defined in:
lib/carrier_wave/card_mount.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

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



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/carrier_wave/card_mount.rb', line 19

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, 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 attachment
      #{column}
    end

    def store_attachment!
      set_specific.delete :#{column}
      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?
      self.selected_action_id = Time.now.to_i unless history?
      assign_file(new_file) { super }
    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

    def serializable_hash(opts=nil)
      except = opts&.dig(:except) && Array.wrap(opts[:except]).map(&:to_s)
      only = opts&.dig(:only) && Array.wrap(opts[:only]).map(&:to_s)

      self.class.uploaders.each_with_object(super(opts)) do |(column, uploader), hash|
        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
    end

    def reload(*)
      @_mounters = nil
      super
    end
  RUBY
end

#uploader_optionsObject



15
16
17
# File 'lib/carrier_wave/card_mount.rb', line 15

def uploader_options
  Card.uploader_options ||= {}
end

#uploadersObject



11
12
13
# File 'lib/carrier_wave/card_mount.rb', line 11

def uploaders
  Card.uploaders ||= {}
end