Class: RailsDb::TablesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_db/tables_controller.rb

Instance Method Summary collapse

Instance Method Details

#csvObject



30
31
32
# File 'app/controllers/rails_db/tables_controller.rb', line 30

def csv
  send_data(@table.to_csv, type: 'text/csv; charset=utf-8; header=present', filename: "#{@table.name}.csv")
end

#dataObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/rails_db/tables_controller.rb', line 16

def data
  session[:per_page] = per_page
  @model   = @table.as_model
  @q       = @model.ransack(params[:q])
  @sql     = @q.result.page(params[:page]).per(per_page).to_sql
  @records = @q.result.page(params[:page]).per(per_page)
  @q.build_condition if @q.conditions.empty?
  @q.build_sort      if @q.sorts.empty?
  respond_to do |page|
    page.html {}
    page.js {}
  end
end

#destroyObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/rails_db/tables_controller.rb', line 43

def destroy
  @model   = @table.as_model
  @q       = @model.ransack(params[:q])
  @sql     = @q.result.page(params[:page]).per(per_page).to_sql
  @records = @q.result.page(params[:page]).per(per_page)
  @q.build_condition if @q.conditions.empty?
  @q.build_sort      if @q.sorts.empty?
  @table.delete(params[:pk_id])
  respond_to do |page|
    page.html { redirect_to action: :data, table_id: params[:table_id] }
    page.js {}
  end
end

#editObject



57
58
59
60
61
62
63
# File 'app/controllers/rails_db/tables_controller.rb', line 57

def edit
  @record = @table.as_model.find(params[:pk_id])
  respond_to do |page|
    page.html { redirect_to action: :data, table_id: params[:table_id] }
    page.js {}
  end
end

#indexObject



9
10
11
# File 'app/controllers/rails_db/tables_controller.rb', line 9

def index
  @tables = RailsDb::Database.accessible_tables
end

#showObject



13
14
# File 'app/controllers/rails_db/tables_controller.rb', line 13

def show
end

#truncateObject



38
39
40
41
# File 'app/controllers/rails_db/tables_controller.rb', line 38

def truncate
  @table.truncate
  render :data
end

#updateObject



65
66
67
68
69
70
71
72
# File 'app/controllers/rails_db/tables_controller.rb', line 65

def update
  @record = @table.as_model.find(params[:pk_id])
  @record.update_attributes(record_attributes)
  respond_to do |page|
    page.html { redirect_to action: :data, table_id: params[:table_id] }
    page.js {}
  end
end

#xlsxObject



34
35
36
# File 'app/controllers/rails_db/tables_controller.rb', line 34

def xlsx
  render xlsx: 'table', filename: "#{@table.name}.xlsx"
end