Class: Databasium::RecordsController

Inherits:
ApplicationController show all
Includes:
ActionView::RecordIdentifier, Pagy::Method
Defined in:
app/controllers/databasium/records_controller.rb

Instance Method Summary collapse

Instance Method Details

#bulk_destroyObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/databasium/records_controller.rb', line 58

def bulk_destroy
  deleted_records = @record_service.bulk_destroy(params[:ids])
  raise_user_error(deleted_records.errors.full_messages.join(", ")) unless deleted_records

  doms_ids = deleted_records.map { |record| dom_id(record) }

  render turbo_stream:
           doms_ids.flat_map { |dom_id|
             [
               turbo_stream.remove(dom_id),
               turbo_stream.remove("record-tab-#{dom_id}"),
               turbo_stream.remove("record-form-#{dom_id}")
             ]
           }
end

#createObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/databasium/records_controller.rb', line 11

def create
  new_record = @record_service.create_new(attributes: model_columns_params)
  unless new_record && new_record.valid?
    return raise_user_error(new_record.errors.full_messages.join(", "))
  end

  render turbo_stream: [
           turbo_stream.append(
             "records_body",
             Components::Databasium::Records::Table::Row.new(
               record: new_record,
               turbo_frame: "records_list"
             )
           ),
           turbo_stream.remove("suggestion")
         ]
end

#indexObject



7
8
9
# File 'app/controllers/databasium/records_controller.rb', line 7

def index
  render Views::Databasium::Records::Index.new
end

#recordsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/databasium/records_controller.rb', line 29

def records
  raise_user_error("Missing model for #{params[:table]} table.") if @model.blank?
  @context = records_context
  raw_records = @record_service.filter_records(filter_params)
  pagy, records = pagy(raw_records, limit: params[:limit].presence || 10, root_key: "records")

  if foreign_records_frame?(@context[:turbo_frame])
    render_foreign_records_table
  else
    respond_to do |format|
      format.html { render_records_table(records: records, pagy: pagy) }
      format.turbo_stream { render_records_table_turbo_stream(records: records, pagy: pagy) }
    end
  end
end


74
75
76
77
78
# File 'app/controllers/databasium/records_controller.rb', line 74

def sidebar
  @pagy_tables, @tables =
    pagy(@schema_service.get_tables(params[:search]), limit: 7, root_key: "tables")
  render Components::Databasium::SearchResults::Tables.new(tables: @tables, pagy: @pagy_tables)
end

#updateObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/databasium/records_controller.rb', line 45

def update
  record = @record_service.update_by_id(params[:id], attributes: model_columns_params)
  raise_user_error(record.errors.full_messages.join(", ")) unless record
  render turbo_stream:
           turbo_stream.replace(
             dom_id(record),
             Components::Databasium::Records::Table::Row.new(
               record: record,
               turbo_frame: "records_list"
             )
           )
end