app/controllers/admin/base_controller.rb in ab_admin-0.6.1 vs app/controllers/admin/base_controller.rb in ab_admin-0.7.0
- old
+ new
@@ -19,27 +19,27 @@
helper_method :admin?, :moderator?
helper_method :button_scopes, :collection_action?, :action_items, :resource_action_items,
:preview_resource_path, :get_subject, :settings, :batch_action_list, :tree_node_renderer,
- :normalized_index_views, :current_index_view, :pjax?, :xhr?, :max_per_page
+ :normalized_index_views, :current_index_view, :pjax?, :xhr?, :max_per_page, :params_for_links
rescue_from ::CanCan::AccessDenied, with: :render_unauthorized
def index
super do |format|
format.js { render layout: false }
format.csv do
authorize! :export, resource_class
doc = AbAdmin::Utils::CsvDocument.new(collection, export_options)
- send_data(doc.render, filename: doc.filename, type: Mime::CSV, disposition: 'attachment')
+ send_data(doc.render, filename: doc.filename, type: Mime[:csv], disposition: 'attachment')
end
- if defined?(Mime::XLSX)
- format.xls do
+ if Mime[:xlsx]
+ format.xlsx do
authorize! :export, resource_class
doc = AbAdmin::Utils::XlsDocument.new(collection, export_options)
- send_data(doc.render, filename: doc.filename, type: Mime::XLSX, disposition: 'attachment')
+ send_data(doc.render, filename: doc.filename, type: Mime[:xlsx], disposition: 'attachment')
end
end
end
end
@@ -93,14 +93,21 @@
raise 'No ids specified for batch action' unless params[:by_ids].present?
batch_action = params[:batch_action].to_sym
if allow_batch_action?(batch_action) && collection.all?{|item| can?(batch_action, item) }
if batch_action.to_s.end_with?('_collection')
count = collection.size
- resource_class.public_send(batch_action, collection)
- collection.each { |item| track_action!("batch_#{batch_action}", item) } if settings[:history]
+ resource_class.public_send(batch_action, collection, *[params[:batch_params]].compact)
+ if settings[:history]
+ if Object.const_defined?('ActiveRecord::Import') && Track.respond_to?(:import)
+ tracks = collection.map { |item| track_action("batch_#{batch_action}", item) }
+ Track.import_from_batch_collection_action(tracks)
+ else
+ collection.each { |item| track_action!("batch_#{batch_action}", item) }
+ end
+ end
else
- count = collection.inject(0) { |c, item| apply_batch_action(item, batch_action) ? c + 1 : c }
+ count = collection.inject(0) { |c, item| apply_batch_action(item, batch_action, *[params[:batch_params]].compact) ? c + 1 : c }
end
batch_action_name = I18n.t("admin.actions.batch_#{batch_action}.title", default: batch_action.to_s.humanize)
flash[:success] = I18n.t('admin.batch_actions.status', count: count, action: batch_action_name)
else
raise CanCan::AccessDenied
@@ -108,10 +115,18 @@
redirect_to_back_or_root
end
protected
+ def build_resource_params
+ permitted_params || params[resource_class.model_name.param_key]
+ end
+
+ def params_for_links
+ params.slice(:q, :s, :model_name, :per_page, :page).permit!
+ end
+
def respond_to_format?(format)
self.class.mimes_for_respond_to[format]
end
def default_url_options
@@ -119,22 +134,22 @@
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
end
- def apply_batch_action(item, batch_action)
- success = item.send(batch_action)
+ def apply_batch_action(item, batch_action, *batch_params)
+ success = item.send(batch_action, *batch_params)
track_action!("batch_#{batch_action}", item) if settings[:history]
success
end
def allow_batch_action?(batch_action)
- resource_class.batch_actions.include?(batch_action)
+ batch_action_list.detect { |a| a.name == batch_action }
end
def redirect_to_back_or_root
- redirect_to request.env['HTTP_REFERER'] ? :back : admin_root_path
+ redirect_back fallback_location: admin_root_path
end
def track_action(key=nil, item=nil)
(item || resource).track(key: key || action_name, user: current_user)
end
@@ -145,10 +160,14 @@
def batch_action_list
self.class.batch_action_list ||= begin
resource_class.batch_actions.map do |a|
opts = a == :destroy ? {confirm: I18n.t('admin.delete_confirmation')} : {}
+ if a.is_a?(Hash)
+ opts.merge!(a.except(:name))
+ a = a[:name]
+ end
AbAdmin::Config::BatchAction.new(a, opts)
end
end
end
@@ -163,11 +182,11 @@
def track_current_action(*)
track_action if settings[:history]
end
- def interpolation_options
+ def flash_interpolation_options
return {} if collection_action? || resource.errors.empty?
{errors: resource.errors.full_messages.map { |m| "<br/> - #{m}" }.join.html_safe}
end
def self.export(options={}, &block)
@@ -215,12 +234,20 @@
def collection_action?
%w(index search batch rebuild).include?(action_name)
end
+ def self.scope(name, options={})
+ has_scope name, options.without(:badge)
+ options[:badge] = {} if options[:badge] && !options[:badge].is_a?(Hash)
+ self.button_scopes ||= []
+ self.button_scopes << [name, options]
+ end
+
def button_scopes
- self.class.button_scopes ||= self.class.scopes_configuration.except(:by_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 }
+ self.class.button_scopes.to_h
end
def add_breadcrumbs
@breadcrumbs = []
if parent?
@@ -250,17 +277,21 @@
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
- params[:q] ||= {}
+ query = params[:q].try!{|q| q.permit!.to_h } || {}
nested = resource_class.respond_to?(:acts_as_nested_set_options) && current_index_view == 'tree'
- params[:q][:s] ||= settings[:default_order] || ('id desc' unless nested)
- @search = end_of_association_chain.accessible_by(current_ability).admin.ransack(params[:q].no_blank)
+ 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?)
end
+ def with_scopes(relation)
+ relation
+ end
+
def collection
@collection ||= begin
result = search_collection
result = result.paginate(page: params[:page], per_page: per_page, large: true) if action_name != 'batch' && !settings[:skip_pagination]
result
@@ -337,20 +368,18 @@
fv.test = true
AbAdmin.test_settings.each { |k, v| fv[k] = v }
end
end
- # utility methods
def pjax?
request.headers['X-PJAX']
end
def xhr?
request.xhr?
end
- # user role logic
def moderator?
user_signed_in? && current_user.moderator?
end
def admin?
@@ -367,35 +396,15 @@
def require_admin
raise CanCan::AccessDenied unless admin?
end
- def bind_current_user(*args)
+ def bind_current_user(*)
resource.user_id = current_user.id if !settings[:skip_bind_current_user] && resource.respond_to?(:user_id)
end
- def bind_current_updater(*args)
+ def bind_current_updater(*)
resource.updater_id = current_user.id if !settings[:skip_bind_current_updater] && resource.respond_to?(:updater_id)
- end
-
- # roles logic
- def role_given?
- fetch_role
- end
-
- def as_role
- {as: fetch_role}
- end
-
- def get_role
- return [:default, :moderator, :admin] if admin?
- return [:default, :moderator] if moderator?
- nil
- end
-
- def fetch_role
- return @as_role if defined?(@as_role)
- @as_role = get_role
end
def get_subject
params[:id] ? resource : resource_class
end