Module: RailsOnboarding::ResponsiveHelper
- Defined in:
- lib/rails_onboarding/responsive_helper.rb
Overview
Responsive layout helper module Provides helper methods for implementing responsive design features in the host application
Instance Method Summary collapse
-
#image_loading_strategy(eager: false) ⇒ String
Returns appropriate loading attribute for images.
-
#mobile_device? ⇒ Boolean
Checks if the current device is mobile based on user agent.
-
#phone_device? ⇒ Boolean
Checks if the current device is a phone.
-
#rails_onboarding_ios_meta ⇒ String
Apple-specific meta tags for iOS devices.
-
#rails_onboarding_manifest_link(manifest_path = "/manifest.json") ⇒ String
Returns manifest link for PWA support.
-
#rails_onboarding_theme_color(color = nil) ⇒ String
Returns theme color meta tag for mobile browsers.
-
#rails_onboarding_viewport_meta(options = {}) ⇒ String
Returns the viewport meta tag for responsive design This should be included in the host application's layout.
-
#responsive_device_classes ⇒ String
Returns CSS classes for responsive context.
-
#responsive_device_data ⇒ Hash
Returns data attributes for JavaScript device detection.
-
#responsive_image(src, options = {}) ⇒ Object
Helper to render responsive images.
-
#supports_hover? ⇒ Boolean
Checks if the viewport supports hover (desktop/laptop).
-
#tablet_device? ⇒ Boolean
Checks if the current device is a tablet.
-
#touch_friendly_attrs ⇒ Hash
Returns touch-friendly attributes for interactive elements.
Instance Method Details
#image_loading_strategy(eager: false) ⇒ String
Returns appropriate loading attribute for images
196 197 198 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 196 def image_loading_strategy(eager: false) eager ? "eager" : "lazy" end |
#mobile_device? ⇒ Boolean
Checks if the current device is mobile based on user agent
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 61 def mobile_device? return false unless request.present? user_agent = request.user_agent.to_s.downcase mobile_patterns = [ /mobile/, /android/, /iphone/, /ipad/, /ipod/, /blackberry/, /windows phone/, /opera mini/, /kindle/, /silk/ ] mobile_patterns.any? { |pattern| user_agent.match?(pattern) } end |
#phone_device? ⇒ Boolean
Checks if the current device is a phone
96 97 98 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 96 def phone_device? mobile_device? && !tablet_device? end |
#rails_onboarding_ios_meta ⇒ String
Apple-specific meta tags for iOS devices
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 135 def = [] # Enable web app capable mode << tag(:meta, name: "apple-mobile-web-app-capable", content: "yes") # Status bar style << tag(:meta, name: "apple-mobile-web-app-status-bar-style", content: "default") # Disable phone number detection << tag(:meta, name: "format-detection", content: "telephone=no") safe_join(, "\n") end |
#rails_onboarding_manifest_link(manifest_path = "/manifest.json") ⇒ String
Returns manifest link for PWA support
157 158 159 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 157 def rails_onboarding_manifest_link(manifest_path = "/manifest.json") tag(:link, rel: "manifest", href: manifest_path) end |
#rails_onboarding_theme_color(color = nil) ⇒ String
Returns theme color meta tag for mobile browsers
53 54 55 56 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 53 def rails_onboarding_theme_color(color = nil) color ||= "#6366f1" # Default primary color tag(:meta, name: "theme-color", content: color) end |
#rails_onboarding_viewport_meta(options = {}) ⇒ String
Returns the viewport meta tag for responsive design This should be included in the host application's layout
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 25 def ( = {}) width = [:width] || "device-width" initial_scale = [:initial_scale] || 1.0 maximum_scale = [:maximum_scale] || 5.0 minimum_scale = [:minimum_scale] || 1.0 user_scalable = .fetch(:user_scalable, true) ? "yes" : "no" = [:viewport_fit] || "cover" content = [ "width=#{width}", "initial-scale=#{initial_scale}", "maximum-scale=#{maximum_scale}", "minimum-scale=#{minimum_scale}", "user-scalable=#{user_scalable}", "viewport-fit=#{}" ].join(", ") tag(:meta, name: "viewport", content: content) end |
#responsive_device_classes ⇒ String
Returns CSS classes for responsive context
106 107 108 109 110 111 112 113 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 106 def responsive_device_classes classes = [] classes << "is-mobile" if mobile_device? classes << "is-tablet" if tablet_device? classes << "is-phone" if phone_device? classes << "is-desktop" unless mobile_device? classes.join(" ") end |
#responsive_device_data ⇒ Hash
Returns data attributes for JavaScript device detection
121 122 123 124 125 126 127 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 121 def responsive_device_data { "data-mobile" => mobile_device?, "data-tablet" => tablet_device?, "data-phone" => phone_device? } end |
#responsive_image(src, options = {}) ⇒ Object
Helper to render responsive images
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 174 def responsive_image(src, = {}) alt = [:alt] || "" srcset = [:srcset] sizes = [:sizes] css_class = [:class] || "" = { src: src, alt: alt, class: css_class } if srcset.present? srcset_value = srcset.map { |density, path| "#{path} #{density}" }.join(", ") [:srcset] = srcset_value end [:sizes] = sizes if sizes.present? tag(:img, ) end |
#supports_hover? ⇒ Boolean
Checks if the viewport supports hover (desktop/laptop)
Note: This is a server-side estimation; client-side detection is more accurate
204 205 206 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 204 def supports_hover? !mobile_device? end |
#tablet_device? ⇒ Boolean
Checks if the current device is a tablet
84 85 86 87 88 89 90 91 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 84 def tablet_device? return false unless request.present? user_agent = request.user_agent.to_s.downcase tablet_patterns = [ /ipad/, /tablet/, /kindle/, /silk/, /playbook/ ] tablet_patterns.any? { |pattern| user_agent.match?(pattern) } end |
#touch_friendly_attrs ⇒ Hash
Returns touch-friendly attributes for interactive elements
211 212 213 214 215 216 |
# File 'lib/rails_onboarding/responsive_helper.rb', line 211 def touch_friendly_attrs { "data-touch-enabled" => mobile_device?, "style" => mobile_device? ? "touch-action: manipulation;" : nil }.compact end |