Module: RailsOnboarding::TemplatesHelper

Defined in:
app/helpers/rails_onboarding/templates_helper.rb

Overview

Helper methods for working with onboarding templates

Instance Method Summary collapse

Instance Method Details

#apply_template(template_key) ⇒ Boolean

Apply a template to configuration

Parameters:

  • template_key (Symbol, String)

    Template identifier

Returns:

  • (Boolean)

    Success status



25
26
27
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 25

def apply_template(template_key)
  RailsOnboarding.configuration.apply_template(template_key)
end

#available_templatesHash

Get all available templates

Returns:

  • (Hash)

    Hash of template_key => template_config



9
10
11
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 9

def available_templates
  RailsOnboarding.configuration.onboarding_templates || {}
end

#compare_templates(*template_keys) ⇒ Hash

Compare templates

Parameters:

  • template_keys (Array<Symbol>)

    Template keys to compare

Returns:

  • (Hash)

    Comparison data



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 124

def compare_templates(*template_keys)
  comparison = {}

  template_keys.each do |key|
    template = get_template(key)
    next unless template

    comparison[key] = {
      name: template[:name],
      total_steps: template[:steps]&.size || 0,
      required_steps: template[:steps]&.count { |s| !s[:skippable] } || 0,
      optional_steps: template[:steps]&.count { |s| s[:skippable] } || 0,
      steps: template[:steps]
    }
  end

  comparison
end

#detect_current_templateSymbol?

Detect which template matches current configuration

Returns:

  • (Symbol, nil)

    Matched template key or nil



111
112
113
114
115
116
117
118
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 111

def detect_current_template
  current_steps = RailsOnboarding.configuration.steps
  return nil if current_steps.empty?

  available_templates.find do |key, template|
    template[:steps] == current_steps
  end&.first
end

#estimate_template_time(template) ⇒ String

Estimate completion time for a template

Parameters:

  • template (Hash)

    Template configuration

Returns:

  • (String)

    Estimated time (e.g., "5-10 minutes")



210
211
212
213
214
215
216
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 210

def estimate_template_time(template)
  steps_count = template[:steps]&.size || 0
  min_time = steps_count * 1 # 1 minute per step minimum
  max_time = steps_count * 3 # 3 minutes per step maximum

  "#{min_time}-#{max_time} minutes"
end

#get_template(template_key) ⇒ Hash?

Get a specific template

Parameters:

  • template_key (Symbol, String)

    Template identifier

Returns:

  • (Hash, nil)

    Template configuration or nil



17
18
19
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 17

def get_template(template_key)
  RailsOnboarding.configuration.template(template_key)
end

Get recommended template based on context

Examples:

recommended_template(industry: :ecommerce, user_count: 1)
# => :ecommerce

Parameters:

  • context (Hash) (defaults to: {})

    Context information (industry, user_count, etc.)

Returns:

  • (Symbol, nil)

    Recommended template key or nil



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
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 37

def recommended_template(context = {})
  templates = available_templates

  # Recommend based on industry if provided
  if context[:industry]
    return context[:industry].to_sym if templates[context[:industry].to_sym]
  end

  # Recommend based on user type
  if context[:user_type]
    case context[:user_type].to_sym
    when :student, :learner
      return :education if templates[:education]
    when :seller, :merchant
      return :marketplace if templates[:marketplace]
    when :business, :company
      return :saas if templates[:saas]
    end
  end

  # Recommend based on team size
  if context[:team_size]
    if context[:team_size] > 10
      return :saas if templates[:saas]
    elsif context[:team_size] == 1
      return :community if templates[:community]
    end
  end

  # Default to first available template
  templates.keys.first
end

#render_template_comparison(*template_keys) ⇒ String

Render template comparison table

Parameters:

  • template_keys (Array<Symbol>)

    Templates to compare

Returns:

  • (String)

    HTML table comparing templates



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 147

def render_template_comparison(*template_keys)
  comparison = compare_templates(*template_keys)
  return "" if comparison.empty?

   :table, class: "template-comparison" do
    concat((:thead) do
      (:tr) do
        concat((:th, "Feature"))
        comparison.each_key do |key|
          concat((:th, comparison[key][:name]))
        end
      end
    end)

    concat((:tbody) do
      # Total steps row
      concat((:tr) do
        concat((:td, "Total Steps"))
        comparison.each_value do |data|
          concat((:td, data[:total_steps]))
        end
      end)

      # Required steps row
      concat((:tr) do
        concat((:td, "Required Steps"))
        comparison.each_value do |data|
          concat((:td, data[:required_steps]))
        end
      end)

      # Optional steps row
      concat((:tr) do
        concat((:td, "Optional Steps"))
        comparison.each_value do |data|
          concat((:td, data[:optional_steps]))
        end
      end)
    end)
  end
end

#template_categories(template) ⇒ Array<Symbol>

Get categories/tags for a template

Parameters:

  • template (Hash)

    Template configuration

Returns:

  • (Array<Symbol>)

    Categories



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 239

def template_categories(template)
  categories = []

  step_names = template[:steps]&.map { |s| s[:name].to_s } || []

  categories << :profile if step_names.any? { |n| n.include?("profile") }
  categories << :team if step_names.any? { |n| n.include?("team") || n.include?("invite") }
  categories << :payment if step_names.any? { |n| n.include?("payment") || n.include?("billing") }
  categories << :content if step_names.any? { |n| n.include?("post") || n.include?("product") }
  categories << :social if step_names.any? { |n| n.include?("connect") || n.include?("friends") }

  categories
end

#template_difficulty(template) ⇒ Symbol

Determine template difficulty

Parameters:

  • template (Hash)

    Template configuration

Returns:

  • (Symbol)

    :easy, :medium, or :hard



222
223
224
225
226
227
228
229
230
231
232
233
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 222

def template_difficulty(template)
  total_steps = template[:steps]&.size || 0
  required_steps = template[:steps]&.count { |s| !s[:skippable] } || 0

  if total_steps <= 3 && required_steps <= 2
    :easy
  elsif total_steps <= 5 && required_steps <= 3
    :medium
  else
    :hard
  end
end

#template_metadata(template_key) ⇒ Hash

Get template metadata

Parameters:

  • template_key (Symbol, String)

    Template identifier

Returns:

  • (Hash)

    Metadata about the template



193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 193

def (template_key)
  template = get_template(template_key)
  return {} unless template

  {
    name: template[:name],
    total_steps: template[:steps]&.size || 0,
    estimated_time: estimate_template_time(template),
    difficulty: template_difficulty(template),
    categories: template_categories(template)
  }
end

#template_option(key, template, selected = false) ⇒ String

Render a single template option

Parameters:

  • key (Symbol)

    Template key

  • template (Hash)

    Template configuration

  • selected (Boolean) (defaults to: false)

    Whether this template is selected

Returns:

  • (String)

    HTML for template option



95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 95

def template_option(key, template, selected = false)
   :div, class: "template-option #{selected ? 'selected' : ''}", data: { template: key } do
    concat((:div, class: "template-icon") do
      template[:icon] || "📋"
    end)
    concat((:div, class: "template-info") do
      concat((:h4, template[:name]))
      concat((:p, template[:description] || "#{key.to_s.titleize} onboarding"))
      concat((:span, "#{template[:steps]&.size || 0} steps", class: "step-count"))
    end)
  end
end

#template_selector(options = {}) ⇒ String

Render template selection UI

Parameters:

  • options (Hash) (defaults to: {})

    Display options

Returns:

  • (String)

    HTML for template selector



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/rails_onboarding/templates_helper.rb', line 74

def template_selector(options = {})
  templates = available_templates
  current_template = options[:current] || detect_current_template

   :div, class: "template-selector" do
    concat((:h3, options[:title] || "Choose a Template"))

    concat((:div, class: "template-options") do
      templates.each do |key, template|
        concat(template_option(key, template, key == current_template))
      end
    end)
  end
end