Class: RailsAdmin::MainController

Inherits:
ApplicationController show all
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/controllers/rails_admin/main_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#get_model, #get_object, #to_model_name

Instance Method Details

#bulk_actionObject



219
220
221
222
223
224
225
226
# File 'app/controllers/rails_admin/main_controller.rb', line 219

def bulk_action
  redirect_to index_path, :flash => { :info => t("admin.flash.noaction") } and return if params[:bulk_ids].blank?
  case params[:bulk_action]
  when "delete" then bulk_delete
  when "export" then export
  else redirect_to(index_path(:model_name => @abstract_model.to_param), :flash => { :info => t("admin.flash.noaction") })
  end
end

#bulk_deleteObject



228
229
230
231
232
233
234
235
236
# File 'app/controllers/rails_admin/main_controller.rb', line 228

def bulk_delete
  @authorization_adapter.authorize(:bulk_delete, @abstract_model) if @authorization_adapter
  @page_name = t("admin.actions.delete").capitalize + " " + @model_config.label.downcase
  @page_type = @abstract_model.pretty_name.downcase
  @objects = list_entries(@model_config, :destroy)
  not_found and return if @objects.empty?
  
  render :action => 'bulk_delete'
end

#bulk_destroyObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/controllers/rails_admin/main_controller.rb', line 238

def bulk_destroy
  @authorization_adapter.authorize(:bulk_destroy, @abstract_model) if @authorization_adapter
  @objects = list_entries(@model_config, :destroy)
  processed_objects = @abstract_model.destroy(@objects)
  
  destroyed = processed_objects.select(&:destroyed?)
  not_destroyed = processed_objects - destroyed

  destroyed.each do |object|
    message = "Destroyed #{@model_config.with(:object => object).object_label}"
    History.create_history_item(message, object, @abstract_model, _current_user)
  end

  unless destroyed.empty?
    flash[:success] = t("admin.flash.successful", :name => pluralize(destroyed.count, @model_config.label), :action => t("admin.actions.deleted"))
  end

  unless not_destroyed.empty?
    flash[:error] = t("admin.flash.error", :name => pluralize(not_destroyed.count, @model_config.label), :action => t("admin.actions.deleted"))
  end

  redirect_to index_path
end

#createObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/rails_admin/main_controller.rb', line 97

def create
  @modified_assoc = []
  @object = @abstract_model.new
  @model_config.create.fields.each {|f| f.parse_input(@attributes) if f.respond_to?(:parse_input) }
  if @authorization_adapter
    @authorization_adapter.attributes_for(:create, @abstract_model).each do |name, value|
      @object.send("#{name}=", value)
    end
    @authorization_adapter.authorize(:create, @abstract_model, @object)
  end
  @object.set_attributes(@attributes, _attr_accessible_role)
  @page_name = t("admin.actions.create").capitalize + " " + @model_config.label.downcase
  @page_type = @abstract_model.pretty_name.downcase

  if @object.save
    History.create_history_item("Created #{@model_config.with(:object => @object).object_label}", @object, @abstract_model, _current_user)
    respond_to do |format|
      format.html do
        redirect_to_on_success
      end
      format.js do
        render :json => {
          :id => @object.id,
          :label => @model_config.with(:object => @object).object_label,
        }
      end
    end
  else
    handle_save_error
  end
end

#dashboardObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/rails_admin/main_controller.rb', line 13

def dashboard
  @page_name = t("admin.dashboard.pagename")
  @page_type = "dashboard"

  @history= History.all
  @abstract_models = RailsAdmin::Config.visible_models.map(&:abstract_model)
  
  @most_recent_changes = {}
  @count = {}
  @max = 0
  @abstract_models.each do |t|
    scope = @authorization_adapter && @authorization_adapter.query(:index, t)
    current_count = t.count({}, scope)
    @max = current_count > @max ? current_count : @max
    @count[t.pretty_name] = current_count
    @most_recent_changes[t.pretty_name] = t.model.order("updated_at desc").first.try(:updated_at) rescue nil
  end
  render :dashboard
end

#deleteObject



176
177
178
179
180
181
182
183
184
# File 'app/controllers/rails_admin/main_controller.rb', line 176

def delete
  @authorization_adapter.authorize(:delete, @abstract_model, @object) if @authorization_adapter
  @page_name = "#{t("admin.actions.delete").capitalize} #{@model_config.label.downcase} '#{@object.send(@model_config.object_label_method)}'"
  @page_type = @abstract_model.pretty_name.downcase
  respond_to do |format|
    format.html
    format.js   { render :layout => false }
  end
end

#destroyObject



186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/controllers/rails_admin/main_controller.rb', line 186

def destroy
  @authorization_adapter.authorize(:destroy, @abstract_model, @object) if @authorization_adapter

  if @abstract_model.destroy(@object)
    History.create_history_item("Destroyed #{@model_config.with(:object => @object).object_label}", @object, @abstract_model, _current_user)
    flash[:success] = t("admin.flash.successful", :name => @model_config.label, :action => t("admin.actions.deleted"))
  else
    flash[:error] = t("admin.flash.error", :name => @model_config.label, :action => t("admin.actions.deleted"))
  end

  redirect_to index_path(:model_name => @abstract_model.to_param)
end

#editObject



135
136
137
138
139
140
141
142
143
# File 'app/controllers/rails_admin/main_controller.rb', line 135

def edit
  @authorization_adapter.authorize(:edit, @abstract_model, @object) if @authorization_adapter
  @page_name = "#{t("admin.actions.update").capitalize} #{@model_config.label.downcase} '#{@object.send(@model_config.object_label_method)}'"
  @page_type = @abstract_model.pretty_name.downcase
  respond_to do |format|
    format.html
    format.js   { render :layout => false }
  end
end

#exportObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/controllers/rails_admin/main_controller.rb', line 199

def export
  # todo
  #   limitation: need to display at least one real attribute ('only') so that the full object doesn't get displayed, a way to fix this? maybe force :only => [""]
  #   use send_file instead of send_data to leverage the x-sendfile header set by rails 3 (generate and let the front server handle the rest)
  # maybe
  #   n-levels (backend: possible with xml&json, frontend: not possible?)
  @authorization_adapter.authorize(:export, @abstract_model) if @authorization_adapter

  if format = params[:json] && :json || params[:csv] && :csv || params[:xml] && :xml
    request.format = format
    @schema = params[:schema].symbolize if params[:schema] # to_json and to_xml expect symbols for keys AND values.
    index
  else
    @page_name = t("admin.actions.export").capitalize + " " + @model_config.label_plural.downcase
    @page_type = @abstract_model.pretty_name.downcase

    render :action => 'export'
  end
end

#indexObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/rails_admin/main_controller.rb', line 33

def index
  @authorization_adapter.authorize(:index, @abstract_model) if @authorization_adapter

  @page_type = @abstract_model.pretty_name.downcase
  @page_name = t("admin.index.select", :name => @model_config.label.downcase)

  @objects = list_entries

  @schema ||= { :only => @model_config.list.visible_fields.map { |f| f.name } }

  respond_to do |format|
    format.html { render :layout => !request.xhr? }
    format.json do
      output = if params[:compact]
        @objects.map{ |o| { :id => o.id, :label => o.send(@model_config.object_label_method) } }
      else
        @objects.to_json(@schema)
      end
      if params[:send_data]
        send_data output, :filename => "#{params[:model_name]}_#{DateTime.now.strftime("%Y-%m-%d_%Hh%Mm%S")}.json"
      else
        render :json => output
      end
    end
    format.xml do
      output = @objects.to_xml(@schema)
      if params[:send_data]
        send_data output, :filename => "#{params[:model_name]}_#{DateTime.now.strftime("%Y-%m-%d_%Hh%Mm%S")}.xml"
      else
        render :xml => output
      end
    end
    format.csv do
      header, encoding, output = CSVConverter.new(@objects, @schema).to_csv(params[:csv_options])
      if params[:send_data]
        send_data output,
          :type => "text/csv; charset=#{encoding}; #{"header=present" if header}",
          :disposition => "attachment; filename=#{params[:model_name]}_#{DateTime.now.strftime("%Y-%m-%d_%Hh%Mm%S")}.csv"
      else
        render :text => output
      end
    end
  end
end

#list_entries(model_config = @model_config, auth_scope_key = :index, additional_scope = get_association_scope_from_params) ⇒ Object



262
263
264
265
266
267
# File 'app/controllers/rails_admin/main_controller.rb', line 262

def list_entries(model_config = @model_config, auth_scope_key = :index, additional_scope = get_association_scope_from_params)
  scope = @authorization_adapter && @authorization_adapter.query(auth_scope_key, model_config.abstract_model)
  scope = model_config.abstract_model.scoped.merge(scope)
  scope = scope.instance_eval(&additional_scope) if additional_scope
  get_collection(model_config, scope)
end

#newObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/rails_admin/main_controller.rb', line 78

def new
  @object = @abstract_model.new
  if @authorization_adapter
    @authorization_adapter.attributes_for(:new, @abstract_model).each do |name, value|
      @object.send("#{name}=", value)
    end
    @authorization_adapter.authorize(:new, @abstract_model, @object)
  end
  if object_params = params[@abstract_model.to_param]
    @object.set_attributes(@object.attributes.merge(object_params), _attr_accessible_role)
  end
  @page_name = t("admin.actions.create").capitalize + " " + @model_config.label.downcase
  @page_type = @abstract_model.pretty_name.downcase
  respond_to do |format|
    format.html
    format.js   { render :layout => false }
  end
end

#showObject



129
130
131
132
133
# File 'app/controllers/rails_admin/main_controller.rb', line 129

def show
  @authorization_adapter.authorize(:show, @abstract_model, @object) if @authorization_adapter
  @page_name = t("admin.show.page_name", :name => "#{@model_config.label.downcase} '#{@object.send(@model_config.object_label_method)}'")
  @page_type = @abstract_model.pretty_name.downcase
end

#updateObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/rails_admin/main_controller.rb', line 145

def update
  @authorization_adapter.authorize(:update, @abstract_model, @object) if @authorization_adapter

  @cached_assocations_hash = associations_hash
  @modified_assoc = []

  @page_name = "#{t("admin.actions.update").capitalize} #{@model_config.label.downcase} '#{@object.send(@model_config.object_label_method)}'"
  @page_type = @abstract_model.pretty_name.downcase

  @old_object = @object.dup
  @model_config.update.fields.map {|f| f.parse_input(@attributes) if f.respond_to?(:parse_input) }
  @object.set_attributes(@attributes, _attr_accessible_role)

  if @object.save
    History.create_update_history @abstract_model, @object, @cached_assocations_hash, associations_hash, @modified_assoc, @old_object, _current_user
    respond_to do |format|
      format.html do
        redirect_to_on_success
      end
      format.js do
        render :json => {
          :id => @object.id,
          :label => @model_config.with(:object => @object).object_label,
        }
      end
    end
  else
    handle_save_error :edit
  end
end