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_active_job, define_event_method, define_event_perform_later_method, define_on_format, ensure_set, event, extended, format, process_base_module_list, process_base_modules, register_set, register_set_format, shortname, view, write_tmp_file

Class Method Details

.included(host_class) ⇒ Object



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

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

Instance Method Details

#assign_attachment(file, original_filename) ⇒ Object



53
54
55
56
57
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 53

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

#assign_set_specific_attributesObject



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

def assign_set_specific_attributes
  if @set_specific && @set_specific.present?
    self.content = nil
  end
  super
end

#attachment_changed?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 101

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

#attachment_format(ext) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 188

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_tmp_dirObject



155
156
157
158
159
160
161
162
163
164
165
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 155

def clear_upload_tmp_dir
  Dir.entries(tmp_store_dir).each do |filename|
    if filename =~/^\d+/
      path = File.join(tmp_store_dir, filename )
      older_than_five_days = ( DateTime.now - File.ctime(path) > 432000)
      if older_than_five_days
        FileUtils.rm path
      end
    end
  end
end

#create_versions?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 105

def create_versions?
  true
end

#item_names(args = {}) ⇒ Object

needed for flexmail attachments. hacky.



85
86
87
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 85

def item_names(args={})  # needed for flexmail attachments.  hacky.
  [self.cardname]
end

#load_from_modObject



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

def load_from_mod
  @mod
end

#load_from_mod=(value) ⇒ Object



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

def load_from_mod= value
  @mod = value
end

#mod_file?Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 133

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

#original_filenameObject



89
90
91
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 89

def original_filename
  attachment.original_filename
end

#preliminary_upload?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 93

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

#save_preliminary_upload?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 97

def save_preliminary_upload?
  Card::Env.params[:cached_upload].present?
end

#store_dirObject



121
122
123
124
125
126
127
128
129
130
131
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 121

def store_dir
  if (store_place == :deck)
    if id
      "#{ Card.paths['files'].existent.first }/#{id}"
    else
      tmp_store_dir
    end
  else
    "#{ Cardio.gem_root}/mod/#{store_place}/file/#{codename}"
  end
end

#store_placeObject



109
110
111
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 109

def store_place
  @store_place ||= mod_file? || :deck
end

create filesystem links to files from prior action



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 167

def symlink_to(prior_action_id) # create filesystem links to files from prior action
  if 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
end

#tmp_store_dir(action_id = nil) ⇒ Object



81
82
83
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 81

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

#upload_cache_cardObject



77
78
79
# File 'tmpsets/set/mod007-05_standard/abstract/attachment.rb', line 77

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