Module: RubyNative::Helper

Defined in:
lib/ruby_native/helper.rb

Defined Under Namespace

Classes: NavbarBuilder, NavbarMenuBuilder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resolve_icon(icon: nil, icons: nil, platform: nil) ⇒ Object

Picks the right icon name for the current native platform. Accepts the single ‘icon:` form (applied to every platform) and/or the `icons:` hash form (`{ ios: “…”, android: “…” }`). When both are given, a matching `icons` wins; otherwise falls back to `icon`. Returns nil when nothing resolves.



93
94
95
96
97
98
99
100
# File 'lib/ruby_native/helper.rb', line 93

def self.resolve_icon(icon: nil, icons: nil, platform: nil)
  if icons.is_a?(Hash) && platform
    key = platform.to_sym
    per_platform = icons[key] || icons[key.to_s]
    return per_platform if per_platform
  end
  icon
end

Instance Method Details

#native_back_button_tag(text = nil, **options) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ruby_native/helper.rb', line 29

def native_back_button_tag(text = nil, **options)
  options[:class] = [options[:class], "native-back-button"].compact.join(" ")
  default_content = tag.svg(
    tag.path(d: "M15.75 19.5L8.25 12l7.5-7.5", stroke_linecap: "round", stroke_linejoin: "round"),
    width: 24, height: 24, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", stroke_width: 2.5
  )
  tag.button(text || default_content, onclick: "RubyNative.postMessage({action: 'back'})", **options)
end

#native_badge_tag(count = nil, home: nil, tab: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_native/helper.rb', line 38

def native_badge_tag(count = nil, home: nil, tab: nil)
  home = count if count && home.nil?
  tab = count if count && tab.nil?

  data = { native_badge: "" }
  data[:native_badge_home] = home unless home.nil?
  data[:native_badge_tab] = tab unless tab.nil?

  tag.div(data: data, hidden: true)
end

#native_fab_tag(icon: nil, icons: nil, href: nil, click: nil) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
# File 'lib/ruby_native/helper.rb', line 56

def native_fab_tag(icon: nil, icons: nil, href: nil, click: nil)
  resolved = RubyNative::Helper.resolve_icon(icon: icon, icons: icons, platform: try(:native_platform))
  raise ArgumentError, "native_fab_tag requires an icon" if resolved.nil?
  data = { native_fab: true, native_icon: resolved }
  data[:native_href] = href if href
  data[:native_click] = click if click
  tag.div(data: data, hidden: true)
end

#native_form_tagObject



21
22
23
# File 'lib/ruby_native/helper.rb', line 21

def native_form_tag
  tag.div(data: { native_form: true }, hidden: true)
end

#native_haptic_data(feedback = :success, **data) ⇒ Object



69
70
71
72
73
74
# File 'lib/ruby_native/helper.rb', line 69

def native_haptic_data(feedback = :success, **data)
  feedback = feedback.to_s
  feedback = "success" if feedback.empty?
  data[:native_haptic] = feedback
  data
end

#native_navbar_tag(title = nil, &block) ⇒ Object



49
50
51
52
53
54
# File 'lib/ruby_native/helper.rb', line 49

def native_navbar_tag(title = nil, &block)
  builder = NavbarBuilder.new(self)
  capture(builder, &block) if block

  tag.div(data: { native_navbar: title.to_s }, hidden: true) { builder.to_html }
end

#native_overscroll_tag(top:, bottom: nil) ⇒ Object



65
66
67
# File 'lib/ruby_native/helper.rb', line 65

def native_overscroll_tag(top:, bottom: nil)
  tag.div(data: { native_overscroll_top: top, native_overscroll_bottom: bottom || top }, hidden: true)
end

#native_push_tagObject



25
26
27
# File 'lib/ruby_native/helper.rb', line 25

def native_push_tag
  tag.div(data: { native_push: true }, hidden: true)
end

#native_review_tagObject

Renders a signal element that asks the app to request an App Store rating from the user. The system decides whether to actually show the prompt (Apple throttles it to a few times per year), so it is safe to render this on any page where a review would be welcome, like a confirmation screen after the user finishes something worthwhile.

See Apple’s docs on requesting App Store reviews: developer.apple.com/documentation/storekit/requesting-app-store-reviews



84
85
86
# File 'lib/ruby_native/helper.rb', line 84

def native_review_tag
  tag.div(data: { native_review: true }, hidden: true)
end

#native_tabs_tag(enabled: true) ⇒ Object



16
17
18
19
# File 'lib/ruby_native/helper.rb', line 16

def native_tabs_tag(enabled: true)
  return "".html_safe unless enabled
  tag.div(data: { native_tabs: true }, hidden: true)
end

#ruby_native_screenshot_session?Boolean

True when the current request is part of a Ruby Native screenshot run. Use this to render deterministically: freeze relative timestamps, hide push banners, suppress ads, disable A/B variants, skip notifications.

<% if ruby_native_screenshot_session? %>
  Stamped 2 days ago
<% else %>
  <%= time_ago_in_words(stamp.created_at) %>
<% end %>

Returns:

  • (Boolean)


12
13
14
# File 'lib/ruby_native/helper.rb', line 12

def ruby_native_screenshot_session?
  cookies[:_ruby_native_screenshot_session] == "1"
end