Skip to content

Commit

Permalink
Allow overriding passed options in default_url_options plugin
Browse files Browse the repository at this point in the history
Now people can override passed options by deleting them, e.g:

  plugin :default_url_options, store: -> (io, options) do
    disposition = options.delete(:response_content_disposition, "inline")

    {
      response_content_disposition: ContentDisposition.format(
        disposition: disposition,
        filename:    io.original_filename,
      )
    }
  end
  • Loading branch information
janko committed Jul 25, 2019
1 parent b1868a3 commit b1dd6e9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD

* `default_url_options` – Allow deleting passed options when using a block (@janko)

* `activerecord` – Make it work with ActiveRecord 3 (@texpert)

* `infer_extension` – Fix compatibility with `pretty_location` plugin (@janko)
Expand Down
2 changes: 1 addition & 1 deletion doc/plugins/default_url_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ which will receive the UploadedFile object along with any options that were
passed to `UploadedFile#url`.

```rb
plugin :default_url_options, store: -> (io, **options) do
plugin :default_url_options, store: -> (io, options) do
{ response_content_disposition: ContentDisposition.attachment(io.original_filename) }
end
```
Expand Down
2 changes: 1 addition & 1 deletion lib/shrine/plugins/default_url_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.configure(uploader, options = {})
module FileMethods
def url(**options)
default_options = default_url_options
default_options = default_options.call(self, **options) if default_options.respond_to?(:call)
default_options = default_options.call(self, options) if default_options.respond_to?(:call)
default_options ||= {}

super(default_options.merge(options))
Expand Down
10 changes: 10 additions & 0 deletions test/plugin/default_url_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@
uploaded_file.storage.expects(:url).with(uploaded_file.id, {foo: "foo"})
uploaded_file.url(foo: "foo")
end

it "allows user to override passed options" do
@uploader.class.plugin :default_url_options, store: ->(io, options) do
{ foo: "#{options.delete(:foo)} bar" }
end

uploaded_file = @uploader.upload(fakeio)
uploaded_file.storage.expects(:url).with(uploaded_file.id, {foo: "foo bar"})
uploaded_file.url(foo: "foo")
end
end

0 comments on commit b1dd6e9

Please sign in to comment.