Module: Jetski::Helpers::ViewHelpers

Included in:
ViewRenderer
Defined in:
lib/jetski/helpers/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#button_to(button_text, url = nil, **html_opts) ⇒ Object

button_to("Click me", class: "cool-btn")



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jetski/helpers/view_helpers.rb', line 24

def button_to(button_text, url = nil, **html_opts)
  if url
    _method = html_opts[:method] || 'POST'
    # Create form with button inside lol
    _form = "<form action='#{url}' method='#{_method}'>"
    _form += "<button type='submit' #{format_html_options(**html_opts)}>#{button_text}</button>"
    _form += "</form>"
    _form
  else
    _button = "<button #{format_html_options(**html_opts)}>#{button_text}</button>"
  end
end

#favicon_tag(url, **html_opts) ⇒ Object



57
58
59
60
# File 'lib/jetski/helpers/view_helpers.rb', line 57

def favicon_tag(url, **html_opts)
  abs_url = process_url(url)
  "<link rel='icon' type='image/x-icon' #{format_html_options(**html_opts)} href='#{abs_url}'>"
end

#format_html_options(**html_opts) ⇒ Object



80
81
82
83
84
85
# File 'lib/jetski/helpers/view_helpers.rb', line 80

def format_html_options(**html_opts)
  html_opts.map do |k, v|
    formatted_key = k.to_s.gsub("_", "-")
    "#{formatted_key}='#{v}'"
  end.join(" ")
end

#image_tag(image_path, **html_opts) ⇒ Object



52
53
54
55
# File 'lib/jetski/helpers/view_helpers.rb', line 52

def image_tag(image_path, **html_opts)
  abs_url = process_url(image_path)
  "<img src='#{abs_url}' #{format_html_options(**html_opts)}></img>"
end

#input_tag(input_name = nil, **html_opts) ⇒ Object



37
38
39
40
# File 'lib/jetski/helpers/view_helpers.rb', line 37

def input_tag(input_name = nil, **html_opts)
  html_opts[:name] ||= input_name
  "<input #{format_html_options(**html_opts)}>"
end

#javascript_include_tag(url, **html_opts) ⇒ Object



67
68
69
70
# File 'lib/jetski/helpers/view_helpers.rb', line 67

def javascript_include_tag(url, **html_opts)
  abs_url = process_url(url)
  "<script src='#{abs_url}' #{format_html_options(**html_opts)}></script>"
end

#javascript_tag(**html_opts) ⇒ Object



72
73
74
# File 'lib/jetski/helpers/view_helpers.rb', line 72

def javascript_tag(**html_opts)
  "<script #{format_html_options(**html_opts)}>#{yield}</script>"
end

#label_tag(text, **html_opts) ⇒ Object



42
43
44
# File 'lib/jetski/helpers/view_helpers.rb', line 42

def label_tag(text, **html_opts)
  "<label #{format_html_options(**html_opts)}>#{text}</label>"
end

link_to "New post", "/posts/new"



18
19
20
21
# File 'lib/jetski/helpers/view_helpers.rb', line 18

def link_to(link_text, url, **html_opts)
  # TODO: Allow passing block to link_to
  "<a href='#{url}' #{format_html_options(**html_opts)}>#{link_text}</a>"
end

#render(partial_path, **locals) ⇒ Object

Expecting a relative path.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/jetski/helpers/view_helpers.rb', line 5

def render(partial_path, **locals)
  path_to_file = File.join(Jetski.app_root, 'app/views', 
    path_to_controller, "_#{partial_path}.html.erb")
  content = File.read(path_to_file)
  bind = binding
  locals.each do |k, v| 
    bind.local_variable_set k, v
  end
  template = ERB.new(content)
  template.result(bind)
end

#stylesheet_tag(url, **html_opts) ⇒ Object



62
63
64
65
# File 'lib/jetski/helpers/view_helpers.rb', line 62

def stylesheet_tag(url, **html_opts)
  abs_url = process_url(url)
  "<link rel='stylesheet' #{format_html_options(**html_opts)} href='#{abs_url}'>"
end

#textarea_tag(name = nil, value = nil, **html_opts) ⇒ Object



46
47
48
49
50
# File 'lib/jetski/helpers/view_helpers.rb', line 46

def textarea_tag(name = nil, value = nil, **html_opts)
  html_opts[:name] ||= name
  value ||= html_opts[:value]
  "<textarea #{format_html_options(**html_opts.except(:value))}>#{value}</textarea>"
end

#truncate(text, limit) ⇒ Object



76
77
78
# File 'lib/jetski/helpers/view_helpers.rb', line 76

def truncate(text, limit)
  text[0..limit]
end