Module: ActiveAdminScopedCollectionActions::ResourceExtension

Defined in:
lib/active_admin_scoped_collection_actions/resource_extension.rb

Instance Method Summary collapse

Instance Method Details

#add_scoped_collection_action(name, options) ⇒ Object



21
22
23
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 21

def add_scoped_collection_action(name, options)
  (@scoped_collection_actions ||= {})[name.to_sym] = options
end

#add_scoped_collection_actions_sidebar_sectionObject



25
26
27
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 25

def add_scoped_collection_actions_sidebar_section
  self.sidebar_sections << scoped_collection_actions_sidebar_section
end

#initializeObject



4
5
6
7
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 4

def initialize(*)
  super
  add_scoped_collection_actions_sidebar_section
end

#scoped_collection_actionsObject



9
10
11
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 9

def scoped_collection_actions
  @scoped_collection_actions || {}
end

#scoped_collection_actions_ifObject



33
34
35
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 33

def scoped_collection_actions_if
  @if_proc
end

#scoped_collection_actions_if=(if_proc) ⇒ Object



29
30
31
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 29

def scoped_collection_actions_if=(if_proc)
  @if_proc = if_proc
end

#scoped_collection_actions_on_allObject



17
18
19
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 17

def scoped_collection_actions_on_all
  @scoped_collection_actions_unconditionally || false
end

#scoped_collection_actions_on_all=(bool) ⇒ Object



13
14
15
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 13

def scoped_collection_actions_on_all=(bool)
  @scoped_collection_actions_unconditionally = bool
end

#scoped_collection_actions_sidebar_sectionObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 51

def scoped_collection_actions_sidebar_section
  ActiveAdmin::SidebarSection.new :collection_actions, only: :index, if: scoped_collection_sidebar_condition do

    div I18n.t('active_admin_scoped_collection_actions.sidebar_msg')

    active_admin_config.scoped_collection_actions.each do |key, options={}|
      b_title = options.fetch(:title, ::ActiveSupport::Inflector.humanize(key))
      b_options = {}
      b_options[:class] = options[:class] if options[:class].present?
      # Important: If user did not specify html_class, then use default
      b_options[:class] = 'scoped_collection_action_button' unless b_options[:class]
      b_data = { auth_token: form_authenticity_token.to_s }
      b_data[:batch_action] = key.to_s
      if options[:form].present?
        b_data[:inputs] = options[:form].is_a?(Proc) ? options[:form].call : options[:form]
      end
      b_data[:confirm] = options.fetch(:confirm, I18n.t('active_admin_scoped_collection_actions.confirm_action_message'))
      b_options[:data] = b_data.to_json
      button b_title, b_options
    end
  end
end

#scoped_collection_sidebar_conditionObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_admin_scoped_collection_actions/resource_extension.rb', line 37

def scoped_collection_sidebar_condition
  -> do
    if active_admin_config.scoped_collection_actions_if.is_a?(Proc)
      instance_exec &active_admin_config.scoped_collection_actions_if
    else
      filtered_scoped = (params[:q] || params[:scope])
      on_all = active_admin_config.scoped_collection_actions_on_all
      has_actions = active_admin_config.scoped_collection_actions.any?
      batch_actions_enabled = active_admin_config.batch_actions_enabled?
      ( batch_actions_enabled && has_actions && (filtered_scoped || on_all) )
    end
  end
end