Add buttons to move columns left and right
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
class Columns::LeftPositionsController < ApplicationController
|
||||
include ActionView::RecordIdentifier, ColumnScoped
|
||||
|
||||
def create
|
||||
@left_column = @column.left_column
|
||||
@column.move_left
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
class Columns::RightPositionsController < ApplicationController
|
||||
include ActionView::RecordIdentifier, ColumnScoped
|
||||
|
||||
def create
|
||||
@right_column = @column.right_column
|
||||
@column.move_right
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,12 @@
|
||||
module ColumnScoped
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
before_action :set_column
|
||||
end
|
||||
|
||||
private
|
||||
def set_column
|
||||
@column = Column.find(params[:column_id])
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,49 @@
|
||||
module Column::Positioned
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
scope :sorted, -> { order(position: :asc) }
|
||||
|
||||
before_create :set_position
|
||||
end
|
||||
|
||||
def move_left
|
||||
swap_position_with left_column
|
||||
end
|
||||
|
||||
def move_right
|
||||
swap_position_with right_column
|
||||
end
|
||||
|
||||
def left_column
|
||||
collection.columns.where("position < ?", position).sorted.last
|
||||
end
|
||||
|
||||
def right_column
|
||||
collection.columns.where("position > ?", position).sorted.first
|
||||
end
|
||||
|
||||
def leftmost?
|
||||
left_column.nil?
|
||||
end
|
||||
|
||||
def rightmost?
|
||||
right_column.nil?
|
||||
end
|
||||
|
||||
private
|
||||
def set_position
|
||||
max_position = collection.columns.maximum(:position)
|
||||
self.position = max_position + 1
|
||||
end
|
||||
|
||||
def swap_position_with(other_column)
|
||||
return if other_column.nil?
|
||||
|
||||
transaction do
|
||||
old_position = self.position
|
||||
self.update_column(:position, other_column.position)
|
||||
other_column.update_column(:position, old_position)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,35 +0,0 @@
|
||||
module Column::Positioned
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
scope :sorted, -> { order(position: :asc) }
|
||||
|
||||
before_create :set_position
|
||||
end
|
||||
|
||||
def move_left
|
||||
if column = collection.columns.where("position < ?", position).sorted.last
|
||||
swap_position_with(column)
|
||||
end
|
||||
end
|
||||
|
||||
def move_right
|
||||
if column = collection.columns.where("position > ?", position).sorted.first
|
||||
swap_position_with(column)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def set_position
|
||||
max_position = collection.columns.maximum(:position)
|
||||
self.position = max_position + 1
|
||||
end
|
||||
|
||||
def swap_position_with(other_column)
|
||||
transaction do
|
||||
other_column.position, self.position = self.position, other_column.position
|
||||
other_column.save!
|
||||
save!
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -11,6 +11,24 @@
|
||||
<span>Edit column</span>
|
||||
</button>
|
||||
</li>
|
||||
<li class="popup__item">
|
||||
<%= button_to column_left_position_path(column),
|
||||
class: "popup__btn btn",
|
||||
form_class: "display-contents",
|
||||
disabled: column.leftmost? do %>
|
||||
<%= icon_tag "arrow-left" %>
|
||||
<span>Move left</span>
|
||||
<% end %>
|
||||
</li>
|
||||
<li class="popup__item">
|
||||
<%= button_to column_right_position_path(column),
|
||||
class: "popup__btn btn",
|
||||
form_class: "display-contents",
|
||||
disabled: column.rightmost? do %>
|
||||
<%= icon_tag "arrow-right" %>
|
||||
<span>Move right</span>
|
||||
<% end %>
|
||||
</li>
|
||||
<li class="popup__item">
|
||||
<%= button_to collection_column_path(column.collection, column),
|
||||
method: :delete,
|
||||
@@ -21,6 +39,8 @@
|
||||
<span>Delete column</span>
|
||||
<% end %>
|
||||
</li>
|
||||
|
||||
|
||||
</ul>
|
||||
</dialog>
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<% if @left_column %>
|
||||
<%= turbo_stream.remove(dom_id(@column)) %>
|
||||
<%= turbo_stream.before(@left_column, partial: "collections/show/column", locals: { column: @column }) %>
|
||||
<% end %>
|
||||
@@ -0,0 +1,4 @@
|
||||
<% if @right_column %>
|
||||
<%= turbo_stream.remove(dom_id(@column)) %>
|
||||
<%= turbo_stream.after(@right_column, partial: "collections/show/column", locals: { column: @column }) %>
|
||||
<% end %>
|
||||
@@ -35,6 +35,11 @@ Rails.application.routes.draw do
|
||||
end
|
||||
end
|
||||
|
||||
resources :columns, only: [] do
|
||||
resource :left_position, module: :columns
|
||||
resource :right_position, module: :columns
|
||||
end
|
||||
|
||||
namespace :columns do
|
||||
resources :cards do
|
||||
scope module: "cards" do
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require_relative "../../config/environment"
|
||||
|
||||
ApplicationRecord.with_each_tenant do |tenant|
|
||||
puts "Processing tenant: #{tenant}"
|
||||
|
||||
Collection.find_each do |collection|
|
||||
puts " Processing collection: #{collection.name} (ID: #{collection.id})"
|
||||
|
||||
columns = collection.columns.order(:id)
|
||||
|
||||
columns.each_with_index do |column, index|
|
||||
column.update_column(:position, index)
|
||||
puts " Set position #{index} for column '#{column.name}' (ID: #{column.id})"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
puts "Migration completed!"
|
||||
Reference in New Issue
Block a user