app/controllers/admin/base_controller.rb in ab_admin-0.7.0 vs app/controllers/admin/base_controller.rb in ab_admin-0.8.0
- old
+ new
@@ -17,11 +17,11 @@
has_scope :by_ids, type: :array
helper_method :admin?, :moderator?
- helper_method :button_scopes, :collection_action?, :action_items, :resource_action_items,
+ helper_method :button_scopes, :collection_action?, :action_items, :resource_action_items, :query_params,
:preview_resource_path, :get_subject, :settings, :batch_action_list, :tree_node_renderer,
:normalized_index_views, :current_index_view, :pjax?, :xhr?, :max_per_page, :params_for_links
rescue_from ::CanCan::AccessDenied, with: :render_unauthorized
@@ -204,19 +204,11 @@
def preview_resource_path(item)
nil
end
def settings
- {
- index_view: 'table',
- sidebar: collection_action?,
- well: (collection_action? || %w(show history).include?(action_name)),
- search: true,
- batch: true,
- hotkeys: true,
- list_dblclick: true
- }
+ {sidebar: collection_action?, well: (collection_action? || %w(show history).include?(action_name))}.update(AbAdmin.default_resource_settings)
end
def action_items
case action_name.to_sym
when :show
@@ -234,13 +226,14 @@
def collection_action?
%w(index search batch rebuild).include?(action_name)
end
- def self.scope(name, options={})
- has_scope name, options.without(:badge)
+ def self.scope(name, options={}, &block)
+ has_scope name, options.without(:badge, :if), &block
options[:badge] = {} if options[:badge] && !options[:badge].is_a?(Hash)
+ options[:block] = block
self.button_scopes ||= []
self.button_scopes << [name, options]
end
def button_scopes
@@ -277,14 +270,20 @@
def tree_node_renderer
@tree_node_renderer ||= lambda { |r| link_to AbAdmin.display_name(r), resource_path(r), class: 'tree-item_link' }
end
def search_collection
- query = params[:q].try!{|q| q.permit!.to_h } || {}
+ @search_collection ||= begin
+ @search = end_of_association_chain.accessible_by(current_ability).ransack(query_params)
+ with_scopes(@search.result(distinct: @search.object.joins_values.present?)).admin(current_user)
+ end
+ end
+
+ def query_params
+ query = params[:q].try! {|q| q.permit!.to_h} || {}
nested = resource_class.respond_to?(:acts_as_nested_set_options) && current_index_view == 'tree'
query[:s] ||= settings[:default_order] || ('id desc' unless nested)
- @search = with_scopes(end_of_association_chain.accessible_by(current_ability).admin).ransack(query.no_blank)
- @search.result(distinct: @search.object.joins_values.present?)
+ query.reject_blank
end
def with_scopes(relation)
relation
end