Class: Admin::BaseController

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AbAdmin::Controllers::Fv

#fv

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



22
23
24
# File 'app/controllers/admin/base_controller.rb', line 22

def settings
  @settings
end

Instance Method Details

#batchObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/admin/base_controller.rb', line 93

def batch
  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, *[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, *[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
  end
  redirect_to_back_or_root
end

#createObject



47
48
49
50
51
52
53
# File 'app/controllers/admin/base_controller.rb', line 47

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



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

def destroy
  destroy! do
    track_action! if @settings[:history]
    redirect_to_on_success
  end
end

#editObject



81
82
83
84
85
# File 'app/controllers/admin/base_controller.rb', line 81

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

#indexObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/admin/base_controller.rb', line 29

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(self, locale: params[:locale]), filename: doc.filename, type: Mime[:csv], disposition: 'attachment')
    end
    if Mime[:xlsx]
      format.xlsx do
        authorize! :export, resource_class
        doc = AbAdmin::Utils::XlsDocument.new(collection, export_options)
        send_data(doc.render(self, locale: params[:locale]), filename: doc.filename, type: Mime[:xlsx], disposition: 'attachment')
      end
    end
  end
end

#newObject



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

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

#showObject



75
76
77
78
79
# File 'app/controllers/admin/base_controller.rb', line 75

def show
  show! do |format|
    format.js { render layout: false }
  end
end

#updateObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/admin/base_controller.rb', line 55

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 }
    unless respond_to_format?(:json)
      success.json { head :no_content }
      failure.json { head :unprocessable }
    end
  end
end