Class: Mg::ReportItemsController

Inherits:
Mg
  • Object
show all
Defined in:
lib/mountain-goat/controllers/mg/report_items_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mountain-goat/controllers/mg/report_items_controller.rb', line 20

def create
  @report = Mg::Report.find(params[:report_id])
  raise ArgumentError, "Invalid report" if @report.nil?
  
  @report_item = @report.mg_report_items.new(params[:report_item].clone.delete_if { |k, v| k.intern == :reportable || k.intern == :pivot } )
  @report_item.order = ( @report.mg_report_items.maximum(:order) || 0 ) + 1 # @report.mg_report_items.to_a.map { |ri| ri.order }.push(0).max + 1# -- weird sqlite3 bugs
  
  if !params[:report_item][:reportable].blank?
    id, model = params[:report_item][:reportable].split('-')
    @report_item.reportable = model.constantize.find(id)
  end
  
  if !params[:report_item][:pivot].blank?
    id, model = params[:report_item][:pivot].split('-')
    @report_item.pivot = model.constantize.find(id)
  end
  
  if @report_item.save
    render :json => { :success => true,
                      :close_dialog => true,
                      :result => "<span>Successfully added report item</span>",
                      :also => [ { :item => ".report-report-items", :result => render_to_string( :partial => "mg/reports/report_report_items", :locals => { :report => @report_item.mg_report } ) } ] }
  else
    render :json => { :success => true,
                      :result => render_to_string(:action => :new, :layout => 'xhr') }
  end
end

#destroyObject



109
110
111
112
113
114
# File 'lib/mountain-goat/controllers/mg/report_items_controller.rb', line 109

def destroy
  @report_item = Mg::ReportItem.find(params[:id])
  @report_item.destroy
  
  render :json => { :success => true, :result => render_to_string( :partial => "mg/reports/report_report_items", :locals => { :report => @report_item.mg_report } ) }
end

#downObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mountain-goat/controllers/mg/report_items_controller.rb', line 95

def down
  @report_item = Mg::ReportItem.find(params[:id])
  
  old_order = @report_item.order
  @report_item2 = Mg::ReportItem.first( :conditions => ["mg_report_items.order > ?", old_order], :order => "mg_report_items.order")
  
  ( render :json => { :success => true } and return ) if @report_item2.nil?
  
  @report_item.update_attribute(:order, @report_item2.order)
  @report_item2.update_attribute(:order, old_order)
  
  render :json => { :success => true, :result => render_to_string( :partial => "mg/reports/report_report_items", :locals => { :report => @report_item.mg_report } ) }
end

#editObject



48
49
50
51
52
53
54
# File 'lib/mountain-goat/controllers/mg/report_items_controller.rb', line 48

def edit
  @report_item = Mg::ReportItem.find(params[:id])
  @report = @report_item.mg_report
  
  render :json => { :success => true,
                      :result => render_to_string(:action => :edit, :layout => 'xhr') }
end

#get_extraObject



3
4
5
6
7
8
# File 'lib/mountain-goat/controllers/mg/report_items_controller.rb', line 3

def get_extra
  ( render :json => { :success => true, :result => "<span></span>" } and return ) if params[:value].blank?
  id, model = params[:value].split('-')
  reportable = model.constantize.find(id)
  render :json => { :success => true, :result => render_to_string( :partial => 'mg/report_items/report_item_pivot_form', :locals => { :reportable => reportable } ) }
end

#newObject

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
# File 'lib/mountain-goat/controllers/mg/report_items_controller.rb', line 10

def new
  @report = Mg::Report.find(params[:report_id])
  raise ArgumentError, "Invalid report" if @report.nil?
  
  @report_item = Mg::ReportItem.new
  
  render :json => { :success => true,
                      :result => render_to_string(:action => :new, :layout => 'xhr') }
end

#upObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mountain-goat/controllers/mg/report_items_controller.rb', line 81

def up
  @report_item = Mg::ReportItem.find(params[:id])
  
  old_order = @report_item.order
  @report_item2 = Mg::ReportItem.last( :conditions => ["mg_report_items.order < ?", old_order], :order => "mg_report_items.order")
  
  ( render :json => { :success => true } and return ) if @report_item2.nil?
  
  @report_item.update_attribute(:order, @report_item2.order)
  @report_item2.update_attribute(:order, old_order)
  
  render :json => { :success => true, :result => render_to_string( :partial => "mg/reports/report_report_items", :locals => { :report => @report_item.mg_report } ) }
end

#updateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mountain-goat/controllers/mg/report_items_controller.rb', line 56

def update
  @report_item = Mg::ReportItem.find(params[:id])
  @report_item.update_attributes(params[:report_item].clone.delete_if { |k, v| k.intern == :reportable || k.intern == :pivot } )
  
  if !params[:report_item][:reportable].blank?
    id, model = params[:report_item][:reportable].split('-')
    @report_item.reportable = model.constantize.find(id)
  end
  
  if !params[:report_item][:pivot].blank?
    id, model = params[:report_item][:pivot].split('-')
    @report_item.pivot = model.constantize.find(id)
  end
  
  if @report_item.save
    render :json => { :success => true,
                      :close_dialog => true,
                      :result => "<span>Successfully updated report item</span>",
                      :also => [ { :item => ".report-report-items", :result => render_to_string( :partial => "mg/reports/report_report_items", :locals => { :report => @report_item.mg_report }) } ] }
  else
    render :json => { :success => true,
                      :result => render_to_string(:action => :edit, :layout => 'xhr') }
  end
end