-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split part of attacher off into AttachmentDefinition
- Loading branch information
Showing
7 changed files
with
104 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Refile | ||
# @api private | ||
class AttachmentDefinition | ||
attr_reader :record, :name, :cache, :store, :options, :type, :valid_extensions, :valid_content_types | ||
attr_accessor :remove | ||
|
||
def initialize(name, cache:, store:, raise_errors: true, type: nil, extension: nil, content_type: nil) | ||
@name = name | ||
@raise_errors = raise_errors | ||
@cache_name = cache | ||
@store_name = store | ||
@type = type | ||
@valid_extensions = [extension].flatten if extension | ||
@valid_content_types = [content_type].flatten if content_type | ||
@valid_content_types ||= Refile.types.fetch(type).content_type if type | ||
end | ||
|
||
def cache | ||
Refile.backends.fetch(@cache_name.to_s) | ||
end | ||
|
||
def store | ||
Refile.backends.fetch(@store_name.to_s) | ||
end | ||
|
||
def accept | ||
if valid_content_types | ||
valid_content_types.join(",") | ||
elsif valid_extensions | ||
valid_extensions.map { |e| ".#{e}" }.join(",") | ||
end | ||
end | ||
|
||
def raise_errors? | ||
@raise_errors | ||
end | ||
|
||
def validate(attacher) | ||
errors = [] | ||
errors << :invalid_extension if valid_extensions and not valid_extensions.include?(attacher.extension) | ||
errors << :invalid_content_type if valid_content_types and not valid_content_types.include?(attacher.content_type) | ||
errors << :too_large if cache.max_size and attacher.size and attacher.size >= cache.max_size | ||
errors | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters