Class: ModelsController

Inherits:
ApplicationController show all
Includes:
LinkableController, ModelListable, Merge, Permittable
Defined in:
app/controllers/models_controller.rb

Defined Under Namespace

Modules: Merge

Instance Method Summary collapse

Methods included from LinkableController

#sync

Methods included from Permittable

#find_caber_subjects

Instance Method Details

#bulk_edit ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/models_controller.rb', line 157

def bulk_edit
  authorize Model
  @models = @filter.models(policy_scope(Model, policy_scope_class: ApplicationPolicy::UpdateScope)).includes(:collections, :creator)
  generate_available_tag_list
  page = params[:page] || 1
  # Double the normal page size for bulk editing
  @models = @models.page(page).per(helpers.pagination_settings["per_page"] * 2)
  set_indexable @models
  # Apply tag filters in-place
  @filter_in_place = true
end

#bulk_update ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/controllers/models_controller.rb', line 169

def bulk_update
  authorize Model
  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))

  models_to_update = if params.key?(:update_all)
    # If "Update All Models" was clicked, update all models in the filtered set
    @filter.models(policy_scope(Model, policy_scope_class: ApplicationPolicy::UpdateScope))
  else
    # If "Update Selected Models" was clicked, only update checked models
    ids = params[:models].select { |k, v| v == "1" }.keys
    policy_scope(Model, policy_scope_class: ApplicationPolicy::UpdateScope).where(public_id: ids)
  end

  models_to_update.find_each do |model|
    if model&.update(hash)
      existing_tags = Set.new(model.tag_list)
      model.tag_list = existing_tags + add_tags - remove_tags
      model.save
    end
  end
  redirect_back_or_to edit_models_path(@filter.to_params), notice: t(".success")
end

#create ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/models_controller.rb', line 81

def create
  authorize :model
  p = upload_params
  # First, is this a single or multi-model event?
  multiple = p[:file]&.values&.all? { MediaType.archive_extensions.include? File.extname(it[:name]).delete(".").downcase }
  # Then run validations on a dummy object
  common_args = {
    name: multiple ? nil : p[:name],
    owner: current_user,
    creator_id: p[:creator_id],
    collection_ids: p[:collections]&.map(&:id),
    license: p[:license],
    sensitive: (p[:sensitive] == "1"),
    tag_list: p[:tag_list],
    permission_preset: p[:permission_preset]
  }
  library = SiteSettings.show_libraries ? Library.find_param(p[:library]) : Library.default
  @model = Model.new(common_args.merge(library: library)) # dummy model object
  if @model.valid?(multiple ? :multi_upload : :single_upload)
    # Handle actual files
    jobs = if multiple
      # If this is all separate archives, enqueue separate jobs for each
      p[:file]&.values&.map { cached_file_data(it) }
    else
      # Otherwise, enqueue one job for all files and add name to args
      [p[:file]&.values&.map { cached_file_data(it) }]
    end
    jobs&.each do
      ProcessUploadedFileJob.perform_later(library.id, it, **common_args)
    end
    respond_to do |format|
      format.html { redirect_to models_path, notice: t(".success") }
      format.manyfold_api_v0 { head :accepted }
    end
  else
    get_creators_and_collections
    generate_available_tag_list
    respond_to do |format|
      format.html { render :new, status: :unprocessable_content }
      format.manyfold_api_v0 { render json: @model.errors.to_json, status: :unprocessable_content }
    end
  end
end

#destroy ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/controllers/models_controller.rb', line 195

def destroy
  @model.delete_from_disk_and_destroy
  respond_to do |format|
    format.html do
      if request.referer && (URI.parse(request.referer).path == model_path(@model))
        # If we're coming from the model page itself, we can't go back there
        redirect_to helpers.landing_page_path, notice: t(".success")
      else
        redirect_back_or_to helpers.landing_page_path, notice: t(".success")
      end
    end
    format.manyfold_api_v0 { head :no_content }
  end
end

#edit ⇒ Object



75
76
77
78
79
# File 'app/controllers/models_controller.rb', line 75

def edit
  @model.links.build if @model.links.empty? # populate empty link
  @model.caber_relations.build if @model.caber_relations.empty?
  generate_available_tag_list
end

#index ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/models_controller.rb', line 21

def index
  @models = @filter.models(policy_scope(Model))
  @search = params[:q].presence
  prepare_model_list
  set_indexable @models
  respond_to do |format|
    format.html { render layout: "card_list_page" }
    format.manyfold_api_v0 { render json: ManyfoldApi::V0::ModelListSerializer.new(@models).serialize }
  end
end

#new ⇒ Object



69
70
71
72
73
# File 'app/controllers/models_controller.rb', line 69

def new
  @model = Model.new # dummy model object
  authorize :model
  generate_available_tag_list
end

#scan ⇒ Object



150
151
152
153
154
155
# File 'app/controllers/models_controller.rb', line 150

def scan
  # Start the scans
  @model.check_later
  # Back to the model page
  redirect_to @model, notice: t(".success")
end

#show ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/models_controller.rb', line 32

def show
  respond_to do |format|
    format.html do
      files = policy_scope(@model.model_files).without_special
      @locked_files = @model.model_files.without_special.count - files.count
      @images = files.select(&:is_image?)
      @images.unshift(@model.preview_file) if @images.delete(@model.preview_file)
      if helpers.file_list_settings["hide_presupported_versions"]
        hidden_ids = files.includes(:relationships).where("relationships.predicate": ["supported_version_of", "alternative_format_of"])
        files = files.where.not(id: hidden_ids)
      end
      files = files.includes(:problems)
      files = files.reject(&:is_image?)
      @groups = helpers.group(files)
      @num_files = files.count
    end
    format.zip do
      if policy(@model).download?
        download = ArchiveDownloadService.new(model: @model, selection: params[:selection])
        if download.ready?
          send_file(download.output_file, filename: download.filename, type: :zip, disposition: :attachment)
        elsif download.preparing?
          redirect_to model_path(@model, format: :html), notice: t(".download_preparing")
        else
          download.prepare
          redirect_to model_path(@model, format: :html), notice: t(".download_requested")
        end
      else
        head :forbidden
      end
    end
    format.oembed { render json: OEmbed::ModelSerializer.new(@model, helpers.oembed_params).serialize }
    format.manyfold_api_v0 { render json: ManyfoldApi::V0::ModelSerializer.new(@model).serialize }
  end
  # i18n-tasks-use t("activerecord.attributes.model.path")
end

#update ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/models_controller.rb', line 125

def update
  hash = model_params
  organize = hash.delete(:organize) == "true"
  result = @model.update(hash)
  respond_to do |format|
    format.html do
      if result
        @model.organize_later if organize
        redirect_to @model, notice: t(".success")
      else
        get_creators_and_collections
        edit
        render :edit, status: :unprocessable_content
      end
    end
    format.manyfold_api_v0 do
      if result
        render json: ManyfoldApi::V0::ModelSerializer.new(@model).serialize
      else
        render json: @model.errors.to_json, status: :unprocessable_content
      end
    end
  end
end