Class: CustomTable::SettingsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/custom_table/settings_controller.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject

PATCH/PUT settings



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/custom_table/settings_controller.rb', line 54

def destroy

  model = params[:id].constantize
  variant = params[:variant].presence

  defs = helpers.custom_table_fields_definition_for(model)
  variants = helpers.custom_table_variants_for(model)

  if !variant.nil? && !variants.include?(variant)
    render status: 422, html: "No such variant: #{variant}"
    return
  end

  if current_user.destroy_custom_table_settings(model, variant)
    flash[:notice] = t("custom_table.customization_saved")
    respond_to do |format|
      format.html { render nil, status: :ok }
      format.turbo_stream {render turbo_stream: turbo_stream.action(:refresh, nil)}
    end
  else
    # optional_redirect_to main_app.root_path, alert: t("custom_table.cannot_save_customization")
  end

end

#editObject



9
10
11
12
13
14
15
16
# File 'app/controllers/custom_table/settings_controller.rb', line 9

def edit
  @search_model = params[:id].constantize
  @variant = params[:variant].presence
  if !@variant.nil? && !helpers.custom_table_variants_for(@search_model).include?(@variant)
    render status: 404, html: "No such variant: #{@variant}"
    return
  end
end

#updateObject

PATCH/PUT settings



19
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
47
48
49
50
51
# File 'app/controllers/custom_table/settings_controller.rb', line 19

def update

  p = settings_params.to_h

  model = p[:model].constantize
  variant = p[:variant].presence

  defs = helpers.custom_table_fields_definition_for(model, variant)
  variants = helpers.custom_table_variants_for(model)

  if !variant.nil? && !variants.include?(variant)
    render status: 422, html: "No such variant: #{variant}"
    return
  end

  if defs.nil?
    optional_redirect_to profile_path, alert: t("custom_table.customization_not_allowed", model: model)
  end

  p[:fields].reject!{|k,v| defs[k.to_sym].nil?} # Clearing unknown fields
  p[:fields].each { |k, v| p[:fields][k] = (defs[k.to_sym][:appear] == :always) ? true : ActiveModel::Type::Boolean.new.cast(v) } 

  if current_user.save_custom_table_settings(model, variant, fields: p[:fields])
    flash[:notice] = t("custom_table.customization_saved")
    respond_to do |format|
      format.html { render nil, status: :ok }
      format.turbo_stream {render turbo_stream: turbo_stream.action(:refresh, nil)}
    end
  else
    # optional_redirect_to main_app.root_path, alert: t("custom_table.cannot_save_customization")
  end

end