app/controllers/admin/base_controller.rb in ab_admin-0.4.0 vs app/controllers/admin/base_controller.rb in ab_admin-0.5.0
- old
+ new
@@ -55,11 +55,11 @@
update! do |success, failure|
success.html { redirect_to redirect_to_on_success }
failure.html { render :edit }
success.js { render layout: false }
failure.js { render :edit, layout: false }
- unless Admin::ManagerController.mimes_for_respond_to[:json]
+ unless respond_to_format?(:json)
success.json { head :no_content }
failure.json { head :unprocessable }
end
end
end
@@ -102,10 +102,14 @@
redirect_to_back_or_root
end
protected
+ def respond_to_format?(format)
+ self.class.mimes_for_respond_to[format]
+ end
+
def default_url_options
options = {format: nil}
options.update instance_exec(&AbAdmin.default_url_options) if AbAdmin.default_url_options
options.update instance_exec(&settings[:default_url_options]) if settings[:default_url_options]
options
@@ -145,14 +149,18 @@
def self.inherited(base)
super
base.class_eval do
before_create :bind_current_user
before_save :bind_current_updater
- before_save { track_action if settings[:history] }
+ before_save :track_current_action
end
end
+ def track_current_action(*)
+ track_action if settings[:history]
+ end
+
def interpolation_options
return {} if collection_action? || resource.errors.empty?
{errors: resource.errors.full_messages.map { |m| "<br/> - #{m}" }.join.html_safe}
end
@@ -195,11 +203,11 @@
def collection_action?
%w(index search batch rebuild).include?(action_name)
end
def button_scopes
- self.class.button_scopes ||= self.class.scopes_configuration.except(:ids).find_all{|_, s| s[:type] == :boolean }.to_h
+ self.class.button_scopes ||= self.class.scopes_configuration.except(:by_ids).find_all{|_, s| s[:type] == :boolean }.to_h
end
def add_breadcrumbs
@breadcrumbs = []
if parent?
@@ -230,25 +238,25 @@
@tree_node_renderer ||= lambda { |r| link_to AbAdmin.display_name(r), resource_path(r), class: 'tree-item_link' }
end
def search_collection
params[:q] ||= {}
- params[:q][:s] ||= 'id desc'
+ params[:q][:s] ||= settings[:default_order] || 'id desc'
@search = end_of_association_chain.accessible_by(current_ability).admin.ransack(params[:q].no_blank)
- @search.result(distinct: true)
+ @search.result(distinct: @search.object.joins_values.present?)
end
def collection
@collection ||= search_collection.paginate(page: params[:page], per_page: per_page, large: true)
end
def per_page
- return params[:per_page] if params[:per_page].present?
- if current_index_view == 'tree'
- params[:per_page] = 1000
+ request_per_page = (params[:per_page].presence || cookies[:pp].presence).to_i
+ if request_per_page.zero?
+ params[:per_page] = current_index_view == 'tree' ? 1000 : 50
else
- params[:per_page] = cookies[:pp] || 50
+ params[:per_page] = request_per_page
end
end
def set_layout
pjax? ? false : 'admin/application'
@@ -279,10 +287,12 @@
def redirect_to_on_success
if params[:_add_another]
new_resource_path(return_to: params[:return_to])
elsif params[:_add_edit]
edit_resource_path(resource, return_to: params[:return_to])
+ elsif params[:_save_and_show]
+ resource_path(resource, return_to: params[:return_to])
elsif params[:_add_edit_next] || params[:_add_edit_prev]
rec = resource.next_prev_by_url(end_of_association_chain.accessible_by(current_ability).unscoped.base, params[:return_to], !!params[:_add_edit_prev])
if rec
edit_resource_path(rec, return_to: params[:return_to])
else
@@ -300,11 +310,11 @@
fv.admin = admin?
fv.hotkeys = settings[:hotkeys]
fv.env = Rails.env
if AbAdmin.test_env?
fv.test = true
- AbAdmin.test_settings.each { |k, v| fv.set_variable k, v }
+ AbAdmin.test_settings.each { |k, v| fv[k] = v }
end
end
# utility methods
def pjax?
@@ -375,9 +385,17 @@
render partial: 'admin/shared/flash', locals: {flash: {alert: exception.message}}
elsif request.format.try(:html?)
redirect_to (current_user.try(:admin_access?) ? admin_root_path : root_path), alert: exception.message
else
head :unauthorized
+ end
+ end
+
+ def default_serializer_options
+ if resource_class
+ {root: resource_class.model_name.plural}
+ else
+ {root: false}
end
end
end
\ No newline at end of file