Module: Refile::AttachmentHelper
- Defined in:
- lib/refile/rails/attachment_helper.rb
Instance Method Summary collapse
- #attachment_field(object_name, method, options = {}) ⇒ Object
- #attachment_image_tag(record, name, *args, fallback: nil, format: nil, host: nil, **options) ⇒ Object
- #attachment_url(record, name, *args, filename: nil, format: nil, host: nil) ⇒ Object
Instance Method Details
#attachment_field(object_name, method, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/refile/rails/attachment_helper.rb', line 32 def (object_name, method, = {}) [:data] ||= {} if [:object] attacher = [:object].send(:"#{method}_attacher") [:accept] = attacher.accept if [:direct] host = [:host] || Refile.host || request.base_url backend_name = Refile.backends.key(attacher.cache) url = ::File.join(host, main_app.refile_app_path, backend_name) [:data].merge!(direct: true, as: "file", url: url) end if [:presigned] and attacher.cache.respond_to?(:presign) [:data].merge!(direct: true).merge!(attacher.cache.presign.as_json) end end hidden_field(object_name, :"#{method}_cache_id", .slice(:object)) + file_field(object_name, method, ) end |
#attachment_image_tag(record, name, *args, fallback: nil, format: nil, host: nil, **options) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/refile/rails/attachment_helper.rb', line 18 def (record, name, *args, fallback: nil, format: nil, host: nil, **) file = record.send(name) classes = ["attachment", record.class.model_name.singular, name, *[:class]] if file image_tag((record, name, *args, format: format, host: host), .merge(class: classes)) elsif fallback classes << "fallback" image_tag(fallback, .merge(class: classes)) end end |
#attachment_url(record, name, *args, filename: nil, format: nil, host: nil) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/refile/rails/attachment_helper.rb', line 3 def (record, name, *args, filename: nil, format: nil, host: nil) file = record.send(name) return unless file filename ||= name.to_s backend_name = Refile.backends.key(file.backend) host = host || Refile.host || request.base_url filename = filename.parameterize("_") filename << "." << format.to_s if format ::File.join(host, main_app.refile_app_path, backend_name, *args.map(&:to_s), file.id.to_s, filename) end |