Created
January 15, 2016 10:30
-
-
Save hanaori/38b529856ad5bcd5da11 to your computer and use it in GitHub Desktop.
lib/display_order_action.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 以下2つのファイルを require する | |
require "rails_admin/config/actions" | |
require "rails_admin/config/actions/base" | |
module RailsAdmin | |
module Config | |
module Actions | |
class DisplayOrderAction < RailsAdmin::Config::Actions::Base | |
RailsAdmin::Config::Actions.register(self) | |
# カスタムコントローラを作成するため、以下を true にする | |
register_instance_option :collection? do | |
true | |
end | |
register_instance_option :bulkable do | |
true | |
end | |
register_instance_option :http_methods do | |
[:get, :put] | |
end | |
# コントローラーアクションの処理 | |
register_instance_option :controller do | |
Proc.new do | |
if request.get? | |
# 一覧表示 (モデル内容を全て取得) | |
@objects = list_entries(@model_config, :destroy, get_association_scope_from_params, false) | |
elsif request.put? | |
# 表示順の更新 | |
objects = list_entries(@model_config, :destroy) | |
params[:products].each do |params_product| | |
object = objects.find(params_product[:id]) | |
object.update_attributes(order: params_product[:order]) | |
end | |
redirect_to(:back) | |
else | |
raise "エラーメッセージ" | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment