Skip to content
Permalink
Browse files

Implement ActsAsList

  • Loading branch information...
niclin committed May 15, 2017
1 parent 48a4caa commit 7d58f3c55570a2b18b6608e33c4849e4be0a1d4c
@@ -32,6 +32,7 @@ gem 'simple_form'
gem 'font-awesome-rails'
gem 'carrierwave'
gem 'mini_magick'
gem 'acts_as_list'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
@@ -38,6 +38,8 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
acts_as_list (0.9.2)
activerecord (>= 3.0)
arel (7.1.1)
autoprefixer-rails (6.7.7.2)
execjs
@@ -182,6 +184,7 @@ PLATFORMS
ruby

DEPENDENCIES
acts_as_list
bootstrap-sass
byebug
carrierwave
@@ -5,7 +5,7 @@ class Admin::ProductsController < ApplicationController
before_action :admin_required

def index
@products = Product.all
@products = Product.all.order("position ASC")
end

def new
@@ -36,6 +36,18 @@ def create
end
end

def move_up
@product = Product.find(params[:id])
@product.move_higher
redirect_to :back
end

def move_down
@product = Product.find(params[:id])
@product.move_lower
redirect_to :back
end

private

def product_params
@@ -1,6 +1,6 @@
class ProductsController < ApplicationController
def index
@products = Product.all
@products = Product.all.order("position ASC")
end

def show
@@ -1,3 +1,4 @@
class Product < ApplicationRecord
mount_uploader :image, ImageUploader
acts_as_list
end
@@ -35,6 +35,8 @@
</td>
<td>
<%= link_to("Edit", edit_admin_product_path(product)) %>
<%= link_to "向上", move_up_admin_product_path(product), method: :patch %>
<%= link_to "向下", move_down_admin_product_path(product), method: :patch %>
</td>
</tr>
<% end %>
@@ -16,7 +16,12 @@
end

namespace :admin do
resources :products
resources :products do
member do
patch :move_up
patch :move_down
end
end
end

root 'products#index'
@@ -0,0 +1,9 @@
class AddPositionToProduct < ActiveRecord::Migration[5.0]
def change
add_column :products, :position, :integer

Product.order(:updated_at).each.with_index(1) do |product, index|
product.update_column :position, index
end
end
end
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170512123447) do
ActiveRecord::Schema.define(version: 20170515084500) do

create_table "cart_items", force: :cascade do |t|
t.integer "cart_id"
@@ -33,6 +33,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
t.integer "position"
end

create_table "users", force: :cascade do |t|

0 comments on commit 7d58f3c

Please sign in to comment.
You can’t perform that action at this time.