Module: RailsOnboarding::LazyLoading

Extended by:
ActiveSupport::Concern
Defined in:
lib/rails_onboarding/lazy_loading.rb

Overview

Lazy Loading module for performance optimization

This module provides helpers for lazy loading onboarding components to improve initial page load times and reduce unnecessary processing

Instance Method Summary collapse

Instance Method Details

#lazy_current_stepHash?

Lazy load current step data only when needed

Returns:

  • (Hash, nil)

    Step data



69
70
71
72
73
74
75
76
77
# File 'lib/rails_onboarding/lazy_loading.rb', line 69

def lazy_current_step
  return @lazy_current_step if defined?(@lazy_current_step)

  @lazy_current_step = if lazy_load_enabled?
    current_onboarding_step
  else
    nil
  end
end

#lazy_load_enabled?Boolean

Check if lazy loading should be enabled for this user based on both the enabled flag and user count threshold

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
# File 'lib/rails_onboarding/lazy_loading.rb', line 130

def lazy_load_enabled?
  return false unless self.class.lazy_loading_enabled

  # Use ActiveRecord's count with uncached query to ensure fresh count
  # This works better with Rails test transactions
  count = self.class.uncached { self.class.count }

  count < self.class.lazy_load_threshold
end

#lazy_milestones_availableArray<Hash>

Lazy load available milestones

Returns:

  • (Array<Hash>)

    Available milestones



95
96
97
98
99
100
101
102
103
# File 'lib/rails_onboarding/lazy_loading.rb', line 95

def lazy_milestones_available
  return @lazy_milestones_available if defined?(@lazy_milestones_available)

  @lazy_milestones_available = if lazy_load_enabled? && RailsOnboarding.configuration.enable_milestones
    milestones_available
  else
    []
  end
end

#lazy_next_stepHash?

Lazy load next step data only when needed

Returns:

  • (Hash, nil)

    Next step data



82
83
84
85
86
87
88
89
90
# File 'lib/rails_onboarding/lazy_loading.rb', line 82

def lazy_next_step
  return @lazy_next_step if defined?(@lazy_next_step)

  @lazy_next_step = if lazy_load_enabled?
    next_onboarding_step
  else
    nil
  end
end

#lazy_tooltips_for_page(page_context) ⇒ Hash

Lazy load tooltips only when on a page that needs them

Parameters:

  • page_context (String)

    Current page or controller action

Returns:

  • (Hash)

    Available tooltips for current page



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rails_onboarding/lazy_loading.rb', line 109

def lazy_tooltips_for_page(page_context)
  return {} unless lazy_load_enabled?
  return {} unless RailsOnboarding.configuration.enable_tooltips

  cache_key = "rails_onboarding:user:#{id}:tooltips:#{page_context}"
  Rails.cache.fetch(cache_key, expires_in: 600) do
    tooltips = {}
    RailsOnboarding.configuration.feature_tooltips.each do |feature, config|
      # Only include tooltips relevant to current page
      if feature.include?(page_context) && show_feature_tooltip?(feature)
        tooltips[feature] = config
      end
    end
    tooltips
  end
end

#preload_onboarding_dataHash

Preload onboarding data for this user Call this in controller before rendering to avoid N+1

Returns:

  • (Hash)

    Preloaded data



144
145
146
147
148
149
150
151
152
153
# File 'lib/rails_onboarding/lazy_loading.rb', line 144

def preload_onboarding_data
  {
    needs_onboarding: needs_onboarding?,
    current_step: current_onboarding_step,
    next_step: next_onboarding_step,
    progress: onboarding_progress,
    milestones: RailsOnboarding.configuration.enable_milestones ? achieved_milestones : [],
    milestone_points: RailsOnboarding.configuration.enable_milestones ? total_milestone_points : 0
  }
end