Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Feature/pages index draggable position #288

Open
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ source 'https://rubygems.org/'

gem 'rails-controller-testing'
gem 'spree', github: 'spree/spree', branch: 'master'

gem 'spree_auth_devise'
gemspec
4 changes: 4 additions & 0 deletions app/controllers/spree/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ module Admin
class PagesController < ResourceController
private

def collection
model_class.order(position: :asc)
end

def update_page_attribute
params.require(:page).permit(permitted_params)
end
Expand Down
1 change: 1 addition & 0 deletions app/models/spree/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Spree::Page < ActiveRecord::Base
scope :by_store, ->(store) { joins(:stores).where('spree_pages_stores.store_id = ?', store) }

before_save :update_positions_and_slug
acts_as_list

def initialize(*args)
super(*args)
Expand Down
14 changes: 10 additions & 4 deletions app/views/spree/admin/pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
<% end %>

<% if @pages.any? %>
<table class="table" id="listing_pages" data-hook>
<table class="table sortable" id='listing_pages' data-sortable-link='<%= update_positions_admin_pages_url %>'>
<colgroup>
<col style="width: 5%">
<col style="width: 40%">
<col style="width: 35%">
<col style="width: 10%">
<col style="width: 15%">
<col style="width: 10%">
</colgroup>

<thead>
<tr data-hook="admin_pages_index_headers">
<th class="no-border"></th>
<th><%= Spree::Page.human_attribute_name(:title) %></th>
<th><%= Spree.t(:link, scope: :static_content) %></th>
<th class="text-center"><%= Spree::Page.human_attribute_name(:visible) %></th>
Expand All @@ -24,7 +27,10 @@
</thead>
<tbody>
<% @pages.each do |page| %>
<tr data-hook="admin_pages_index_rows" class="<%= cycle('odd', 'even') %>" id="<%= dom_id page %>">
<tr data-hook="admin_pages_index_rows" class="<%= cycle('odd', 'even') %>" id="<%= spree_dom_id page %>">
<td class="move-handle text-center">
<span class="icon icon-move handle"></span>
</td>
<td><%= page.title %></td>
<td><%= link_to page.link, page.link, target: '_blank' %></td>
<td class="text-center"><%= content_tag(:i, '', class: 'icon icon-ok') if page.visible %></td>
Expand All @@ -41,4 +47,4 @@
<%= Spree.t(:no_resource_found, resource: Spree::Page.model_name.human) %>,
<%= link_to Spree.t(:add_one), spree.new_admin_page_path %>!
</div>
<% end %>
<% end %>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Spree::Core::Engine.add_routes do
namespace :admin, path: Spree.admin_path do
resources :pages do
collection do
post :update_positions
end
patch :translate, on: :member
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/controllers/spree/admin/pages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
RSpec.describe Spree::Admin::PagesController, type: :controller do
stub_authorization!

describe '#update_positions' do
subject do
post :update_positions, params: {
positions: { page_1.position => '2', page_2.position => '1' }, format: 'js'
}
end

let(:page_1) { create(:page, position: 1) }
let(:page_2) { create(:page, position: 2) }

it 'updates the position of page 1' do
expect { subject }.to change { page_1.reload.position }.from(1).to(2)
end
end
end
1 change: 1 addition & 0 deletions spree_static_content.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.requirements << 'none'

s.add_dependency 'spree_core', '>= 3.7.0', '< 5.0'
s.add_dependency 'spree_backend', '>= 3.7.0', '< 5.0'
s.add_dependency 'spree_extension'
s.add_dependency 'deface', '~> 1.5'

Expand Down