Skip to content

Commit 3d32518

Browse files
committed
Address review findings for uploader and mapping helpers
1 parent b7282e9 commit 3d32518

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

lib/active_storage/service/uploadcare_service/uuid_mapping.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,22 @@ def persist_uuid_to_blob(key, uuid)
4141
end
4242

4343
def keys_for_prefix(prefix)
44-
return ActiveStorage::Blob.where('key LIKE ?', "#{prefix}%").pluck(:key) if defined?(ActiveStorage::Blob)
44+
if defined?(ActiveStorage::Blob)
45+
sanitized_prefix = sanitize_sql_like_prefix(prefix)
46+
return ActiveStorage::Blob.where('key LIKE ?', "#{sanitized_prefix}%").pluck(:key)
47+
end
4548

4649
@key_uuid_map.keys.select { |key| key.start_with?(prefix) }
4750
end
4851

52+
def sanitize_sql_like_prefix(prefix)
53+
if defined?(ActiveRecord::Base) && ActiveRecord::Base.respond_to?(:sanitize_sql_like)
54+
ActiveRecord::Base.sanitize_sql_like(prefix.to_s)
55+
else
56+
prefix.to_s.gsub(/[\\%_]/) { |char| "\\#{char}" }
57+
end
58+
end
59+
4960
def key_if_uuid(key)
5061
key if key.to_s.match?(/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i)
5162
end

lib/uploadcare/rails/action_view/uploadcare_uploader_tags.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def uploadcare_uploader_field(object_name, method_name, options = {})
4343
# @param options [Hash]
4444
# @return [String]
4545
def uploadcare_uploader_field_tag(object_name, options = {})
46-
hidden_field_tag(object_name, options[:value], uploadcare_uploader_options(options))
46+
options = options.dup
47+
value = options.delete(:value)
48+
hidden_field_tag(object_name, value, uploadcare_uploader_options(options))
4749
end
4850

4951
# Converts uploader options into HTML data attributes.

lib/uploadcare/rails/objects/concerns/loadable.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def uploadcare_configuration
2727
# @param new_attrs [Hash]
2828
# @return [Object]
2929
def update_attrs(new_attrs)
30+
return self if new_attrs.nil?
31+
raise ArgumentError, 'new_attrs must be a Hash' unless new_attrs.is_a?(Hash)
32+
3033
new_attrs.each do |key, value|
3134
setter = "#{key}="
3235
public_send(setter, value) if respond_to?(setter)

spec/uploadcare/rails/action_view/uploadcare_uploader_tags_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949
expect(tag).to include(fragment)
5050
end
5151
end
52+
53+
it 'does not duplicate value as a data attribute' do
54+
tag = uploadcare_uploader_field_tag(:title, value: 'https://ucarecdn.com/file/', multiple: true)
55+
56+
expect(tag).to include('value="https://ucarecdn.com/file/"')
57+
expect(tag).to include('data-multiple="true"')
58+
expect(tag).not_to include('data-value=')
59+
end
5260
end
5361

5462
RSpec.configure do |c|

spec/uploadcare/rails/active_storage/uploadcare_service_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@
141141
expect(service).to have_received(:delete).with('prefix-2')
142142
end
143143

144+
it 'escapes SQL wildcard characters in prefix when deleting prefixed keys' do
145+
relation = double(pluck: [])
146+
allow(ActiveStorage::Blob).to receive(:where).with('key LIKE ?',
147+
'pre\%fix\_%').and_return(relation)
148+
149+
service.delete_prefixed('pre%fix_')
150+
end
151+
144152
it 'supports existence check using mapped uuid' do
145153
blob = double(metadata: { 'uploadcare_uuid' => uuid }, update!: true)
146154
allow(ActiveStorage::Blob).to receive(:find_by).with(key: 'blob-key').and_return(blob)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'uploadcare/rails/objects/concerns/loadable'
5+
6+
RSpec.describe Uploadcare::Rails::Objects::Loadable do
7+
let(:klass) do
8+
Class.new do
9+
include Uploadcare::Rails::Objects::Loadable
10+
11+
attr_accessor :name
12+
13+
def self.uploadcare_configuration
14+
Struct.new(:cache_namespace, :cache_expires_in, :cache_files).new(nil, 1.minute, false)
15+
end
16+
end
17+
end
18+
let(:instance) { klass.new }
19+
20+
describe '#update_attrs' do
21+
it 'returns self when attrs are nil' do
22+
expect(instance.update_attrs(nil)).to eq(instance)
23+
end
24+
25+
it 'raises for non-hash attrs' do
26+
expect { instance.update_attrs('name') }.to raise_error(ArgumentError, 'new_attrs must be a Hash')
27+
end
28+
29+
it 'assigns known attributes from hash' do
30+
instance.update_attrs('name' => 'Uploadcare')
31+
32+
expect(instance.name).to eq('Uploadcare')
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)