Skip to content

Commit 1166ba2

Browse files
committed
closes concerto#1519 ordering content per feed
1 parent 0c2d40f commit 1166ba2

File tree

7 files changed

+67
-16
lines changed

7 files changed

+67
-16
lines changed

app/assets/javascripts/submissions.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ $(document).on('page:change', initBrowse);
7777

7878

7979
function initReorderSubmissions() {
80+
var feedId = $('.submissions-active').data('feed-id');
8081
$('.submissions-active .tile').on('dragstart', function(ev) {
8182
var submissionId = ev.target.closest('.tile').dataset.submissionId;
82-
// console.debug('set data', submissionId);
8383
ev.originalEvent.dataTransfer.setData('text/plain/id', submissionId);
8484
ev.originalEvent.dataTransfer.dropEffect = 'move';
8585
}).on('dragover', function (ev) {
@@ -91,14 +91,14 @@ function initReorderSubmissions() {
9191
if (fromId) {
9292
var beforeId = ev.target.closest('.tile').dataset.submissionId;
9393
if (fromId !== beforeId) {
94-
// console.debug('dropped', fromId, 'on', beforeId);
95-
// TODO! need to make this happen on the backend and then reflect the move here
96-
// but make sure that they are allowed to.
97-
$('.tile[data-submission-id=' + beforeId + ']').before($('.tile[data-submission-id=' + fromId + ']'));
94+
$.get('/feeds/' + feedId + '/submissions/' + fromId + '/reorder?before=' + beforeId)
95+
.done(function(data) {
96+
$('.tile[data-submission-id=' + beforeId + ']').before($('.tile[data-submission-id=' + fromId + ']'));
97+
});
9898
}
9999
}
100100
});
101101
}
102102

103-
$(document).ready(initReorderSubmissions);
103+
//$(document).ready(initReorderSubmissions);
104104
$(document).on('page:change', initReorderSubmissions);

app/controllers/submissions_controller.rb

+52-5
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ def index
1717
@submissions = []
1818
case state
1919
when 'expired'
20-
@submissions = @feed.submissions.approved.expired
20+
@submissions = @feed.submissions.approved.expired.reorder('contents.end_time desc')
2121
when 'future'
22-
@submissions = @feed.submissions.approved.future
22+
@submissions = @feed.submissions.approved.future.reorder('contents.start_time')
2323
when 'pending'
24-
@submissions = @feed.submissions.pending
24+
@submissions = @feed.submissions.pending.reorder('contents.start_time')
2525
when 'denied'
26-
@submissions = @feed.submissions.denied
26+
@submissions = @feed.submissions.denied.reorder('submissions.updated_at desc')
2727
else
28-
@submissions = @feed.submissions.approved.active
28+
@submissions = @feed.submissions.approved.active.reorder('submissions.seq_no, submissions.id')
2929
state = 'active'
3030
end
3131
@submissions = @submissions.includes(:content)
@@ -49,6 +49,53 @@ def index
4949
end
5050
end
5151

52+
# GET /feeds/:feed_id/submissions/1/reorder?before=
53+
# GET /feeds/:feed_id/submissions/1/reorder?before=.js
54+
def reorder
55+
@submission = Submission.find(params[:id])
56+
if cannot?(:moderate, @feed)
57+
head :forbidden
58+
else
59+
@before = Submission.find(params[:before])
60+
if @submission.blank? || @before.blank?
61+
head :not_found
62+
else
63+
@submissions = @feed.submissions.approved.active.reorder('submissions.seq_no, contents.start_time')
64+
seq_no = 0
65+
reserved_slot = 0
66+
parent_seq_nos = {}
67+
@submissions.each do |s|
68+
# child content also has a submission record, and its seq_no should match it's parent, so skip the children here
69+
next if s.content.parent.present?
70+
71+
seq_no = seq_no + 1
72+
if s.id == @before.id
73+
reserved_slot = seq_no
74+
seq_no = seq_no + 1
75+
s.seq_no = seq_no
76+
elsif s.id == @submission.id
77+
s.seq_no = reserved_slot
78+
else
79+
s.seq_no = seq_no
80+
end
81+
s.save
82+
83+
# keep track of each parent's seq_no so we can set their children
84+
if s.content.children_count > 0
85+
parent_seq_nos[s.content.id] = s.seq_no
86+
end
87+
end
88+
@submissions.each do |s|
89+
next if s.content.parent.blank?
90+
s.seq_no = parent_seq_nos[s.content.parent.id]
91+
s.save
92+
end
93+
94+
head :ok
95+
end
96+
end
97+
end
98+
5299
# GET /feeds/:feed_id/submissions/1
53100
# GET /feeds/:feed_id/submissions/1.js
54101
def show

app/controllers/users_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def show
1818
@memberships = @user.memberships.joins(:group).order('groups.name')
1919
auth!({action: :read, object: @memberships})
2020

21-
@contents = @user.contents.where('parent_id IS NULL')
21+
@contents = @user.contents.where('parent_id IS NULL').reorder('start_time desc')
2222
@contents_count = @contents.count
2323
@contents = Kaminari.paginate_array(@contents).page(params[:page])
2424
auth!({action: :read, object: @contents})

app/models/subscription.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def weight_name
3737

3838
# Get an array of all the approved active content to be shown in a screen's field.
3939
def contents
40-
@contents = self.feed.approved_contents.active.all.to_a
40+
@contents = self.feed.approved_contents.active.all.reorder('submissions.seq_no, contents.start_time').to_a
4141
run_callbacks :filter_contents do
4242
@contents.reject!{|c| !c.can_display_in?(self.screen, self.field)}
4343
end

app/views/submissions/_index_body.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
</div>
188188
</div>
189189
<br />
190-
<div class="submissions-<%= @state %>">
190+
<div class="submissions-<%= @state %>" data-feed-id="<%= @feed.id %>">
191191
<% @paginated_submissions.select{|s| @state != 'active' || s.content.parent.nil?}.each do |submission| %>
192192
<%= render partial: "submissions/submission_tile", locals: { submission: submission } %>
193193
<% end %>

config/routes.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ def matches?(request)
9494
collection do
9595
get :moderate
9696
end
97-
resources :submissions, only: [:index, :show, :update]
97+
resources :submissions, only: [:index, :show, :update] do
98+
member do
99+
get :reorder
100+
end
101+
end
98102
end
99103

100104
get 'content/search' => 'contents#index'

lib/base_shuffle.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ def remove_consecutive(arr)
3434
private
3535

3636
def content
37-
@subscriptions.collect{|s| s.contents.order('submissions.seq_no, submissions.id')}).flatten
37+
@subscriptions.collect{|s| s.contents }.flatten
3838
end
3939
end

0 commit comments

Comments
 (0)