Skip to content

Commit

Permalink
Merge pull request #594 from Floppy/batch-rename
Browse files Browse the repository at this point in the history
Organize (rename) models in bulk
  • Loading branch information
Floppy authored Jul 12, 2022
2 parents 4f57788 + e8b9b95 commit 12ffc6f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
5 changes: 3 additions & 2 deletions app/controllers/models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def bulk_update_params
:scale_factor,
:creator_id,
:add_tags,
:remove_tags
:remove_tags,
:organize
).compact_blank
end

Expand All @@ -89,7 +90,7 @@ def model_params
:name,
:scale_factor,
:tags,
:path,
:organize,
links_attributes: [:id, :url, :_destroy]
)
end
Expand Down
9 changes: 6 additions & 3 deletions app/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Model < ApplicationRecord
has_many :links, as: :linkable, dependent: :destroy
accepts_nested_attributes_for :links, reject_if: :all_blank, allow_destroy: true

attr_accessor :organize

before_update :move_files

default_scope { order(:name) }
Expand Down Expand Up @@ -74,11 +76,12 @@ def create_folder_if_necessary(folder)
end

def move_files
if path_changed?
old_path = File.join(library.path, path_was)
new_path = File.join(library.path, path)
if ActiveModel::Type::Boolean.new.cast(organize)
old_path = File.join(library.path, path)
new_path = File.join(library.path, formatted_path)
create_folder_if_necessary(File.dirname(new_path))
File.rename(old_path, new_path)
self.path = formatted_path
end
end
end
18 changes: 10 additions & 8 deletions app/views/models/_tags_edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<div class="row mb-3 input-group">
<div class="row mb-3">
<label for="tags" class="col-sm-2 col-form-label"><%= label %></label>
<div class="form-control col-auto tag-container">
<%= form.text_field :tags,
as: :string,
name: name,
value: value,
data: { tags_edit: "" }
%>
<div class="col-sm-10">
<div class="form-control col-auto tag-container">
<%= form.text_field :tags,
as: :string,
name: name,
value: value,
data: { tags_edit: "" }
%>
</div>
</div>
</div>
27 changes: 22 additions & 5 deletions app/views/models/bulk_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<th>Scale Factor</th>
<th>Tags</th>
<th>Creator</th>
<th>Organize files</th>
</tr>
<% @models.each do |model| %>
<tr>
Expand All @@ -22,26 +23,42 @@
<td><%= number_to_percentage model.scale_factor, strip_insignificant_zeros: true %></td>
<td><%= render partial: 'tag', collection: model.tags, locals: {selected: nil, model_id: model.id } %></td>
<td><%= link_to model.creator.name, model.creator if model.creator %></td>
<td><code><%= model.formatted_path if model.formatted_path != model.path %></code></td>
</tr>
<% end %>
</table>

<h3>Select changes to make:</h3>

<div class="row mb-3 input-group">
<div class="row mb-3">
<%= form.label :scale_factor, class: "col-sm-2 col-form-label" %>
<%= form.number_field :scale_factor, class: "form-control col-auto" %>
<div class="col-sm-10">
<%= form.number_field :scale_factor, class: "form-control col-auto" %>
</div>
</div>

<div class="row mb-3 input-group">
<div class="row mb-3">
<%= form.label :creator_id, class: "col-sm-2 col-form-label" %>
<%= form.collection_select :creator_id, @creators, :id, :name, {include_blank: true}, {class: "form-control col-auto form-select"} %>
<%= link_to "New Creator", new_creator_path, class: "btn btn-outline-secondary col-auto" %>
<div class="col-sm-10">
<div class="input-group">
<%= form.collection_select :creator_id, @creators, :id, :name, {include_blank: true}, {class: "form-control col-auto form-select"} %>
<%= link_to "New Creator", new_creator_path, class: "btn btn-outline-secondary col-auto" %>
</div>
</div>
</div>

<%= render 'tags_edit', :form => form, :name => :add_tags, :value => "", :label => "Add Tags" %>
<%= render 'tags_edit', :form => form, :name => :remove_tags, :value => "", :label => "Remove Tags" %>

<div class="row mb-3">
<%= form.label "Organize files", class: "col-sm-2 col-form-label" %>
<div class="col-sm-10">
<div class="form-switch">
<%= form.check_box :organize, class: "form-check-input form-check-inline" %>
</div>
</div>
</div>

<%= form.hidden_field :tag, value: @tag if @tag %>
<%= form.submit "Update Selected Models", class: "btn btn-primary" %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/models/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="confirmMoveLabel">Move model files</h5>
<h5 class="modal-title" id="confirmMoveLabel">Organize files</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Expand All @@ -53,12 +53,12 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">No, cancel</button>
<%= button_to "Yes, move the files", library_model_path(@library, @model, "model[path]": @model.formatted_path), method: :patch, class: "btn btn-warning" %>
<%= button_to "Yes, move the files", library_model_path(@library, @model, "model[organize]": true), method: :patch, class: "btn btn-warning" %>
</div>
</div>
</div>
</div>
<%= button_tag "Reformat Path", class: "btn btn-warning", "data-bs-toggle": "modal", "data-bs-target": "#confirm-move" %>
<%= button_tag "Organize Files", class: "btn btn-warning", "data-bs-toggle": "modal", "data-bs-target": "#confirm-move" %>
<% end %>
<% end %>
Expand Down

0 comments on commit 12ffc6f

Please sign in to comment.