Module: ActiveAdminScopedCollectionActions::DSL

Defined in:
lib/active_admin_scoped_collection_actions/dsl.rb

Instance Method Summary collapse

Instance Method Details

#add_scoped_collection_action_default_destroy(_, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_admin_scoped_collection_actions/dsl.rb', line 47

def add_scoped_collection_action_default_destroy(_, &block)
  batch_action :scoped_collection_destroy, if: proc { false } do |_|
    unless authorized?(:batch_destroy, resource_class)
      flash[:error] = I18n.t('active_admin_scoped_collection_actions.actions.no_permissions_msg')
      head :ok and next
    end
    if block_given?
      instance_eval &block
    else
      errors = []
      scoped_collection_records.find_each do |record|
        unless destroy_resource(record)
          errors << "#{record.attributes[resource_class.primary_key]} | #{I18n.t('active_admin_scoped_collection_actions.fail_on_destroy_msg')}"
        end
      end
      if errors.empty?
        flash[:notice] = I18n.t('active_admin_scoped_collection_actions.batch_destroy_status_msg')
      else
        flash[:error] = errors.join(". ")
      end
      head :ok
    end
  end
end

#add_scoped_collection_action_default_update(options, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_admin_scoped_collection_actions/dsl.rb', line 18

def add_scoped_collection_action_default_update(options, &block)
  batch_action :scoped_collection_update, if: proc { false } do
    unless authorized?(:batch_edit, resource_class)
      flash[:error] = I18n.t('active_admin_scoped_collection_actions.actions.no_permissions_msg')
      head :ok and next
    end
    if !params.has_key?(:changes) || params[:changes].empty?
      head :ok and next
    end
    permitted_changes = params.require(:changes).permit(*(options[:form].call.keys))
    if block_given?
      instance_eval &block
    else
      errors = []
      scoped_collection_records.find_each do |record|
        unless update_resource(record, [permitted_changes])
          errors << "#{record.attributes[resource_class.primary_key]} | #{record.errors.full_messages.join('. ')}"
        end
      end
      if errors.empty?
        flash[:notice] = I18n.t('active_admin_scoped_collection_actions.batch_update_status_msg')
      else
        flash[:error] = errors.join(". ")
      end
      head :ok
    end
  end
end

#scoped_collection_action(name, options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/active_admin_scoped_collection_actions/dsl.rb', line 4

def scoped_collection_action(name, options = {}, &block)
  if name == :scoped_collection_destroy
    options[:title] = I18n.t('active_admin_scoped_collection_actions.actions.delete') if options[:title].nil?
    add_scoped_collection_action_default_destroy(options, &block)
  elsif name == :scoped_collection_update
    options[:title] = I18n.t('active_admin_scoped_collection_actions.actions.update') if options[:title].nil?
    add_scoped_collection_action_default_update(options, &block)
  else
    batch_action(name, if: proc { false }, &block)
  end
  # sidebar button
  config.add_scoped_collection_action(name, options)
end