diff --git a/.env.travis b/.env.travis new file mode 100644 index 0000000..0ec3999 --- /dev/null +++ b/.env.travis @@ -0,0 +1,3 @@ +POSTGRES_URL="postgres://localhost/test" +MYSQL_URL="mysql2://travis@localhost/test" +SQLITE_PATH="sqlite://spec/tmp/hanami_shrine_test" diff --git a/.gitignore b/.gitignore index f35b16e..c650400 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /spec/reports/ /spec/tmp/ /tmp/ +.env \ No newline at end of file diff --git a/.rspec b/.rspec index 8c18f1a..4e1e0d2 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1 @@ ---format documentation --color diff --git a/.travis.yml b/.travis.yml index 050c5c7..1631816 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,19 @@ language: ruby rvm: - 2.3.3 -before_install: gem install bundler -v 1.10.6 +before_install: gem install bundler -v 2.0.1 + +services: + - postgresql + - mysql + +env: + - DB=sqlite + - DB=mysql + - DB=postgres + +script: + - cp .env.travis .env + - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test;'; fi + - if [[ "$DB" == "postgres" ]]; then psql -c 'create database test;' -U postgres; fi + - bundle exec rake diff --git a/CHANGELOG.md b/CHANGELOG.md index 51fb7a9..32f6d4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.4.1 + +* Make sure it's using Shrine 2.x, as this does not work with 3.0 (yet) + +## 0.4.0 + +* Added support for Shrine versions 2.7.0 and up, while dropping support for lower versions (@famano, see #6) + ## 0.3.0 * Fixed incompatibility with Hanami 1.0.1+ leading to `RuntimeError: can't modify frozen Hash` error upon deletion diff --git a/Gemfile b/Gemfile index 507a281..fce203f 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,6 @@ source 'https://rubygems.org' # Specify your gem's dependencies in hanami-shrine.gemspec gemspec -gem 'shrine', '>= 2.0.0' gem 'sqlite3' gem 'pry' gem 'image_processing' diff --git a/README.md b/README.md index 3f6d625..8d33ccc 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,13 @@ # Hanami::Shrine -This gem aims at providing support for [Shrine](https://github.com/janko-m/shrine) uploader in Hanami applications. It also tries to be as simple as possible, without polluting the world around. +**Current status**: this gem's development is currently on hold, waiting for Hanami 2 to be released (which will probably make it obsolete anyway). + +This gem aims at providing support for [Shrine](https://github.com/shrinerb/shrine) uploader in Hanami applications. It also tries to be as simple as possible, without polluting the world around. [![Build Status](https://travis-ci.org/katafrakt/hanami-shrine.svg)](https://travis-ci.org/katafrakt/hanami-shrine) [![Gem Version](https://badge.fury.io/rb/hanami-shrine.svg)](https://badge.fury.io/rb/hanami-shrine) [![Code Climate](https://codeclimate.com/github/katafrakt/hanami-shrine/badges/gpa.svg)](https://codeclimate.com/github/katafrakt/hanami-shrine) -### Current compatibility status - -#### **0.1.x** -* works with Hanami 0.7.x -* works with Hanami 0.8.x without validations (which have been extracted to separate gem) - -#### **0.2.x** -* works with Hanami 0.9.x and 1.0. - -Future compatibility will only be provided with Hanami 1.0 and later (which will propably be compatible with 0.9 too). - ## Installation Add this line to your application's Gemfile: @@ -31,7 +22,7 @@ And then execute: ## Usage -Setup Shrine with `hanami` plugin enabled. Check [Shrine's repository](https://github.com/janko-m/shrine) for more detailed description of the process. +Setup Shrine with `hanami` plugin enabled. Check [Shrine's repository](https://github.com/shrinerb/shrine) for more detailed description of the process. ```ruby class ImageAttachment < Shrine @@ -66,6 +57,16 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, 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](https://rubygems.org). +### Testing + +Testing is done against 3 major database engines: PostgreSQL, MySQL and sqlite3. It is steered by environment variable `DB`. To run locally, first `cp .env.travis .env`, then put values matching your local configuration there. After that, you can test all three versions: + +``` +DB=sqlite bundle exec rake +DB=postgres bundle exec rake +DB=mysql bundle exec rake +``` + ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/katafrakt/hanami-shrine. diff --git a/Rakefile b/Rakefile index b7e9ed5..0cc599b 100644 --- a/Rakefile +++ b/Rakefile @@ -4,3 +4,22 @@ require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) task :default => :spec + +namespace :spec do + task :all do + require 'dotenv' + Dotenv.load! + require_relative 'spec/database_helper' + + %w(sqlite postgres mysql).each do |adapter| + ENV['DB_ADAPTER'] = adapter + ENV['DB_URL'] = DatabaseHelper.db_url + p ENV.keys + puts '########################' + puts "# Running for: #{adapter}" + puts "########################\n\n" + Rake::Task['spec'].invoke + Rake::Task['spec'].reenable + end + end +end diff --git a/hanami-shrine.gemspec b/hanami-shrine.gemspec index b37a799..6a4019a 100644 --- a/hanami-shrine.gemspec +++ b/hanami-shrine.gemspec @@ -19,12 +19,15 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - spec.add_dependency 'shrine' + spec.add_dependency 'shrine', '>= 2.16.0', '< 3.0' spec.add_dependency 'hanami-model', '>= 1.0' spec.add_dependency "hanami-utils", '>= 1.0' - spec.add_development_dependency "bundler", "~> 1.10" + spec.add_development_dependency "bundler", "> 1.0" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec" spec.add_development_dependency "json" + spec.add_development_dependency "dotenv" + spec.add_development_dependency "pg" + spec.add_development_dependency "mysql2" end diff --git a/lib/hanami/shrine/version.rb b/lib/hanami/shrine/version.rb index 6002350..8b52e6d 100644 --- a/lib/hanami/shrine/version.rb +++ b/lib/hanami/shrine/version.rb @@ -1,5 +1,5 @@ module Hanami module Shrine - VERSION = "0.3.0" + VERSION = '0.4.1' end end diff --git a/lib/shrine/plugins/hanami.rb b/lib/shrine/plugins/hanami.rb index 99d2248..b18fb40 100644 --- a/lib/shrine/plugins/hanami.rb +++ b/lib/shrine/plugins/hanami.rb @@ -6,7 +6,7 @@ class Shrine module Plugins module Hanami module AttachmentMethods - def initialize(name) + def initialize(name, **options) super module_eval <<-RUBY, __FILE__, __LINE__ + 1 @@ -78,14 +78,20 @@ def delete(id) end private + def _attachments self.class._attachments end def save_attachments(entity) + if !entity.is_a?(::Hanami::Entity) + entity = self.class.entity.new(entity) + end + _attachments.each do |a| entity = save_attachment(entity, a[:name], a[:class]) end + yield(entity) end diff --git a/spec/database_helper.rb b/spec/database_helper.rb new file mode 100644 index 0000000..3eb1dae --- /dev/null +++ b/spec/database_helper.rb @@ -0,0 +1,22 @@ +module DatabaseHelper + MAPPING = { + 'postgres' => 'POSTGRES_URL', + 'mysql' => 'MYSQL_URL', + 'sqlite' => 'SQLITE_PATH', + } + + module_function + + def adapter + ENV['DB'] || 'sqlite' + end + + def postgres? + adapter == 'postgres' + end + + def db_url(desired_adapter = nil) + env_name = MAPPING.fetch(desired_adapter || adapter) + ENV[env_name] + end +end diff --git a/spec/fixtures/migrations/20161120200000_create_plugins_model.rb b/spec/fixtures/migrations/20161120200000_create_plugins_model.rb index c09c832..8108de9 100644 --- a/spec/fixtures/migrations/20161120200000_create_plugins_model.rb +++ b/spec/fixtures/migrations/20161120200000_create_plugins_model.rb @@ -2,7 +2,7 @@ change do create_table :plugins_models do primary_key :id - column :image_data, String + column :image_data, String, size: 1000 end end end diff --git a/spec/fixtures/models.rb b/spec/fixtures/models.rb index 2005fc0..ac022c7 100644 --- a/spec/fixtures/models.rb +++ b/spec/fixtures/models.rb @@ -14,12 +14,12 @@ class ComplexAttachment < Shrine plugin :store_dimensions process(:store) do |io, context| - original = io.download + io.download do |original| + size_100 = ImageProcessing::MiniMagick.source(original).resize_to_limit!(100, 100) + size_30 = ImageProcessing::MiniMagick.source(original).resize_to_limit!(30, 30) - size_100 = resize_to_limit(original, 100, 100) - size_30 = resize_to_limit(size_100, 30, 30) - - {original: io, small: size_100, tiny: size_30} + {original: io, small: size_100, tiny: size_30} + end end end diff --git a/spec/hanami/plugins_spec.rb b/spec/hanami/plugins_spec.rb index 995aaad..a8e9ffb 100644 --- a/spec/hanami/plugins_spec.rb +++ b/spec/hanami/plugins_spec.rb @@ -43,6 +43,20 @@ expect(File.exist?(file_path)).to eq(true) end end + + it 'keeps original size' do + version = model.image[:original] + file_path = File.join('spec/tmp/uploads', version.id) + dimensions = MiniMagick::Image.open(file_path).dimensions + expect(dimensions).to eq([470, 459]) + end + + it 'resizes correctly' do + version = model.image[:small] + file_path = File.join('spec/tmp/uploads', version.id) + dimensions = MiniMagick::Image.open(file_path).dimensions + expect(dimensions).to eq([100, 98]) + end end context 'determine_mime_type' do diff --git a/spec/hanami/repository_spec.rb b/spec/hanami/repository_spec.rb index 26c4aca..d395ad4 100644 --- a/spec/hanami/repository_spec.rb +++ b/spec/hanami/repository_spec.rb @@ -79,6 +79,14 @@ context '#create' do include_context 'creation' + + context 'with hash params' do + let(:cat) do + KittenRepository.new.create(image: image) + end + + include_context 'creation' + end end context '#delete' do @@ -100,6 +108,14 @@ context '#update' do include_context 'update' + + context 'with hash params' do + let(:cat) do + KittenRepository.new.create(image: image) + end + + include_context 'update' + end end context '#persist' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4fe5ea4..44a0db6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,6 @@ +require 'dotenv' +Dotenv.load! + $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'hanami/shrine' require 'shrine' @@ -5,14 +8,15 @@ require 'hanami/model' require 'hanami/model/sql' require 'hanami/model/migrator' +require_relative 'database_helper' Shrine.storages = { cache: Shrine::Storage::FileSystem.new(Dir.tmpdir), - store: Shrine::Storage::FileSystem.new("spec/tmp", prefix: "uploads") + store: Shrine::Storage::FileSystem.new("spec/tmp", prefix: 'uploads') } Hanami::Model.configure do - adapter :sql, 'sqlite://spec/tmp/hanami-shrine_test' + adapter :sql, DatabaseHelper.db_url migrations Pathname.new(__dir__ + '/fixtures/migrations').to_s end -Hanami::Model::Migrator.prepare +Hanami::Model::Migrator.migrate