Provides a simple WebDAV storage for Shrine (http://shrinerb.com)
Ruby
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/shrine/storage
spec
.gitignore
.rspec
.travis.yml
CODE_OF_CONDUCT.md
Gemfile
LICENSE.txt
README.md
Rakefile
shrine-webdav.gemspec

README.md

Shrine::Storage::WebDAV

Provides a simple WebDAV storage for Shrine.

Build Status

Installation

Add this line to your application's Gemfile:

gem 'shrine-webdav'

Usage

Suppose you have Report model which should be able to store data in a remote WebDAV storage.

class Report < ApplicationRecord
end
# == Schema Information
#
# Table name: reports
#
#  id                :uuid             not null, primary key
#  name              :string           not null
#  created_at        :datetime         not null
#  updated_at        :datetime         not null

Before you can add attributes pointing to remote files to your model you should describe Uploader class:

# app/models/reports/report_uploader.rb
class ReportUploader < Shrine
  plugin :activerecord
  plugin :logging, logger: Rails.logger

  def generate_location(_io, context)
    uuid = context[:record].id
    "#{uuid}.#{context[:name]}"
  end
end

You don't have to override method generate_location, but if you didn't you would have random file names.

Now you can add the attributes:

class Report < ApplicationRecord
  include ReportUploader::Attachment.new(:pdf)
  include ReportUploader::Attachment.new(:xls)
end

Note corresponding migrations:

class AddFileAttributes < ActiveRecord::Migration[5.1]
  def change
    add_column :reports, :xls_data, :text
    add_column :reports, :pdf_data, :text
  end
end

Create file shrine.rb in config/initializers/ to configure WebDAV storage:

# config/initializers/shrine.rb
require 'shrine'
require "shrine/storage/webdav"

Shrine.storages = {
  cache: Shrine::Storage::WebDAV.new(host: 'http://webdav-server.com', prefix: 'your_project/cache'),
  store: Shrine::Storage::WebDAV.new(host: 'http://webdav-server.com', prefix: 'your_project/store')
}

Now you can use your virtual attributes pdf and xls like this:

report = Report.new(name: 'Senseless report')

# here you are going to have file sample.pdf uploaded
# to "http://webdav-server.com/your_project/cache/#{report.id}.pdf"
report.pdf = File.open('sample.pdf')

# file sample.xls is being uploading
# to "http://webdav-server.com/your_project/cache/#{report.id}.xls"
report.xls = File.open('sample.xls')

# after committing in database both files sample.pdf and sample.xls have
# been uploaded to "http://webdav-server.com/your_project/store/..."
report.save

Development

After checking out the repo, run bundle install to install dependencies. Then, run rake spec to run the tests.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in shrine-webdav.gemspec, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/funbox/shrine-webdav. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.