Skip to content

Commit

Permalink
Add a has_many_attached type to the test app
Browse files Browse the repository at this point in the history
  • Loading branch information
pedantic-git authored and Dreamersoul committed May 17, 2024
1 parent 5e906c4 commit 4d42fdc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 45 deletions.
80 changes: 36 additions & 44 deletions test_app/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,16 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
administrate (0.16.0)
actionpack (>= 5.0)
actionview (>= 5.0)
activerecord (>= 5.0)
datetime_picker_rails (~> 0.0.7)
jquery-rails (>= 4.0)
kaminari (>= 1.0)
momentjs-rails (~> 2.8)
administrate (0.20.1)
actionpack (>= 6.0, < 8.0)
actionview (>= 6.0, < 8.0)
activerecord (>= 6.0, < 8.0)
jquery-rails (~> 4.6.0)
kaminari (~> 1.2.2)
sassc-rails (~> 2.1)
selectize-rails (~> 0.6)
base64 (0.2.0)
bigdecimal (3.1.4)
bigdecimal (3.1.8)
bindex (0.8.1)
builder (3.2.4)
byebug (11.1.3)
Expand All @@ -109,28 +107,25 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
childprocess (4.1.0)
concurrent-ruby (1.2.2)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.4)
datetime_picker_rails (0.0.7)
momentjs-rails (>= 2.8.1)
drb (2.2.0)
ruby2_keywords
drb (2.2.1)
erubi (1.12.0)
ffi (1.15.4)
ffi (1.16.3)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
io-console (0.6.0)
irb (1.9.0)
rdoc
reline (>= 0.3.8)
jquery-rails (4.4.0)
io-console (0.7.2)
irb (1.13.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
jquery-rails (4.6.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
Expand Down Expand Up @@ -158,9 +153,7 @@ GEM
matrix (0.4.2)
mini_magick (4.12.0)
mini_mime (1.1.5)
minitest (5.20.0)
momentjs-rails (2.29.1)
railties (>= 3.1)
minitest (5.23.0)
mutex_m (0.2.0)
net-imap (0.4.5)
date
Expand All @@ -172,28 +165,28 @@ GEM
net-smtp (0.4.0)
net-protocol
nio4r (2.6.0)
nokogiri (1.15.5-arm64-darwin)
nokogiri (1.16.5-arm64-darwin)
racc (~> 1.4)
nokogiri (1.15.5-x86_64-darwin)
nokogiri (1.16.5-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.15.5-x86_64-linux)
nokogiri (1.16.5-x86_64-linux)
racc (~> 1.4)
psych (5.1.1.1)
psych (5.1.2)
stringio
public_suffix (4.0.6)
puma (5.6.4)
nio4r (~> 2.0)
racc (1.7.3)
rack (2.2.8)
rack (3.0.11)
rack-mini-profiler (2.3.3)
rack (>= 1.2.0)
rack-session (1.0.1)
rack (< 3)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rackup (1.0.0)
rack (< 3)
webrick
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rails (7.1.2)
actioncable (= 7.1.2)
actionmailbox (= 7.1.2)
Expand Down Expand Up @@ -223,16 +216,15 @@ GEM
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rake (13.1.0)
rdoc (6.6.0)
rake (13.2.1)
rdoc (6.6.3.1)
psych (>= 4.0.0)
regexp_parser (2.2.0)
reline (0.4.0)
reline (0.5.7)
io-console (~> 0.5)
rexml (3.2.5)
ruby-vips (2.2.0)
ffi (~> 1.12)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
Expand All @@ -249,19 +241,19 @@ GEM
childprocess (>= 0.5, < 5.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2)
sprockets (4.0.2)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
rack (>= 2.2.4, < 4)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.7.3-arm64-darwin)
sqlite3 (1.7.3-x86_64-darwin)
sqlite3 (1.7.3-x86_64-linux)
stringio (3.0.9)
thor (1.3.0)
tilt (2.0.10)
stringio (3.1.0)
thor (1.3.1)
tilt (2.3.0)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand All @@ -276,7 +268,7 @@ GEM
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.12)
zeitwerk (2.6.14)

PLATFORMS
arm64-darwin-23
Expand Down
6 changes: 6 additions & 0 deletions test_app/app/controllers/admin/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@ class PostsController < Admin::ApplicationController

# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information

def destroy_other_image
other_image = requested_resource.other_images.find(params[:attachment_id])
other_image.purge
redirect_back(fallback_location: requested_resource)
end
end
end
12 changes: 12 additions & 0 deletions test_app/app/dashboards/post_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class PostDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
id: Field::Number,
cover_image: Field::ActiveStorage,
other_images: Field::ActiveStorage.with_options(
destroy_url: proc do |namespace, resource, attachment|
[:other_image_admin_post, { id: resource.id, attachment_id: attachment.id }]
end
),
title: Field::String,
created_at: Field::DateTime,
updated_at: Field::DateTime,
Expand All @@ -24,6 +29,7 @@ class PostDashboard < Administrate::BaseDashboard
id
title
cover_image
other_images
created_at
].freeze

Expand All @@ -33,6 +39,7 @@ class PostDashboard < Administrate::BaseDashboard
id
title
cover_image
other_images
created_at
updated_at
].freeze
Expand All @@ -43,6 +50,7 @@ class PostDashboard < Administrate::BaseDashboard
FORM_ATTRIBUTES = %i[
title
cover_image
other_images
].freeze

# COLLECTION_FILTERS
Expand All @@ -63,4 +71,8 @@ class PostDashboard < Administrate::BaseDashboard
# def display_resource(post)
# "Post ##{post.id}"
# end

def permitted_attributes(action = nil)
super + [:other_images => []]
end
end
1 change: 1 addition & 0 deletions test_app/app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Post < ApplicationRecord
has_one_attached :cover_image
has_many_attached :other_images

validates :title, presence: true
end
4 changes: 3 additions & 1 deletion test_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

namespace :admin do
resources :posts
resources :posts do
delete :other_image, on: :member, action: :destroy_other_image
end

root to: "posts#index"
end
Expand Down

0 comments on commit 4d42fdc

Please sign in to comment.