Skip to content

Commit a3862d4

Browse files
committed
Supporting sortable text_blocks
1 parent e4a3f84 commit a3862d4

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

app/controllers/text_blocks_controller.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,20 @@ def update
4040
r = RedmineTextBlocks::SaveTextBlock.(text_block_params,
4141
text_block: @text_block)
4242
if r.text_block_saved?
43-
redirect_to index_path
43+
respond_to do |format|
44+
format.html {
45+
flash[:notice] = l(:notice_successful_update)
46+
redirect_to index_path
47+
}
48+
format.js { head 200 }
49+
end
4450
else
45-
render 'edit'
51+
respond_to do |format|
52+
format.html {
53+
render 'edit'
54+
}
55+
format.js { head 422 }
56+
end
4657
end
4758
end
4859

@@ -69,7 +80,7 @@ def index_path
6980
end
7081

7182
def text_block_params
72-
params[:text_block].permit :name, :text, :issue_status_ids => []
83+
params[:text_block].permit :name, :text, :issue_status_ids, :position
7384
end
7485

7586
def find_text_block
@@ -83,7 +94,7 @@ def find_project_by_project_id
8394
end
8495

8596
def text_block_scope
86-
TextBlock.order(name: :asc).where(project_id: @project&.id)
97+
TextBlock.where(project_id: @project&.id).sorted
8798
end
8899

89100
def get_issue_statuses

app/models/text_block.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class TextBlock < ActiveRecord::Base
22
belongs_to :project
33
has_and_belongs_to_many :issue_statuses
4+
acts_as_positioned :scope => [:project_id]
45

56
validates :name, presence: true
67
validate :name_uniqueness
7-
8+
scope :sorted, ->{ order :position }
89

910
private
1011

app/views/projects/settings/_text_blocks.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
<%= link_to l(:label_text_block_new), new_project_text_block_path(@project), class: 'icon icon-add' %>
33
</p>
44

5-
<%= render partial: 'text_blocks/list', locals: { text_blocks: TextBlock.where(project_id: @project.id) } %>
5+
<%= render partial: 'text_blocks/list', locals: { text_blocks: TextBlock.where(project_id: @project.id).sorted } %>
66

app/views/text_blocks/_list.html.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@
3232
$("table.list.textblocks tbody td.text div").trigger('update.dot');
3333
}
3434
});
35+
$(function() {
36+
$("table.textblocks tbody").positionedItems({
37+
scroll: false,
38+
sort: function (event, ui) {
39+
ui.helper.css({'top': ui.position.top + $(window).scrollTop() + 'px'});
40+
},
41+
});
42+
});
3543
<% end %>
3644
<% end %>

app/views/text_blocks/_text_block.html.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
<%= textilizable s %>
77
<% end %>
88
</div></td>
9-
<td><%= delete_link(text_block.project ? project_text_block_path(text_block.project, text_block) : text_block_path(text_block)) %>
9+
<td class="buttons">
10+
<%= reorder_handle(text_block, url: text_block.project ? project_text_block_path(text_block.project, text_block) : text_block_path(text_block)) %>
11+
<%= delete_link(text_block.project ? project_text_block_path(text_block.project, text_block) : text_block_path(text_block)) %>
12+
</td>
1013
</tr>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class AddPositionToTextBlocks < ActiveRecord::Migration[5.2]
22
def change
3-
add_column :text_blocks, :position, :integer
3+
add_column :text_blocks, :position, :integer, :default => 1
44
end
55
end

0 commit comments

Comments
 (0)