Skip to content

Instantly share code, notes, and snippets.

@hanaori
Created January 20, 2016 07:41
Show Gist options
  • Save hanaori/5db55e7a2388a112a9a9 to your computer and use it in GitHub Desktop.
Save hanaori/5db55e7a2388a112a9a9 to your computer and use it in GitHub Desktop.
lib/rails_admin/config/action/edit.rb
# ほぼオリジナルのパクリ(https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/edit.rb)
# 今回必要になる部分を追記する
module RailsAdmin
module Config
module Actions
class Edit < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
register_instance_option :member do
true
end
register_instance_option :http_methods do
[:get, :put]
end
register_instance_option :controller do
proc do
if request.get? # EDIT
  
# ↓ 挿入部分 ↓
if @model_name == "Product"
@order_objects = Product.joins(:images).includes(:images).find(@object.id).images
end
# ↑ 挿入部分 ↑
  
respond_to do |format|
format.html { render @action.template_name }
format.js { render @action.template_name, layout: false }
end
elsif request.put? # UPDATE
sanitize_params_for!(request.xhr? ? :modal : :update)
@object.set_attributes(params[@abstract_model.param_key])
@authorization_adapter && @authorization_adapter.attributes_for(:update, @abstract_model).each do |name, value|
@object.send("#{name}=", value)
end
changes = @object.changes
# ↓ 挿入部分 ↓
if @model_name == "Product"
params[:products].each do |params_product|
Item.find(params_product[:id]).update_attributes(order: params_product[:order])
end
end
       # ↑ 挿入部分 ↑
  
if @object.save
@auditing_adapter && @auditing_adapter.update_object(@object, @abstract_model, _current_user, changes)
respond_to do |format|
format.html { redirect_to_on_success }
format.js { render json: {id: @object.id.to_s, label: @model_config.with(object: @object).object_label} }
end
else
handle_save_error :edit
end
end
end
end
register_instance_option :link_icon do
'icon-pencil'
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment