Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
Make Attacher#read method public
Browse files Browse the repository at this point in the history
We changed Attacher#reload to reset dirty tracking in addition to
loading attachment data. However, it's nice to also allow just loading
attachment data and keeping dirty tracking as is. That's what
Attacher#read method does.

This is consistent with Attacher#write which is public as well.
  • Loading branch information
janko committed Nov 1, 2020
1 parent a792dce commit f498052
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD

* `entity` – Make `Attacher#read` method public (@janko)

* `entity` – Reset attachment dirty tracking in `Attacher#reload` (@janko)

* `activerecord` – Don't load the attacher on `ActiveRecord::Base#reload` if it hasn't yet been initialized (@janko)
Expand Down
3 changes: 3 additions & 0 deletions doc/plugins/entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ attacher.reload
attacher.file #=> #<ImageUploader::UploadedFile>
```

If you want to reload attachment data while retaining dirty tracking state, use
`Attacher#read` instead.

### Column values

The `Attacher#column_values` method returns a hash with the entity attribute as
Expand Down
10 changes: 5 additions & 5 deletions lib/shrine/plugins/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def reload
self
end

# Loads attachment from the entity attribute.
def read
load_column(read_attribute)
end

# Returns a hash with entity attribute name and column data.
#
# attacher.column_values
Expand All @@ -135,11 +140,6 @@ def attribute

private

# Loads attachment from the entity attribute.
def read
load_column(read_attribute)
end

# Reads value from the entity attribute.
def read_attribute
record.public_send(attribute)
Expand Down
10 changes: 10 additions & 0 deletions test/plugin/entity_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@
end
end

describe "#read" do
it "loads entity file data" do
@attacher.load_entity(@entity_class.new, :file)
@attacher.attach(fakeio)
@attacher.read

assert_nil @attacher.file
end
end

describe "#column_values" do
it "returns column values with an attached file" do
@attacher.load_entity(@entity_class.new, :file)
Expand Down

0 comments on commit f498052

Please sign in to comment.