Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 29 additions & 41 deletions app/models/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,51 +358,14 @@ def touchpoints_js_string
end

# Always use ERB template rendering for now to avoid Rust compilation issues
controller = ApplicationController.new

# Set up a mock request with default URL options to avoid "undefined method 'host' for nil" errors
# This is necessary because the ERB templates use root_url which requires request context
# Try action_controller first, fall back to action_mailer if not set
default_options = Rails.application.config.action_controller.default_url_options ||
Rails.application.config.action_mailer.default_url_options ||
{}
host = default_options[:host] || 'localhost'
port = default_options[:port] || 3000
protocol = default_options[:protocol] || (port == 443 ? 'https' : 'http')

# Create a mock request
mock_request = ActionDispatch::Request.new(
'rack.url_scheme' => protocol,
'HTTP_HOST' => "#{host}#{":#{port}" if port != 80 && port != 443}",
'SERVER_NAME' => host,
'SERVER_PORT' => port.to_s,
)

controller.request = mock_request
controller.render_to_string(partial: 'components/widget/fba', formats: :js, locals: { form: self })
controller_with_request = build_controller_with_mock_request
controller_with_request.render_to_string(partial: 'components/widget/fba', formats: :js, locals: { form: self })
end

# Renders the widget CSS partial for use with the Rust widget renderer
def render_widget_css
controller = ApplicationController.new

# Set up a mock request with default URL options
default_options = Rails.application.config.action_controller.default_url_options ||
Rails.application.config.action_mailer.default_url_options ||
{}
host = default_options[:host] || 'localhost'
port = default_options[:port] || 3000
protocol = default_options[:protocol] || (port == 443 ? 'https' : 'http')

mock_request = ActionDispatch::Request.new(
'rack.url_scheme' => protocol,
'HTTP_HOST' => "#{host}#{":#{port}" if port != 80 && port != 443}",
'SERVER_NAME' => host,
'SERVER_PORT' => port.to_s,
)

controller.request = mock_request
controller.render_to_string(partial: 'components/widget/widget', formats: :css, locals: { form: self })
controller_with_request = build_controller_with_mock_request
controller_with_request.render_to_string(partial: 'components/widget/widget', formats: :css, locals: { form: self })
end

def reportable_submissions(start_date: nil, end_date: nil)
Expand Down Expand Up @@ -1076,6 +1039,31 @@ def self.forms_whose_retention_period_has_passed

private

# Builds an ApplicationController instance with a mock request for rendering partials
# This is necessary because ERB templates use URL helpers which require request context
def build_controller_with_mock_request
controller = ApplicationController.new

# Set up a mock request with default URL options
# Try action_controller first, fall back to action_mailer if not set
default_options = Rails.application.config.action_controller.default_url_options ||
Rails.application.config.action_mailer.default_url_options ||
{}
host = default_options[:host] || 'localhost'
port = default_options[:port] || 3000
protocol = default_options[:protocol] || (port == 443 ? 'https' : 'http')

mock_request = ActionDispatch::Request.new(
'rack.url_scheme' => protocol,
'HTTP_HOST' => "#{host}#{":#{port}" if port != 80 && port != 443}",
'SERVER_NAME' => host,
'SERVER_PORT' => port.to_s,
)

controller.request = mock_request
controller
end

def set_uuid
self.uuid ||= SecureRandom.uuid
self.short_uuid ||= self.uuid[0..7]
Expand Down
2 changes: 0 additions & 2 deletions ext/widget_renderer/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def ensure_rust

puts "Current directory: #{Dir.pwd}"
puts "Using cargo executable: #{cargo_bin}"
puts "Cleaning previous build artifacts..."
system("#{cargo_bin} clean 2>&1")
puts "Running cargo build --release..."
system("#{cargo_bin} build --release 2>&1") or abort 'Failed to build Rust extension'

Expand Down
3 changes: 2 additions & 1 deletion ext/widget_renderer/src/template_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,8 @@ window.touchpointForm{uuid}.init(touchpointFormOptions{uuid});
}}
}}
// Ensure the custom button is also initialized if it exists (for 'custom-button-modal' delivery method)
const customButtonEl = document.getElementById('{element_selector}');
const customButtonSelector = '{element_selector}';
const customButtonEl = (customButtonSelector && customButtonSelector.length > 0) ? document.getElementById(customButtonSelector) : null;
if (customButtonEl && ('{delivery_method}' === 'custom-button-modal')) {{
if (fbaUswds.Modal) {{
fbaUswds.Modal.on(customButtonEl);
Expand Down