inline image upload
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
ActiveAdmin.register Software do
|
||||
permit_params :name, :title, :author, :desc, :story,
|
||||
:license, :platform, :status, :highlighted,
|
||||
software_images_attributes: [ :id, :image_id, :is_default, :position, :_destroy ],
|
||||
software_images_attributes: [ :id, :image_id, :is_default, :position, :file_upload, :_destroy ],
|
||||
external_links_attributes: [ :id, :label, :url, :_destroy ],
|
||||
releases_attributes: [ :id, :version, :html_folder_path, :cartridge_path, :source_path, :docs_folder_path, :web_playable, :_destroy ]
|
||||
|
||||
@@ -52,8 +52,10 @@ ActiveAdmin.register Software do
|
||||
|
||||
f.inputs "Images" do
|
||||
f.has_many :software_images, allow_destroy: true, new_record: true do |si|
|
||||
si.input :image_id, as: :select, label: "Image",
|
||||
collection: Image.order(:original_filename).map { |img| [img.original_filename, img.id] }
|
||||
si.input :file_upload, as: :file, label: "Upload new image", hint: "Or select existing below"
|
||||
si.input :image_id, as: :select, label: "Existing image",
|
||||
collection: Image.order(:original_filename).map { |img| [img.original_filename, img.id] },
|
||||
include_blank: "— select —"
|
||||
si.input :is_default, as: :boolean
|
||||
si.input :position, as: :number, input_html: { min: 0 }
|
||||
end
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
class SoftwareImage < ApplicationRecord
|
||||
belongs_to :software
|
||||
belongs_to :image
|
||||
belongs_to :image, optional: true
|
||||
|
||||
attr_accessor :file_upload
|
||||
|
||||
before_validation :create_image_from_upload, if: -> { file_upload.present? }
|
||||
|
||||
validates :image_id, presence: true
|
||||
validates :image_id, uniqueness: { scope: :software_id }
|
||||
validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
|
||||
@@ -19,6 +24,15 @@ class SoftwareImage < ApplicationRecord
|
||||
|
||||
private
|
||||
|
||||
def create_image_from_upload
|
||||
img = Image.new(file_upload: file_upload)
|
||||
if img.save
|
||||
self.image = img
|
||||
else
|
||||
errors.add(:file_upload, img.errors.full_messages.join(", "))
|
||||
end
|
||||
end
|
||||
|
||||
def unset_other_defaults
|
||||
SoftwareImage.where(software_id: software_id, is_default: true)
|
||||
.where.not(id: id)
|
||||
|
||||
Reference in New Issue
Block a user