Class: Indexmap::Storage::ActiveStorage
- Inherits:
-
Object
- Object
- Indexmap::Storage::ActiveStorage
- Defined in:
- lib/indexmap/storage/active_storage.rb
Constant Summary collapse
- DEFAULT_CONTENT_TYPE =
"application/xml"
Instance Method Summary collapse
- #delete(filename) ⇒ Object
- #exist?(filename) ⇒ Boolean
-
#initialize(model:, public_url:, filename_column: :filename, attachment: :file, content_type: DEFAULT_CONTENT_TYPE) ⇒ ActiveStorage
constructor
A new instance of ActiveStorage.
- #inspect ⇒ Object
- #list(prefix: nil, suffix: nil) ⇒ Object
- #public_url(filename) ⇒ Object
- #read(filename) ⇒ Object
- #write(filename, body, content_type: nil) ⇒ Object
Constructor Details
#initialize(model:, public_url:, filename_column: :filename, attachment: :file, content_type: DEFAULT_CONTENT_TYPE) ⇒ ActiveStorage
Returns a new instance of ActiveStorage.
11 12 13 14 15 16 17 |
# File 'lib/indexmap/storage/active_storage.rb', line 11 def initialize(model:, public_url:, filename_column: :filename, attachment: :file, content_type: DEFAULT_CONTENT_TYPE) @model = model @public_url_base = public_url @filename_column = filename_column @attachment = @default_content_type = content_type end |
Instance Method Details
#delete(filename) ⇒ Object
56 57 58 59 |
# File 'lib/indexmap/storage/active_storage.rb', line 56 def delete(filename) record = find_record(filename) record&.public_send()&.purge end |
#exist?(filename) ⇒ Boolean
39 40 41 |
# File 'lib/indexmap/storage/active_storage.rb', line 39 def exist?(filename) attached?(find_record(filename)) end |
#inspect ⇒ Object
65 66 67 |
# File 'lib/indexmap/storage/active_storage.rb', line 65 def inspect "#<#{self.class.name} model=#{model}>" end |
#list(prefix: nil, suffix: nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/indexmap/storage/active_storage.rb', line 43 def list(prefix: nil, suffix: nil) relation = relation_for(prefix: prefix, suffix: suffix) relation.filter_map do |record| filename = record.public_send(filename_column).to_s next if prefix && !filename.start_with?(prefix) next if suffix && !filename.end_with?(suffix) next unless attached?(record) filename end.sort end |
#public_url(filename) ⇒ Object
61 62 63 |
# File 'lib/indexmap/storage/active_storage.rb', line 61 def public_url(filename) URI.join("#{public_url_base}/", filename).to_s end |
#read(filename) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/indexmap/storage/active_storage.rb', line 32 def read(filename) record = find_record(filename) return unless attached?(record) record.public_send().download end |
#write(filename, body, content_type: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/indexmap/storage/active_storage.rb', line 19 def write(filename, body, content_type: nil) = content_type || default_content_type record = find_or_initialize(filename) record.save! unless record.persisted? record.public_send().attach( io: StringIO.new(body.to_s), filename: filename, content_type: ) File.new(filename: filename, body: body.to_s, content_type: ) end |