Class: RailsDb::TablesController

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

Constant Summary collapse

LOAD_TABLE_ACTIONS =
[:show, :data, :csv, :truncate, :destroy, :edit, :update, :xlsx, :search, :new, :create]

Instance Method Summary collapse

Instance Method Details

#createObject



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

def create
  @record = model.new(record_attributes)
  @record.save!
  build_search
  respond_to do |page|
    page.html { redirect_to action: :data, table_id: params[:table_id] }
    page.js {}
  end
end

#csvObject



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

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

#dataObject



28
29
30
31
32
33
34
35
# File 'app/controllers/rails_db/tables_controller.rb', line 28

def data
  session[:per_page] = per_page
  build_search
  respond_to do |page|
    page.html {}
    page.js {}
  end
end

#destroyObject



54
55
56
57
58
59
60
61
# File 'app/controllers/rails_db/tables_controller.rb', line 54

def destroy
  build_search
  @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



63
64
65
66
67
68
69
# File 'app/controllers/rails_db/tables_controller.rb', line 63

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



7
8
9
# File 'app/controllers/rails_db/tables_controller.rb', line 7

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

#newObject



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

def new
  @record = model.new
end

#showObject



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

def show
end

#truncateObject



49
50
51
52
# File 'app/controllers/rails_db/tables_controller.rb', line 49

def truncate
  @table.truncate
  render :data
end

#updateObject



71
72
73
74
75
76
77
78
# File 'app/controllers/rails_db/tables_controller.rb', line 71

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



41
42
43
44
45
46
47
# File 'app/controllers/rails_db/tables_controller.rb', line 41

def xlsx
  if defined? Axlsx
    render xlsx: 'table', filename: "#{@table.name}.xlsx"
  else
    raise 'RailsDb could not find Axlsx, please add it to your Gemfile: "gem \'axlsx_rails\'"'
  end
end