Class: MerbAdmin::Main

Inherits:
Application show all
Includes:
Merb::MerbAdmin::MainHelper
Defined in:
app/controllers/main.rb

Instance Method Summary collapse

Methods included from Merb::MerbAdmin::MainHelper

#object_title, #paginate

Instance Method Details

#createObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/main.rb', line 54

def create
  object = params[@abstract_model.singular_name] || {}
  # Delete fields that are blank
  object.each do |key, value|
    object[key] = nil if value.blank?
  end
  has_one_associations = @abstract_model.has_one_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
  has_many_associations = @abstract_model.has_many_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
  @object = @abstract_model.new(object)
  if @object.save && has_one_associations.each{|association, id| update_has_one_association(association, id)} && has_many_associations.each{|association, ids| update_has_many_association(association, ids)}
    if params[:_continue]
      redirect(slice_url(:admin_edit, :model_name => @abstract_model.singular_name, :id => @object.id), :message => {:notice => "#{@abstract_model.pretty_name.capitalize} was successfully created"})
    elsif params[:_add_another]
      redirect(slice_url(:admin_new, :model_name => @abstract_model.singular_name), :message => {:notice => "#{@abstract_model.pretty_name.capitalize} was successfully created"})
    else
      redirect(slice_url(:admin_list, :model_name => @abstract_model.singular_name), :message => {:notice => "#{@abstract_model.pretty_name.capitalize} was successfully created"})
    end
  else
    message[:error] = "#{@abstract_model.pretty_name.capitalize} failed to be created"
    render(:new, :layout => 'form')
  end
end

#deleteObject



99
100
101
# File 'app/controllers/main.rb', line 99

def delete
  render(:layout => 'form')
end

#destroyObject



103
104
105
106
107
108
109
# File 'app/controllers/main.rb', line 103

def destroy
  if @object.destroy
    redirect(slice_url(:admin_list, :model_name => @abstract_model.singular_name), :message => {:notice => "#{@abstract_model.pretty_name.capitalize} was successfully destroyed"})
  else
    raise BadRequest
  end
end

#editObject



50
51
52
# File 'app/controllers/main.rb', line 50

def edit
  render(:layout => 'form')
end

#indexObject



11
12
13
# File 'app/controllers/main.rb', line 11

def index
  render(:layout => 'dashboard')
end

#listObject



15
16
17
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
# File 'app/controllers/main.rb', line 15

def list
  options = {}
  merge_filter(options) 
  merge_query(options)
  merge_sort(options)

  if !MerbAdmin[:paginate] || params[:all]
    options = {
      :limit => 200,
    }.merge(options)
    @objects = @abstract_model.find_all(options).reverse
  else
    # monkey patch pagination
    @abstract_model.model.class_eval('is_paginated') unless @abstract_model.model.respond_to?(:paginated)
    @current_page = (params[:page] || 1).to_i
    options = {
      :page => @current_page,
      :per_page => MerbAdmin[:per_page],
    }.merge(options)
    @page_count, @objects = @abstract_model.model.paginated(options)
    options.delete(:page)
    options.delete(:per_page)
    options.delete(:offset)
    options.delete(:limit)
  end

  @record_count = @abstract_model.count(options)
  render(:layout => 'list')
end

#newObject



45
46
47
48
# File 'app/controllers/main.rb', line 45

def new
  @object = @abstract_model.new
  render(:layout => 'form')
end

#updateObject



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

def update
  object = params[@abstract_model.singular_name] || {}
  # Delete fields that are blank
  object.each do |key, value|
    object[key] = nil if value.blank?
  end
  has_one_associations = @abstract_model.has_one_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
  has_many_associations = @abstract_model.has_many_associations.map{|association| [association, (params[:associations] || {}).delete(association[:name])]}
  if @object.update_attributes(object) && has_one_associations.each{|association, id| update_has_one_association(association, id)} && has_many_associations.each{|association, ids| update_has_many_association(association, ids)} 
    if params[:_continue]
      redirect(slice_url(:admin_edit, :model_name => @abstract_model.singular_name, :id => @object.id), :message => {:notice => "#{@abstract_model.pretty_name.capitalize} was successfully updated"})
    elsif params[:_add_another]
      redirect(slice_url(:admin_new, :model_name => @abstract_model.singular_name), :message => {:notice => "#{@abstract_model.pretty_name.capitalize} was successfully updated"})
    else
      redirect(slice_url(:admin_list, :model_name => @abstract_model.singular_name), :message => {:notice => "#{@abstract_model.pretty_name.capitalize} was successfully updated"})
    end
  else
    message[:error] = "#{@abstract_model.pretty_name.capitalize} failed to be updated"
    render(:edit, :layout => 'form')
  end
end