Skip to content

Commit

Permalink
Integrate with Active Storage
Browse files Browse the repository at this point in the history
Closes [#2517][]

[#2517]: #2517
  • Loading branch information
seanpdoyle committed Nov 1, 2024
1 parent 4570b90 commit a129520
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 7 deletions.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions app/views/fields/attached/one/_attachment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="attachments-listing">
<%= image_tag field.data %>
</div>
24 changes: 24 additions & 0 deletions app/views/fields/attached/one/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<%#
# Attached One Form Partial
This partial renders an input element for a single attached attribute.
By default, the input is a file field for the attached file.
## Local variables:
- `f`:
A Rails form generator, used to help create the appropriate input fields.
- `field`:
An instance of [Administrate::Field::Attached::One][1].
A wrapper around the attachment pulled from the database.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Attached/One
%>


<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.file_field(field.attribute) %>
</div>
22 changes: 22 additions & 0 deletions app/views/fields/attached/one/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%#
# Attached One Show Partial
This partial renders an attachment attribute,
to be displayed on a resource's show page.
By default, the attribute is rendered as an attachment tag.
## Local variables:
- `field`:
An instance of [Administrate::Field::Attached::One][1].
A wrapper around the attachment pulled from the database.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Attached::One
%>

<% if field.attached? %>
<%= render partial: "fields/attached/one/attachment", locals: { field: field } %>
<% else %>
No attachment
<% end %>
2 changes: 2 additions & 0 deletions lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "administrate/field/attached/one"
require "administrate/field/attached/many"
require "administrate/field/belongs_to"
require "administrate/field/boolean"
require "administrate/field/date_time"
Expand Down
15 changes: 15 additions & 0 deletions lib/administrate/field/attached/many.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "administrate/field/base"

module Administrate
module Field
module Attached
class Many < Administrate::Field::Base
delegate :attached?, to: :data, allow_nil: true

def to_partial_path
"fields/attached/many/#{page}"
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/administrate/field/attached/one.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "administrate/field/base"

module Administrate
module Field
module Attached
class One < Administrate::Field::Base
delegate :attached?, to: :data, allow_nil: true

def to_partial_path
"fields/attached/one/#{page}"
end
end
end
end
end
24 changes: 24 additions & 0 deletions spec/administrate/views/fields/attached/one/_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "rails_helper"
require "administrate/field/attached/one"

describe "field/attached/one/_form", type: :view do
it "renders a singular file field" do
model = build(:product, hero_image: nil)
field = instance_double(
"Administrate::Field::Attached::One",
attribute: :hero_image,
data: nil
)

fields model: model do |f|
concat render(
partial: "fields/attached/one/form",
locals: {field: field, f: f}
)
end

expect(rendered).to have_field(
"product[hero_image]", type: "file"
)
end
end
29 changes: 29 additions & 0 deletions spec/administrate/views/fields/attached/one/_show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "rails_helper"
require "administrate/field/attached/one"

describe "fields/attached/one/_show", type: :view do
it "renders a 'No attachment' when missing a value" do
field = instance_double(
"Administrate::Field::Attached::One",
attached?: false
)

render(partial: "fields/attached/one/show", locals: {field: field})

expect(rendered).to have_text("No attachment")
end

it "renders a preview when attached" do
model = build(:product, hero_image: file_fixture("image.png"))
field = instance_double(
"Administrate::Field::Attached::One",
data: model.hero_image,
attached?: true
)

render(partial: "fields/attached/one/show", locals: {field: field})

expect(rendered).to have_element("img", src: rails_blob_url(model.hero_image))
.and(have_no_text("No attachment"))
end
end
8 changes: 6 additions & 2 deletions spec/example_app/app/dashboards/product_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class ProductDashboard < Administrate::BaseDashboard
:image_url,
:product_meta_tag,
:release_year,
:banner
:banner,
:hero_image,
:thumbnails
]

ATTRIBUTE_TYPES = {
Expand All @@ -24,7 +26,9 @@ class ProductDashboard < Administrate::BaseDashboard
product_meta_tag: Field::HasOne.with_options(order: "meta_title"),
release_year: Field::Select.with_options(
collection: -> { (Time.current.year - 10)..Time.current.year }
)
),
hero_image: Field::Attached::One,
thumbnails: Field::Attached::Many,
}

COLLECTION_ATTRIBUTES = ATTRIBUTES
Expand Down
2 changes: 2 additions & 0 deletions spec/example_app/app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Product < ApplicationRecord
has_rich_text :banner
has_one_attached :hero_image
has_many_attached :thumbnails

def self.policy_class=(policy)
@policy_class = policy
Expand Down
10 changes: 10 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
end
product_meta_tag
release_year { [2018, 2019, 2020].sample }

transient do
hero_image { nil }
end

after :build do |record, factory|
if (pathname = factory.hero_image)
record.hero_image.attach io: pathname.open, filename: pathname.to_s
end
end
end

factory :product_meta_tag do
Expand Down
12 changes: 7 additions & 5 deletions spec/features/products_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
describe "product form has_one relationship" do
include ActiveSupport::Testing::TimeHelpers

it "saves product and meta tag data correctly" do
it "saves product and meta tag data correctly", :js do
visit new_admin_product_path

fill_in "Name", with: "Example"
fill_in "Price", with: "0"
fill_in "Description", with: "Example"
fill_in "Image url", with: "http://imageurlthatdoesnotexist"
fill_in "Meta title", with: "Example meta title"
fill_in "Meta description", with: "Example meta description"

expect(page).to have_css("legend", text: "Product Meta Tag")
within_fieldset "Product Meta Tag" do
fill_in "Meta title", with: "Example meta title"
fill_in "Meta description", with: "Example meta description"
end
attach_file "Hero image", file_fixture("image.png")

click_on "Create Product"

expect(page).to have_link("Example meta title")
.and(have_element("img", src: /image.png/))
expect(page).to have_flash(
t("administrate.controller.create.success", resource: "Product")
)
Expand Down
Binary file added spec/fixtures/files/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.

0 comments on commit a129520

Please sign in to comment.