Class: Admin::BaseController

Inherits:
InheritedResources::Base
  • Object
show all
Includes:
AbAdmin::Controllers::Callbacks
Defined in:
app/controllers/admin/base_controller.rb

Instance Method Summary collapse

Instance Method Details

#allow_batch_action?(batch_action) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/controllers/admin/base_controller.rb', line 91

def allow_batch_action?(batch_action)
  resource_class.batch_actions.include?(batch_action)
end

#apply_batch_action(item, batch_action) ⇒ Object



87
88
89
# File 'app/controllers/admin/base_controller.rb', line 87

def apply_batch_action(item, batch_action)
  item.send(batch_action)
end

#batchObject



75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/admin/base_controller.rb', line 75

def batch
  raise 'No ids specified for batch action' unless params[:ids].present?
  batch_action = params[:batch_action].to_sym
  if allow_batch_action?(batch_action) && collection.all?{|item| can?(batch_action, item) }
    count = collection.inject(0) { |c, item| apply_batch_action(item, batch_action) ? c + 1 : c }
    flash[:success] = I18n.t('admin.batch_actions.status', :count => count, :action => I18n.t("admin.actions.batch_#{batch_action}.title"))
  else
    raise CanCan::AccessDenied
  end
  redirect_to :back
end

#createObject



42
43
44
45
46
47
48
# File 'app/controllers/admin/base_controller.rb', line 42

def create
  create! do |success, failure|
    success.html { redirect_to redirect_to_on_success }
    success.js { render :layout => false }
    failure.js { render :new, :layout => false }
  end
end

#destroyObject



59
60
61
# File 'app/controllers/admin/base_controller.rb', line 59

def destroy
  destroy! { redirect_to_on_success }
end

#editObject



63
64
65
66
67
# File 'app/controllers/admin/base_controller.rb', line 63

def edit
  edit! do |format|
    format.js { render :layout => false }
  end
end

#indexObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/admin/base_controller.rb', line 26

def index
  super do |format|
    format.js { render collection }
    format.csv do
      doc = AbAdmin::Utils::CsvDocument.new(collection, export_options)
      send_data(doc.render, :filename => doc.filename, :type => Mime::CSV, :disposition => 'attachment')
    end
    if defined?(Mime::XLSX)
      format.xls do
        doc = AbAdmin::Utils::XlsDocument.new(collection, export_options)
        send_data(doc.render, :filename => doc.filename, :type => Mime::XLSX, :disposition => 'attachment')
      end
    end
  end
end

#newObject



69
70
71
72
73
# File 'app/controllers/admin/base_controller.rb', line 69

def new
  new! do |format|
    format.js { render :layout => false }
  end
end

#updateObject



50
51
52
53
54
55
56
57
# File 'app/controllers/admin/base_controller.rb', line 50

def update
  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 }
  end
end