Skip to content

Latest commit

 

History

History
160 lines (110 loc) · 4.31 KB

SELF_CONTAINED_EXAMPLE.md

File metadata and controls

160 lines (110 loc) · 4.31 KB

Simple, Self-contained Example

Option 1: Clone the repository below

It is very difficult to debug a problem without a simple example.

You can clone this repository here with everything set up, to make this as easy as possible to debug.

But if you wish to set things up manually, please follow the guide lines below.

Option 2: Manually set up the self-contained Example:

Appendix A: Sequel template

require "sequel"
require "shrine"
require "shrine/storage/memory"
require "down"

Shrine.storages = {
  cache: Shrine::Storage::Memory.new,
  store: Shrine::Storage::Memory.new,
}

Shrine.plugin :sequel

class MyUploader < Shrine
  # plugins and uploading logic
end

DB = Sequel.sqlite # SQLite memory database
DB.create_table :posts do
  primary_key :id
  String :image_data
end

class Post < Sequel::Model
  include MyUploader::Attachment(:image)
end

post = Post.create(image: Down.download("https://example.com/image-from-internet.jpg"))

# Your code for reproducing

Appendix B: ActiveRecord template

require "active_record"
require "shrine"
require "shrine/storage/memory"
require "down"

Shrine.storages = {
  cache: Shrine::Storage::Memory.new,
  store: Shrine::Storage::Memory.new,
}

Shrine.plugin :activerecord

class MyUploader < Shrine
  # plugins and uploading logic
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.connection.create_table(:posts) { |t| t.text :image_data }

class Post < ActiveRecord::Base
  include MyUploader::Attachment(:image)
end

post = Post.create(image: Down.download("https://example.com/image-from-internet.jpg"))

# Your code for reproducing

Appendix C: Running the Templates

You can either:

  • Create a Gemfile and add the necessary gems.

  • Inline the required gems in the script - if you don't want to create a Gemfile.

Create a Gemfile and Add Gems (Option 1)

# Gemfile
source 'https://rubygems.org' do
  gem "activerecord" # replace with gem "sequel" if you're using it
  gem 'shrine'
  gem "down"
end
  1. Run bundle install.

  2. Run the template with: ruby template_name.rb.

In-lining them Gems in your script (Option 2)

  • Add the following to the above template script (not a Gemfile)
## template_name.rb
## Append the following to the top of the ruby template script
require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem "activerecord" # replace with gem "sequel" if you're using it
  gem 'shrine'
end

# include the rest of the template script below:
  • Run the template with ruby template_name.rb.

Appendix D: Debugging Shrine code

If you would like to debug or step through Shrine's code using the above template scripts, follow these steps:

  1. Download (or clone) Shrine's repository to your local machine git clone https://github.com/shrinerb/shrine.git (or you could create a fork of the above repository and clone your fork - this will make it easier for you if you want to make a pull request).

  2. Checkout the version of shrine you are using by running git checkout <version_tag>.

  3. Modify the Shrine gem line in your Gemfile to point to the local copy of Shrine code:

  gem "shrine", path: "/path/to/your/local/shrine/gem/code" 
  1. If needed, add a debugger like byebug or pry to the Gemfile.

  2. Run bundle install.

  3. Add require "bundler/setup" to the top of the template script.

  4. You can add debugger statement anywhere in your template file or Shrine code.

  5. Run ruby template_name.rb.