Module: Card::Set::Abstract::Attachment

Extended by:
Card::Set
Defined in:
tmpsets/set/mod007-05_standard/abstract/attachment.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

abstract_set?, all_set?, card_accessor, card_reader, card_writer, clean_empty_module_from_hash, clean_empty_modules, define_on_format, ensure_set, extended, format, process_base_module_list, process_base_modules, register_set, register_set_format, shortname, stage_method, view, write_tmp_file

Methods included from Event

#define_event, #event

Class Method Details

.included(host_class) ⇒ Object



6
7
8
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 6

def self.included host_class
  host_class.extend CarrierWave::CardMount
end

Instance Method Details

#action_id_of_cached_uploadObject



152
153
154
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 152

def action_id_of_cached_upload
  @action_id_of_cached_upload
end

#action_id_of_cached_upload=(value) ⇒ Object

action id of the cached upload



148
149
150
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 148

def action_id_of_cached_upload= value
  @action_id_of_cached_upload = value
end

#assign_attachment(file, original_filename) ⇒ Object



57
58
59
60
61
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 57

def assign_attachment file, original_filename
  send "#{attachment_name}=", file
  write_identifier
  @current_action.update_attributes! comment: original_filename
end

#assign_set_specific_attributesObject



232
233
234
235
236
237
238
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 232

def assign_set_specific_attributes
  # reset content if we really have something to upload
  if @set_specific.present? && @set_specific[attachment_name.to_s].present?
    self.content = nil
  end
  super
end

#attachment_changed?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 134

def attachment_changed?
  send "#{attachment_name}_changed?"
end

#attachment_format(ext) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 282

def attachment_format ext
  if ext.present? && attachment && (original_ext = attachment.extension)
    if['file', original_ext].member? ext
      original_ext
    elsif (exts = MIME::Types[attachment.content_type])
      if exts.find { |mt| mt.extensions.member? ext }
        ext
      else
        exts[0].extensions[0]
      end
    end
  end
rescue => e
  Rails.logger.info "attachment_format issue: #{e.message}"
  nil
end

#clear_upload_cache_dir_for_new_cardsObject



240
241
242
243
244
245
246
247
248
249
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 240

def clear_upload_cache_dir_for_new_cards
  Dir.entries(tmp_upload_dir).each do |filename|
    if filename =~/^\d+/
      path = File.join(tmp_upload_dir, filename )
      if Card.older_than_five_days? File.ctime(path)
        FileUtils.rm path
      end
    end
  end
end

#create_versions?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 138

def create_versions?
  true
end

#delete_files_for_action(action) ⇒ Object



252
253
254
255
256
257
258
259
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 252

def delete_files_for_action action
  with_selected_action_id(action.id) do
    FileUtils.rm attachment.file.path
    attachment.versions.each_value do |version|
      FileUtils.rm version.path
    end
  end
end

#empty_ok=(value) ⇒ Object



156
157
158
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 156

def empty_ok= value
  @empty_ok = value
end

#empty_ok?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 160

def empty_ok?
  @empty_ok
end

#file_ready_to_save?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 70

def file_ready_to_save?
  attachment.file.present? &&
    !preliminary_upload? &&
    !save_preliminary_upload? &&
    attachment_changed?
end

#item_names(_args = {}) ⇒ Object

needed for flexmail attachments. hacky.



114
115
116
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 114

def item_names _args={} # needed for flexmail attachments.  hacky.
  [cardname]
end

#load_from_modObject



170
171
172
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 170

def load_from_mod
  @mod
end

#load_from_mod=(value) ⇒ Object



164
165
166
167
168
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 164

def load_from_mod= value
  @mod = value
  write_identifier
  @store_in_mod = true if value
end

#mod_dirObject

place for files of mod file cards



205
206
207
208
209
210
211
212
213
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 205

def mod_dir
  mod = @mod || mod_file?
  Card.paths['mod'].to_a.each do |mod_path|
    dir = File.join(mod_path, mod, 'file', codename)
    if Dir.exist? dir
      return dir
    end
  end
end

#mod_file?Boolean

Returns:

  • (Boolean)


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 215

def mod_file?
  if @store_in_mod
    return @mod
  # when db_content was changed assume that it's no longer a mod file
  elsif !db_content_changed? && content.present?
    case content
    when %r{^:[^/]+/([^.]+)} then $1     # current mod_file format
    when /^\~/               then false  # current id file format
    else
      if (lines = content.split("\n")) && (lines.size == 4)
        # old format, still used in card_changes.
        lines.last
      end
    end
  end
end

#original_filenameObject



118
119
120
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 118

def original_filename
  attachment.original_filename
end

#preliminary_upload?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 126

def preliminary_upload?
  Card::Env && Card::Env.params[:attachment_upload]
end

#retrieve_dirObject



182
183
184
185
186
187
188
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 182

def retrieve_dir
  if mod_file?
    mod_dir
  else
    upload_dir
  end
end

#save_preliminary_upload?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 130

def save_preliminary_upload?
  @action_id_of_cached_upload.present?
end

#store_dirObject



174
175
176
177
178
179
180
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 174

def store_dir
  if @store_in_mod
    mod_dir
  else
    upload_dir
  end
end

create filesystem links to files from prior action



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 262

def symlink_to prior_action_id
  return unless prior_action_id != last_action_id
  save_action_id = selected_action_id
  links = {}

  self.selected_action_id = prior_action_id
  attachment.versions.each do |name, version|
    links[name] = version.store_path
  end
  original = attachment.store_path

  self.selected_action_id = last_action_id
  attachment.versions.each do |name, version|
    ::File.symlink links[name], version.store_path
  end
  ::File.symlink original, attachment.store_path

  self.selected_action_id = save_action_id
end

#tmp_upload_dir(_action_id = nil) ⇒ Object

place for files if card doesn’t have an id yet



200
201
202
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 200

def tmp_upload_dir _action_id=nil
  "#{Card.paths['files'].existent.first}/#{upload_cache_card.id}"
end

#unfilled?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 122

def unfilled?
  !attachment.present? && !save_preliminary_upload? && !subcards.present?
end

#upload_cache_cardObject

used for uploads for new cards until the new card is created



143
144
145
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 143

def upload_cache_card
  @upload_cache_card ||= Card["new_#{attachment_name}".to_sym]
end

#upload_dirObject

place for files of regular file cards



191
192
193
194
195
196
197
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 191

def upload_dir
  if id
    "#{Card.paths['files'].existent.first}/#{id}"
  else
    tmp_upload_dir
  end
end