app/controllers/concerns/adminpanel/rest_actions.rb in adminpanel-2.2.5 vs app/controllers/concerns/adminpanel/rest_actions.rb in adminpanel-2.3.0

- old
+ new

@@ -1,87 +1,81 @@ module Adminpanel module RestActions + extend ActiveSupport::Concern + included do + before_action :set_resource_instance, only: [ + :show, + :edit, + :update, + :destroy, + :fb_choose_page, + :fb_save_token, + :fb_publish, + :twitter_publish, + :move_to_better, + :move_to_worst + ] + before_action :set_resource_collection, only: [:index, :destroy] + before_action :set_relationship_collections, only: [:new, :create, :edit, :update] + end def index - index! do |format| - format.html { render 'shared/index' } - end + render 'adminpanel/shared/index' end def show - show! do |format| - format.html { render 'shared/show' } - end + render 'adminpanel/shared/show' if stale?(last_modified: @resource_instance.updated_at.utc, etag: @resource_instance.cache_key) end def new - set_collections - new! do |format| - format.html { render 'shared/new' } - format.js { render 'shared/new', :locals => { :resource => resource }} + @resource_instance = @model.new + respond_to do |format| + render_new(format) end end def create merge_params - create! do |success, failure| - success.html do - flash[:success] = I18n.t('action.save_success') - redirect_to resource - end - failure.html do - set_collections - render 'shared/new' - end - success.js do - if params[:belongs_request] - render 'shared/create_belongs_to', :locals => { :resource => resource } - else - render 'shared/create_has_many', :locals => { :resource => resource } + @resource_instance = @model.new(send(whitelisted_params)) + respond_to do |format| + if @resource_instance.save + format.html { redirect_to @resource_instance } + format.js do + # if format js, request is from another controller's form + if params[:belongs_request] + render 'adminpanel/shared/create_belongs_to', locals: { resource: @resource_instance } + else + render 'adminpanel/shared/create_has_many', locals: { resource: @resource_instance } + end end + else + render_new(format) end - failure.js do - set_collections - render 'shared/new', :locals => {:resource => resource } - end - respond_to_json(success, failure) end end def edit - edit! do |format| - format.html do - set_collections - render 'shared/edit' - end - end + render 'adminpanel/shared/edit' end def update - update! do |success, failure| - success.html do - flash.now[:success] = I18n.t('action.save_success') - render 'shared/index' - end - failure.html do - set_collections - render 'shared/edit' - end - respond_to_json(success, failure) + if @resource_instance.update(send(whitelisted_params)) + redirect_to @resource_instance + else + render 'adminpanel/shared/edit' end end def destroy - destroy! do |format| - format.html { render 'shared/index' } - end + @resource_instance.destroy + redirect_to action: :index end private - def set_collections + def set_relationship_collections @collections = {} set_belongs_to_collections set_has_many_collections end @@ -104,22 +98,34 @@ @collections.merge!({"#{class_variable}" => class_variable.all}) end end def merge_params - params.merge({:model => params[:model]}) if params[:model].present? - params.merge({:model_name => params[:model_name]}) if params[:model_name].present? - params.merge({:belongs_request => params[:belongs_request]}) if params[:belongs_request].present? - params.merge({:currentcontroller => params[:currentcontroller]}) if params[:currentcontroller].present? + params.merge({model: params[:model]}) if params[:model].present? + params.merge({model_name: params[:model_name]}) if params[:model_name].present? + params.merge({belongs_request: params[:belongs_request]}) if params[:belongs_request].present? + params.merge({currentcontroller: params[:currentcontroller]}) if params[:currentcontroller].present? end - def respond_to_json(success, failure) - success.json do - render json: resource + def whitelisted_params + resource = controller_name.singularize.to_sym + "#{resource}_params" + end + + def set_resource_instance + @resource_instance = @model.find(params[:id]) + end + + def set_resource_collection + @collection = @model.all + end + + def render_new format + format.html do + render 'adminpanel/shared/new' end - failure.json do - render json: resource + format.js do + render 'adminpanel/shared/new', locals: { resource: @resource_instance } end end - end end