Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Appraisal #606

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -26,3 +26,5 @@ s3.yml
spec/refile/test_app/log
spec/refile/test_app/tmp
spec/refile/test_app/db/*.sqlite

vendor/bundle
15 changes: 10 additions & 5 deletions .rubocop.yml
@@ -1,10 +1,12 @@
require:
- rubocop-performance
AllCops:
Exclude:
- 'gemfiles/**/*'
- 'vendor/**/*'
DisplayCopNames: true

Metrics/LineLength:
Layout/LineLength:
Max: 150 # TODO: we should decrease this to 120

Metrics/ClassLength:
Expand All @@ -31,7 +33,7 @@ Metrics/CyclomaticComplexity:
Metrics/PerceivedComplexity:
Enabled: false

Layout/AlignParameters:
Layout/ParameterAlignment:
EnforcedStyle: with_fixed_indentation

Style/StringLiterals:
Expand Down Expand Up @@ -79,13 +81,13 @@ Layout/IndentationWidth:
Style/TrivialAccessors:
ExactNameMatch: true

Lint/EndAlignment:
Layout/EndAlignment:
EnforcedStyleAlignWith: variable

Lint/DefEndAlignment:
Layout/DefEndAlignment:
Enabled: false

Lint/HandleExceptions:
Lint/SuppressedException:
Enabled: false

Style/SpecialGlobalVars:
Expand Down Expand Up @@ -117,3 +119,6 @@ Style/GuardClause:

Layout/MultilineMethodCallBraceLayout:
Enabled: false

Naming/MethodParameterName:
MinNameLength: 2
22 changes: 17 additions & 5 deletions .travis.yml
Expand Up @@ -15,13 +15,25 @@ jobs:
# See https://github.com/rails/rails/commit/892eab777c418135ce0646e91bc9ebb08a29ab9b
#
# - rvm: 2.2
# gemfile: gemfiles/Gemfile_Rails_5_2
# gemfile:
# - gemfiles/rails_5_2.gemfile
#
- rvm: 2.3
gemfile: gemfiles/Gemfile_Rails_5_2
gemfile:
- gemfiles/rails_5_2.gemfile
- rvm: 2.4
gemfile: gemfiles/Gemfile_Rails_5_2
gemfile:
- gemfiles/rails_5_2.gemfile
- rvm: 2.5
gemfile: gemfiles/Gemfile_Rails_5_2
gemfile:
- gemfiles/rails_5_2.gemfile
#- gemfiles/rails_6_1.gemfile
- rvm: 2.6
gemfile: gemfiles/Gemfile_Rails_5_2
gemfile:
- gemfiles/rails_5_2.gemfile
#- gemfiles/rails_6_1.gemfile
- rvm: 2.7
gemfile:
- gemfiles/rails_5_2.gemfile
#- gemfiles/rails_6_1.gemfile

47 changes: 47 additions & 0 deletions Appraisals
@@ -0,0 +1,47 @@
# frozen_string_literal: true

appraise "rails_5_2" do
gem "webmock", "~> 3.5.1"
gem "bundler"
gem "rake"
gem "rspec", "~> 3.0"
gem "rspec-rails", "~> 3.3"
gem "jquery-rails"
gem "capybara"
gem "aws-sdk-s3"
gem "rack-test", "~> 0.6.2"
gem "rails", "~> 5.2"
gem "sqlite3", "~> 1.3.6", platforms: [:ruby]
gem "activerecord-jdbcsqlite3-adapter", "~> 52.1", platforms: [:jruby]
gem "poltergeist"
gem "yard"
gem "rubocop", "~> 0.81.0"
gem "rubocop-performance"
gem "puma"
gem "mini_magick"
gem "simple_form"
gem "i18n", "~> 1.2.0", "< 1.60"
end

appraise "rails_6_1" do
gem "webmock"
gem "bundler"
gem "rake"
gem "rspec"
gem "rspec-rails"
gem "jquery-rails"
gem "capybara"
gem "aws-sdk-s3"
gem "rack-test"
gem "rails", "~> 6.1.1"
gem "sqlite3", ">= 1.3.6", platforms: [:ruby]
gem "activerecord-jdbcsqlite3-adapter", ">= 52.1", platforms: [:jruby]
gem "poltergeist"
gem "yard"
gem "rubocop", "~> 0.81.0"
gem "rubocop-performance"
gem "puma"
gem "mini_magick"
gem "simple_form"
gem "i18n", ">= 1.2.0", "< 1.60"
end
10 changes: 10 additions & 0 deletions Gemfile
@@ -0,0 +1,10 @@
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gemspec

# This here to make sure appraisal works with Rails 3.0.0.
gem "thor", ">= 0.14.0"
2 changes: 2 additions & 0 deletions Rakefile
@@ -1,3 +1,5 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path("lib", File.dirname(__FILE__)))
$LOAD_PATH.unshift(File.expand_path("spec", File.dirname(__FILE__)))

Expand Down
2 changes: 2 additions & 0 deletions config.ru
@@ -1,3 +1,5 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path("spec", File.dirname(__FILE__)))

require "refile/test_app"
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

if Refile.automount
Rails.application.routes.draw do
mount Refile.app, at: Refile.mount_point, as: :refile_app
Expand Down
18 changes: 11 additions & 7 deletions gemfiles/Gemfile_Rails_5_2 → gemfiles/rails_5_2.gemfile
@@ -1,23 +1,27 @@
source "https://rubygems.org"
# This file was generated by Appraisal

gemspec path: '../'
source "https://rubygems.org"

gem "thor", ">= 0.14.0"
gem "webmock", "~> 3.5.1"
gem "bundler", ">= 1.7", "<= 2.0.1"
gem "rake", "< 11.0"
gem "bundler"
gem "rake"
gem "rspec", "~> 3.0"
gem "rspec-rails", "~> 3.3"
gem "jquery-rails"
gem "capybara", "< 3.16"
gem "capybara"
gem "aws-sdk-s3"
gem "rack-test", "~> 0.6.2"
gem "rails", "~> 5.2"
gem "sqlite3", "~> 1.3.6", platforms: [:ruby]
gem "sqlite3", "~> 1.3.6", platforms: [:ruby]
gem "activerecord-jdbcsqlite3-adapter", "~> 52.1", platforms: [:jruby]
gem "poltergeist"
gem "yard"
gem "rubocop", "~> 0.49.0"
gem "rubocop", "~> 0.81.0"
gem "rubocop-performance"
gem "puma"
gem "mini_magick"
gem "simple_form"
gem "i18n", "~> 1.2.0", "< 1.60"

gemspec path: "../"
27 changes: 27 additions & 0 deletions gemfiles/rails_6_1.gemfile
@@ -0,0 +1,27 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "thor", ">= 0.14.0"
gem "webmock"
gem "bundler"
gem "rake"
gem "rspec"
gem "rspec-rails"
gem "jquery-rails"
gem "capybara"
gem "aws-sdk-s3"
gem "rack-test"
gem "rails", "~> 6.1.1"
gem "sqlite3", ">= 1.3.6", platforms: [:ruby]
gem "activerecord-jdbcsqlite3-adapter", ">= 52.1", platforms: [:jruby]
gem "poltergeist"
gem "yard"
gem "rubocop", "~> 0.81.0"
gem "rubocop-performance"
gem "puma"
gem "mini_magick"
gem "simple_form"
gem "i18n", ">= 1.2.0", "< 1.60"

gemspec path: "../"
8 changes: 5 additions & 3 deletions lib/refile.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: false

require "uri"
require "fileutils"
require "tempfile"
Expand Down Expand Up @@ -259,7 +261,7 @@ def extract_content_type(uploadable)
filename = extract_filename(uploadable)
if filename
content_type = MIME::Types.of(filename).first
content_type.to_s if content_type
content_type&.to_s
end
end
end
Expand Down Expand Up @@ -316,7 +318,7 @@ def app_url(host: nil, prefix: nil)
# @param [String, nil] expires_at Adds a sulfix to the URL that sets the expiration time of the URL
# @param [String, nil] force_download Adds a sulfix to the URL to force the download of the file when URL is accessed
# @return [String, nil] The generated URL
def file_url(file, *args, expires_at: nil, host: nil, prefix: nil, filename:, format: nil, force_download: nil)
def file_url(file, *args, filename:, expires_at: nil, host: nil, prefix: nil, format: nil, force_download: nil)
return unless file

host ||= Refile.cdn_host
Expand Down Expand Up @@ -512,7 +514,7 @@ def parse_json(data, *args)
config.allow_uploads_to = ["cache"]
config.allow_downloads_from = :all
config.allow_origin = "*"
config.logger = Logger.new(STDOUT) unless ENV["RACK_ENV"] == "test"
config.logger = Logger.new($stdout) unless ENV["RACK_ENV"] == "test"
config.mount_point = "/attachments"
config.automount = true
config.content_max_age = 60 * 60 * 24 * 365
Expand Down
2 changes: 2 additions & 0 deletions lib/refile/app.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "json"
require "sinatra/base"
require "tempfile"
Expand Down
8 changes: 6 additions & 2 deletions lib/refile/attacher.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: false

module Refile
# @api private
class Attacher
Expand Down Expand Up @@ -82,9 +84,10 @@ def set(value)
end

def retrieve!(value)
if value.is_a?(String)
case value
when String
@metadata = Refile.parse_json(value, symbolize_names: true) || {}
elsif value.is_a?(Hash)
when Hash
@metadata = value
end
write_metadata if cache_id
Expand Down Expand Up @@ -171,6 +174,7 @@ def read(column, strict = false)

def write(column, value, strict = false)
return if record.frozen?

m = "#{name}_#{column}="
record.send(m, value) if strict or record.respond_to?(m)
end
Expand Down
14 changes: 8 additions & 6 deletions lib/refile/attachment.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Refile
module Attachment
# Macro which generates accessors for the given column which make it
Expand Down Expand Up @@ -38,12 +40,12 @@ module Attachment
# @return [void]
def attachment(name, cache: :cache, store: :store, raise_errors: true, type: nil, extension: nil, content_type: nil)
definition = AttachmentDefinition.new(name,
cache: cache,
store: store,
raise_errors: raise_errors,
type: type,
extension: extension,
content_type: content_type
cache: cache,
store: store,
raise_errors: raise_errors,
type: type,
extension: extension,
content_type: content_type
)

define_singleton_method :"#{name}_attachment_definition" do
Expand Down
4 changes: 3 additions & 1 deletion lib/refile/attachment/active_record.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: false

module Refile
module ActiveRecord
module Attachment
Expand Down Expand Up @@ -80,7 +82,7 @@ def accepts_attachments_for(association_name, attachment: :file, append: false)
association = reflect_on_association(association_name)
attachment_pluralized = attachment.to_s.pluralize
name = "#{association_name}_#{attachment_pluralized}"
collection_class = association && association.klass
collection_class = association&.klass

options = {
collection_class: collection_class,
Expand Down
6 changes: 3 additions & 3 deletions lib/refile/attachment/multiple_attachments.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: false

module Refile
module Attachment
# Builds a module to be used by "accepts_attachments_for"
Expand Down Expand Up @@ -30,9 +32,7 @@ def self.new(collection_name, collection_class:, name:, attachment:, append:, &b
cache = cache.reject(&:empty?)
files = files.compact

if not append and (!files.empty? or !cache.empty?)
send("#{collection_name}=", [])
end
send("#{collection_name}=", []) if not append and (!files.empty? or !cache.empty?)

collection = send(collection_name)

Expand Down
11 changes: 7 additions & 4 deletions lib/refile/attachment_definition.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module Refile
# @api private
class AttachmentDefinition
attr_reader :record, :name, :cache, :store, :options, :type, :valid_content_types
attr_reader :record, :name, :options, :type, :valid_content_types
attr_accessor :remove

def initialize(name, cache:, store:, raise_errors: true, type: nil, extension: nil, content_type: nil)
Expand Down Expand Up @@ -37,6 +39,7 @@ def raise_errors?

def valid_extensions
return unless @extension

if @extension.is_a?(Proc)
Array(@extension.call)
else
Expand All @@ -60,15 +63,15 @@ def validate(attacher)
private

def extension_error_params(extension)
[:invalid_extension, extension: format_param(extension), permitted: valid_extensions.to_sentence]
[:invalid_extension, { extension: format_param(extension), permitted: valid_extensions.to_sentence }]
end

def content_type_error_params(content_type)
[:invalid_content_type, content: format_param(content_type), permitted: valid_content_types.to_sentence]
[:invalid_content_type, { content: format_param(content_type), permitted: valid_content_types.to_sentence }]
end

def invalid_extension?(extension)
extension_included = valid_extensions && valid_extensions.map(&:downcase).include?(extension)
extension_included = valid_extensions&.map(&:downcase)&.include?(extension)
valid_extensions and not extension_included
end

Expand Down