Class: FileAsset

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/file_asset.rb

Direct Known Subclasses

Image, TextFile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, options) ⇒ FileAsset

Returns a new instance of FileAsset.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/file_asset.rb', line 109

def initialize(attributes = {}, options)
  attributes ||= {}

  base_path = attributes.delete(:base_path)
  @type, directory, name, data = attributes.values_at(:type, :directory, :name, :data)
  base_path ||= data.original_filename if data.respond_to?(:original_filename)

  directory, name = FileAsset.split_path(base_path) if base_path and name.blank?
  directory.gsub!(Rails.root.to_s,'')

  @type ||= FileAsset.type_for(name) if name
  @type = "TextFile" if @type.nil?
  @name = name

  data = StringIO.new(data) if data.is_a?(String)

  super attributes.merge(:directory => directory, :name => name, :data => data)
end

Class Method Details

.acceptable?(name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/file_asset.rb', line 72

def acceptable?(name)
  valid_extensions.include?(File.extname(name))
end

.all_valid_extensionsObject



98
99
100
# File 'app/models/file_asset.rb', line 98

def all_valid_extensions
  all_subclasses.map { |k| k.valid_extensions }.flatten.uniq
end

.split_path(path) ⇒ Object



102
103
104
105
106
# File 'app/models/file_asset.rb', line 102

def split_path(path)
  directory, name = ::File.split(path)
  directory = nil if directory == '.'
  [directory, name]
end

.type_by_extension(extension) ⇒ Object



81
82
83
84
85
# File 'app/models/file_asset.rb', line 81

def type_by_extension(extension)
  klass = all_subclasses.detect{ |k| k.valid_extensions.include?(extension) }
  klass = TextFile if klass.nil?
  klass
end

.type_for(name) ⇒ Object



76
77
78
79
# File 'app/models/file_asset.rb', line 76

def type_for(name)
  classes = all_subclasses.uniq
  classes.detect{ |k| k.acceptable?(name) }.try(:name)
end

.valid_extensionsObject



94
95
96
# File 'app/models/file_asset.rb', line 94

def valid_extensions
  read_inheritable_attribute(:valid_extensions) || []
end

.validate_extension(data, file) ⇒ Object



87
88
89
90
91
92
# File 'app/models/file_asset.rb', line 87

def validate_extension(data, file)
  if file.name && !file.class.valid_extensions.include?(File.extname(file.name))
    types = all_valid_extensions.map{ |type| type.gsub(/^\./, '') }.join(', ')
    "#{file.name} is not a valid file type. Valid file types are #{types}."
  end
end

Instance Method Details

#basenameObject



128
129
130
# File 'app/models/file_asset.rb', line 128

def basename
  data_file_name.gsub(/\.#{extname}$/, "")
end

#extnameObject



132
133
134
# File 'app/models/file_asset.rb', line 132

def extname
  File.extname(self.data_file_name).gsub(/^\.+/, '')
end

#get_contentsObject



152
153
154
155
# File 'app/models/file_asset.rb', line 152

def get_contents
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)
  file_support.get_contents(File.join(self.directory,self.data_file_name))
end

#move(new_parent_path) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/file_asset.rb', line 157

def move(new_parent_path)
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => ErpTechSvcs::Config.file_storage)

  if ErpTechSvcs::Config.file_storage == :filesystem and !self.directory.include?(Rails.root.to_s)
    old_path = File.join(Rails.root, self.directory, self.name)
  else
    old_path = File.join(self.directory, self.name)
  end

  result, message = file_support.save_move(old_path, new_parent_path)
  if result
    self.directory = new_parent_path.gsub(Regexp.new(Rails.root.to_s), '') # strip rails root from new_parent_path, we want relative path
    self.save
  end

  return result, message
end

#set_content_typeObject



140
141
142
143
144
145
146
# File 'app/models/file_asset.rb', line 140

def set_content_type
  unless @type.nil?
    klass = @type.constantize
    content_type = klass == Image ? "image/#{File.extname(@name).gsub(/^\.+/, '')}" : klass.content_type
    self.data.instance_write(:content_type, content_type)
  end
end

#set_data_file_nameObject



148
149
150
# File 'app/models/file_asset.rb', line 148

def set_data_file_name
  update_attribute :data_file_name, name if data_file_name != name
end

#set_stiObject



136
137
138
# File 'app/models/file_asset.rb', line 136

def set_sti
  update_attribute :type, @type
end