Class: RailsOnboarding::TemplatesController

Inherits:
ApplicationController show all
Includes:
AdminAuthorization
Defined in:
app/controllers/rails_onboarding/templates_controller.rb

Overview

Controller for managing onboarding templates Provides pre-built flows for common use cases

Instance Method Summary collapse

Instance Method Details

#applyObject

POST /templates/:template_key/apply Apply a template to the current configuration



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/rails_onboarding/templates_controller.rb', line 54

def apply
  template_key = params[:template_key] || params[:id]
  template = RailsOnboarding.configuration.template(template_key)

  unless template
    respond_to do |format|
      format.html do
        flash[:error] = "Template not found: #{template_key}"
        redirect_to templates_path
      end
      format.json { render json: { error: "Template not found" }, status: :not_found }
    end
    return
  end

  if RailsOnboarding.configuration.apply_template(template_key)
    respond_to do |format|
      format.html do
        flash[:success] = "Template '#{template[:name]}' has been applied successfully!"
        redirect_to onboarding_path
      end
      format.json do
        render json: {
          success: true,
          template_key: template_key,
          steps: RailsOnboarding.configuration.steps
        }
      end
    end
  else
    respond_to do |format|
      format.html do
        flash[:error] = "Failed to apply template"
        redirect_to templates_path
      end
      format.json { render json: { success: false }, status: :unprocessable_entity }
    end
  end
end

#compareObject

GET /templates/compare Compare multiple templates side by side



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/rails_onboarding/templates_controller.rb', line 120

def compare
  template_keys = params[:templates]&.split(",") || []
  @templates_to_compare = {}

  template_keys.each do |key|
    template = RailsOnboarding.configuration.template(key.to_sym)
    @templates_to_compare[key.to_sym] = template if template
  end

  respond_to do |format|
    format.html
    format.json { render json: @templates_to_compare }
  end
end

#create_customObject

POST /templates/custom Create a custom template based on current configuration



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/rails_onboarding/templates_controller.rb', line 137

def create_custom
  template_name = params[:template_name] || "Custom Template"
  template_key = params[:template_key]&.to_sym || :custom

  custom_template = {
    name: template_name,
    steps: RailsOnboarding.configuration.steps,
    description: params[:description] || "Custom onboarding flow",
    created_at: Time.current
  }

  # In a real application, you'd save this to the database
  # For now, we'll just return it as JSON

  respond_to do |format|
    format.json do
      render json: {
        success: true,
        template_key: template_key,
        template: custom_template
      }
    end
  end
end

#indexObject

GET /templates List all available onboarding templates



20
21
22
23
24
25
26
27
# File 'app/controllers/rails_onboarding/templates_controller.rb', line 20

def index
  @templates = RailsOnboarding.configuration.onboarding_templates

  respond_to do |format|
    format.html
    format.json { render json: @templates }
  end
end

#previewObject

POST /templates/:template_key/preview Preview what a template would look like without applying it



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/rails_onboarding/templates_controller.rb', line 96

def preview
  template_key = params[:template_key] || params[:id]
  template = RailsOnboarding.configuration.template(template_key)

  unless template
    respond_to do |format|
      format.json { render json: { error: "Template not found" }, status: :not_found }
    end
    return
  end

  # Return template details without actually applying it
  render json: {
    template_key: template_key,
    name: template[:name],
    steps: template[:steps],
    total_steps: template[:steps]&.size || 0,
    description: template[:description],
    suitable_for: template[:suitable_for]
  }
end

#showObject

GET /templates/:template_key Show details for a specific template



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/rails_onboarding/templates_controller.rb', line 31

def show
  @template_key = params[:template_key] || params[:id]
  @template = RailsOnboarding.configuration.template(@template_key)

  unless @template
    respond_to do |format|
      format.html do
        flash[:error] = "Template not found: #{@template_key}"
        redirect_to templates_path
      end
      format.json { render json: { error: "Template not found" }, status: :not_found }
    end
    return
  end

  respond_to do |format|
    format.html
    format.json { render json: { template_key: @template_key, template: @template } }
  end
end