Module: Shrine::Plugins::Versions::AttacherMethods

Defined in:
lib/shrine/plugins/versions.rb

Instance Method Summary collapse

Instance Method Details

#dataObject

Converts the Hash/Array of UploadedFile objects into a Hash/Array of data.



90
91
92
93
94
# File 'lib/shrine/plugins/versions.rb', line 90

def data
  Utils.map_file(file, transform_keys: :to_s) do |_, version|
    version.data
  end
end

#destroyObject



56
57
58
# File 'lib/shrine/plugins/versions.rb', line 56

def destroy(*)
  Utils.each_file(self.file) { |_, file| file.delete }
end

#file=(file) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/shrine/plugins/versions.rb', line 96

def file=(file)
  if file.is_a?(Hash) || file.is_a?(Array)
    @file = file
  else
    super
  end
end

#uploaded_file(value, &block) ⇒ Object



104
105
106
# File 'lib/shrine/plugins/versions.rb', line 104

def uploaded_file(value, &block)
  shrine_class.uploaded_file(value, &block)
end

#url(version = nil, **options) ⇒ Object

Smart versioned URLs, which include the version name in the default URL, and properly forwards any options to the underlying storage.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/shrine/plugins/versions.rb', line 62

def url(version = nil, **options)
  if file.is_a?(Hash)
    if version
      version = version.to_sym
      if file.key?(version)
        file[version].url(**options)
      elsif fallback = shrine_class.version_fallbacks[version]
        url(fallback, **options)
      else
        default_url(**options, version: version)
      end
    else
      raise Error, "must call Shrine::Attacher#url with the name of the version"
    end
  else
    if version
      if file && shrine_class.opts[:versions][:fallback_to_original]
        file.url(**options)
      else
        default_url(**options, version: version)
      end
    else
      super(**options)
    end
  end
end