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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'mod/carrierwave/lib/carrier_wave/card_mount.rb', line 16
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, when: :store_#{column}_event? 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_changes_for_#{column}_event, :store,
on: :update, when: proc { |c| !c.history? } do
store_previous_changes_for_#{column}
end
event :remove_previously_stored_#{column}_event, :finalize,
on: :update, when: proc { |c| !c.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!
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?
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 attribute_is_changing? db_column
if web?
self.content = file
else
send(:"#{column}_will_change!")
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(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
|