Class: Tiler::UserWidgetsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/tiler/user_widgets_controller.rb

Overview

CRUD for runtime user-defined (Liquid) widgets. Lives under /settings/user_widgets so it’s discoverable from the Settings page.

Instance Method Summary collapse

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
# File 'app/controllers/tiler/user_widgets_controller.rb', line 17

def create
  @user_widget = UserWidget.new(user_widget_params)
  if @user_widget.save
    redirect_to user_widgets_path, notice: "Custom widget '#{@user_widget.slug}' created."
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



36
37
38
39
40
# File 'app/controllers/tiler/user_widgets_controller.rb', line 36

def destroy
  slug = @user_widget.slug
  @user_widget.destroy!
  redirect_to user_widgets_path, notice: "Custom widget '#{slug}' deleted."
end

#editObject



26
# File 'app/controllers/tiler/user_widgets_controller.rb', line 26

def edit; end

#indexObject



8
9
10
# File 'app/controllers/tiler/user_widgets_controller.rb', line 8

def index
  @user_widgets = UserWidget.order(:slug)
end

#newObject



12
13
14
15
# File 'app/controllers/tiler/user_widgets_controller.rb', line 12

def new
  @user_widget = UserWidget.new(default_w: 4, default_h: 3,
                                template: default_template_seed)
end

#previewObject

POST /settings/user_widgets/preview Body: { template, sample_data } — returns the rendered HTML so the form can show a live preview without saving.



45
46
47
48
49
50
51
52
# File 'app/controllers/tiler/user_widgets_controller.rb', line 45

def preview
  template = params[:template].to_s.first(UserWidget::TEMPLATE_MAX)
  sample   = JSON.parse(params[:sample_data].to_s.presence || "{}") rescue {}
  tpl = ::Liquid::Template.parse(template, error_mode: :strict)
  render plain: tpl.render!("data" => sample, "panel" => { "title" => "Preview" }, "config" => {})
rescue ::Liquid::Error => e
  render plain: "Liquid error: #{e.message}", status: :unprocessable_entity
end

#updateObject



28
29
30
31
32
33
34
# File 'app/controllers/tiler/user_widgets_controller.rb', line 28

def update
  if @user_widget.update(user_widget_params)
    redirect_to user_widgets_path, notice: "Custom widget '#{@user_widget.slug}' updated."
  else
    render :edit, status: :unprocessable_entity
  end
end