mirror of
https://github.com/manyfold3d/manyfold.git
synced 2026-10-22 11:42:19 -05:00
139 lines
3.2 KiB
Ruby
139 lines
3.2 KiB
Ruby
require "fileutils"
|
|
|
|
class ModelsController < ApplicationController
|
|
include ModelFilters
|
|
before_action :get_library, except: [:index, :bulk_edit, :bulk_update]
|
|
before_action :get_model, except: [:bulk_edit, :bulk_update, :index]
|
|
|
|
def index
|
|
process_filters_init
|
|
|
|
# Ordering
|
|
@models = case session["order"]
|
|
when "recent"
|
|
@models.order(created_at: :desc)
|
|
else
|
|
@models.order(name: :asc)
|
|
end
|
|
|
|
if current_user.pagination_settings["models"]
|
|
page = params[:page] || 1
|
|
@models = @models.page(page).per(current_user.pagination_settings["per_page"])
|
|
end
|
|
|
|
process_filters_tags_fetchall
|
|
process_filters
|
|
process_filters_tags_highlight
|
|
end
|
|
|
|
def show
|
|
@groups = helpers.group(@model.model_files)
|
|
end
|
|
|
|
def edit
|
|
@creators = Creator.all
|
|
@collections = Collection.all
|
|
@model.links.build if @model.links.empty? # populate empty link
|
|
end
|
|
|
|
def update
|
|
@model.update(model_params)
|
|
redirect_to [@model.library, @model]
|
|
end
|
|
|
|
def merge
|
|
if params[:target] && (target = (@model.parents.find { |x| x.id == params[:target].to_i }))
|
|
@model.merge_into! target
|
|
Scan::CheckModelIntegrityJob.perform_later(target)
|
|
redirect_to [@library, target]
|
|
elsif params[:all] && @model.contains_other_models?
|
|
@model.contained_models.each do |child|
|
|
child.merge_into! @model
|
|
end
|
|
Scan::CheckModelIntegrityJob.perform_later(@model)
|
|
redirect_to [@library, @model]
|
|
else
|
|
render status: :bad_request
|
|
end
|
|
end
|
|
|
|
def bulk_edit
|
|
@creators = Creator.all
|
|
@collections = Collection.all
|
|
@models = Model.all
|
|
process_filters
|
|
end
|
|
|
|
def bulk_update
|
|
hash = bulk_update_params
|
|
hash[:library_id] = hash.delete(:new_library_id) if hash[:new_library_id]
|
|
|
|
add_tags = Set.new(hash.delete(:add_tags))
|
|
remove_tags = Set.new(hash.delete(:remove_tags))
|
|
|
|
params[:models].each_pair do |id, selected|
|
|
if selected == "1"
|
|
model = Model.find(id)
|
|
if model.update(hash)
|
|
existing_tags = Set.new(model.tag_list)
|
|
model.tag_list = existing_tags + add_tags - remove_tags
|
|
model.save
|
|
end
|
|
end
|
|
end
|
|
redirect_to edit_models_path(@filters)
|
|
end
|
|
|
|
def destroy
|
|
@model.destroy
|
|
|
|
# Delete directory corresponding to model
|
|
pathname = File.join(@library.path, @model.path)
|
|
FileUtils.remove_dir(pathname) if File.exist?(pathname)
|
|
|
|
redirect_to library_path(@library)
|
|
end
|
|
|
|
private
|
|
|
|
def bulk_update_params
|
|
params.permit(
|
|
:creator_id,
|
|
:collection_id,
|
|
:new_library_id,
|
|
:organize,
|
|
add_tags: [],
|
|
remove_tags: []
|
|
).compact_blank
|
|
end
|
|
|
|
def model_params
|
|
params.require(:model).permit(
|
|
:preview_file_id,
|
|
:creator_id,
|
|
:library_id,
|
|
:name,
|
|
:caption,
|
|
:notes,
|
|
:collection_id,
|
|
:q,
|
|
:library,
|
|
:creator,
|
|
:tag,
|
|
:organize,
|
|
:missingtag,
|
|
tag_list: [],
|
|
links_attributes: [:id, :url, :_destroy]
|
|
)
|
|
end
|
|
|
|
def get_library
|
|
@library = Model.find(params[:id]).library
|
|
end
|
|
|
|
def get_model
|
|
@model = Model.includes(:model_files, :creator).find(params[:id])
|
|
@title = @model.name
|
|
end
|
|
end
|