Class: Solidcrud::AdminController

Inherits:
ApplicationController show all
Defined in:
app/controllers/solidcrud/admin_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#flash

Instance Method Details

#createObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/solidcrud/admin_controller.rb', line 88

def create
  @model_name = params[:model]
  @model_klass = model_class(@model_name)
  @record = @model_klass.new(record_params)

  respond_to do |format|
    if @record.save
      format.html { redirect_to solidcrud.admin_model_path(@model_name), notice: 'Created!' }
      format.turbo_stream do
        render turbo_stream: [
          turbo_stream.replace("flash-messages", partial: "solidcrud/admin/shared/flash_messages", locals: { notice: 'Created!' }),
          turbo_stream.replace("records-table", partial: "solidcrud/admin/shared/records_table", locals: { records: @model_klass.all.order(id: :desc).limit(25), model_name: @model_name, model_klass: @model_klass }),
          turbo_stream.append("notification-container", 
            '<div class="notification-trigger" data-type="success" data-message="Record created successfully!" data-auto-dismiss="true" style="display: none;"></div>')
        ]
      end
    else
      format.html { render :new }
      format.turbo_stream { render turbo_stream: turbo_stream.replace("new-form", partial: "solidcrud/admin/shared/form", locals: { record: @record, model_name: @model_name, model_klass: @model_klass }) }
    end
  end
end

#dashboard_statsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/solidcrud/admin_controller.rb', line 13

def dashboard_stats
  @models_count = @models&.count || 0
  @rails_version = Rails.version
  @database_adapter = ActiveRecord::Base.connection.adapter_name

  respond_to do |format|
    format.turbo_stream do
      render turbo_stream: turbo_stream.replace("dashboard-stats",
        partial: "solidcrud/admin/shared/dashboard_stats",
        locals: { models_count: @models_count, rails_version: @rails_version, database_adapter: @database_adapter }
      )
    end
  end
end

#destroyObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/solidcrud/admin_controller.rb', line 145

def destroy
  @model_name = params[:model]
  @model_klass = model_class(@model_name)
  @record = @model_klass.find(params[:id])
  @record.destroy

  respond_to do |format|
    format.html { redirect_to solidcrud.admin_model_path(@model_name), notice: 'Deleted!' }
    format.turbo_stream do
      render turbo_stream: [
        turbo_stream.remove("record-#{params[:id]}"),
        turbo_stream.replace("flash-messages", partial: "solidcrud/admin/shared/flash_messages", locals: { notice: 'Deleted!' }),
        turbo_stream.append("notification-container", 
          '<div class="notification-trigger" data-type="success" data-message="Record deleted successfully!" data-auto-dismiss="true" style="display: none;"></div>')
      ]
    end
  end
end

#editObject



111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/solidcrud/admin_controller.rb', line 111

def edit
  @model_name = params[:model]
  @model_klass = model_class(@model_name)
  @record = @model_klass.find(params[:id])

  respond_to do |format|
    format.html
    format.turbo_stream
  end
end

#indexObject



5
6
7
8
9
10
11
# File 'app/controllers/solidcrud/admin_controller.rb', line 5

def index
  @models ||= []
  respond_to do |format|
    format.html
    format.turbo_stream
  end
end

#modelObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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/solidcrud/admin_controller.rb', line 28

def model
  @model_name = params[:model]
  @model_klass = model_class(@model_name)
  
  if @model_klass.nil?
    raise ActiveRecord::RecordNotFound, "Model '#{@model_name}' not found"
  end
  
  @records = @model_klass.all
  
  # Search functionality
  if params[:search].present?
    search_term = "%#{params[:search]}%"
    searchable_columns = @model_klass.column_names.select do |col|
      column_type = @model_klass.columns_hash[col].type
      [:string, :text].include?(column_type)
    end
    
    if searchable_columns.any?
      # Use ILIKE for PostgreSQL, LIKE for others (case-insensitive search)
      operator = ActiveRecord::Base.connection.adapter_name.downcase.include?('postgresql') ? 'ILIKE' : 'LIKE'
      search_conditions = searchable_columns.map { |col| "LOWER(#{col}) LIKE LOWER(?)" }.join(' OR ')
      search_params = [search_term] * searchable_columns.length
      @records = @records.where(search_conditions, *search_params)
    end
  end
  
  # Sort functionality
  if params[:sort].present? && @model_klass.column_names.include?(params[:sort])
    direction = params[:direction] == 'desc' ? 'DESC' : 'ASC'
    @records = @records.order("#{params[:sort]} #{direction}")
  else
    @records = @records.order(id: :desc)
  end
  
  # Pagination
  @page = (params[:page] || 1).to_i
  @per_page = 25
  @total_count = @records.count
  @records = @records.offset((@page - 1) * @per_page).limit(@per_page)
  
  @total_pages = (@total_count.to_f / @per_page).ceil

  respond_to do |format|
    format.html
    format.turbo_stream
  end
end

#newObject



77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/solidcrud/admin_controller.rb', line 77

def new
  @model_name = params[:model]
  @model_klass = model_class(@model_name)
  @record = @model_klass.new

  respond_to do |format|
    format.html
    format.turbo_stream
  end
end

#updateObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/solidcrud/admin_controller.rb', line 122

def update
  @model_name = params[:model]
  @model_klass = model_class(@model_name)
  @record = @model_klass.find(params[:id])

  respond_to do |format|
    if @record.update(record_params)
      format.html { redirect_to solidcrud.admin_model_path(@model_name), notice: 'Updated!' }
      format.turbo_stream do
        render turbo_stream: [
          turbo_stream.replace("flash-messages", partial: "solidcrud/admin/shared/flash_messages", locals: { notice: 'Updated!' }),
          turbo_stream.replace("record-#{@record.id}", partial: "solidcrud/admin/shared/record_row", locals: { record: @record, model_name: @model_name, model_klass: @model_klass }),
          turbo_stream.append("notification-container", 
            '<div class="notification-trigger" data-type="success" data-message="Record updated successfully!" data-auto-dismiss="true" style="display: none;"></div>')
        ]
      end
    else
      format.html { render :edit }
      format.turbo_stream { render turbo_stream: turbo_stream.replace("edit-form", partial: "solidcrud/admin/shared/form", locals: { record: @record, model_name: @model_name, model_klass: @model_klass }) }
    end
  end
end