Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made services orderable #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ config/database.yml
config/secret_token.yml
public/assets
Procfile.local
/vendor/
15 changes: 15 additions & 0 deletions app/assets/javascripts/admin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ $ ->
notices.hide('fast')
, 5000

$('.data-orderable-list').each ->
new Sortable(this,
group: 'omega'
handle: '.drag-handle'
onUpdate: ->
orderedIds = $.map($(this.el).find('.data-orderable-id'), (elem) ->
$(elem).data 'orderable-id'
)
$.ajax
async: true
url: "/admin/services/reorder"
type: 'POST'
data: ids: orderedIds
)

#
# Open external links in new window
#
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/vendor/sortable.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions app/controllers/admin/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def destroy
redirect_to admin_services_path, :notice => "#{@service.name} has been removed successfully."
end

def reorder
Service.reorder(params[:ids])
render body: nil, status: :no_content
end

private

def safe_params
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class PagesController < ApplicationController

def index
@services = Service.ordered.includes(:group, :status, {:active_maintenances => :service_status})
@services_with_group = @services.group_by(&:group).sort_by { |g,_| g ? g.name : 'zzz' }
@services_with_group = @services.group_by(&:group).sort_by { |g,_| g ? g.name : '000' }
@issues = Issue.ongoing.ordered.to_a
@maintenances = Maintenance.open.ordered.to_a
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ def self.create_defaults
Service.create!(:name => "Customer Support")
end

def self.reorder(ids)
ids.map(&:to_i).each_with_index do |id, index|
service = Service.find(id)
service.update(:position => index + 1)
end
end

end
5 changes: 3 additions & 2 deletions app/views/admin/services/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
%td Name
%td Status
%td
%tbody
%tbody.data-orderable-list
- for service in @services
%tr
%td= link_to service.name, [:edit, :admin, service], :class => 'u-underline'
%td
%span.drag-handle= link_to service.name, [:edit, :admin, service], :class => 'u-underline data-orderable-id', :data => {:orderable_id => service.id}
%td= service_status_tag service, :link_maintenance => :admin
%td.u-align-right= link_to "Edit", [:edit, :admin, service], :class => 'button button--small'
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
resources :service_groups
resources :email_templates, :only => [:index, :edit, :update, :destroy]
resources :api_tokens
post 'services/reorder' => 'services#reorder'

#
# Issues
Expand Down