Class: BananaDocs::Helpers
- Inherits:
-
Object
- Object
- BananaDocs::Helpers
- Includes:
- ActionView::Context, ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
- Defined in:
- lib/banana_docs/helpers.rb
Constant Summary collapse
- FIXED_COLUMNS =
['Parâmetro', 'Descrição', 'Tipo', 'Formato', 'Observações', 'Obrigatório?']
Instance Method Summary collapse
- #action(title, options) ⇒ Object
- #add_nav_item(title, anchor) ⇒ Object
- #build_span(text, clazz) ⇒ Object
- #code(&block) ⇒ Object
- #empty ⇒ Object
- #encode(string) ⇒ Object
- #include_assets ⇒ Object
-
#initialize ⇒ Helpers
constructor
A new instance of Helpers.
- #key_value(key, value, show_keys = true) ⇒ Object
- #key_with_block(key, &block) ⇒ Object
- #more ⇒ Object
- #note(text) ⇒ Object
- #param(name, description, type, format, options = {}) ⇒ Object
- #params(&block) ⇒ Object
- #render(view) ⇒ Object
- #response(&block) ⇒ Object
- #section(title, options = {}) ⇒ Object
- #show_nav_menu ⇒ Object
- #status_code(status) ⇒ Object
Constructor Details
#initialize ⇒ Helpers
Returns a new instance of Helpers.
10 11 12 |
# File 'lib/banana_docs/helpers.rb', line 10 def initialize @navigations = [] end |
Instance Method Details
#action(title, options) ⇒ Object
28 29 30 31 32 |
# File 'lib/banana_docs/helpers.rb', line 28 def action(title, ) content_tag(:h5, encode(title)) + content_tag(:code, "#{[:method]} #{[:url]}") + content_tag(:br) end |
#add_nav_item(title, anchor) ⇒ Object
69 70 71 |
# File 'lib/banana_docs/helpers.rb', line 69 def add_nav_item(title, anchor) @navigations << { title: title, anchor: "##{anchor}" } end |
#build_span(text, clazz) ⇒ Object
59 60 61 |
# File 'lib/banana_docs/helpers.rb', line 59 def build_span(text, clazz) content_tag(:span, encode(text), class: clazz) end |
#code(&block) ⇒ Object
94 95 96 |
# File 'lib/banana_docs/helpers.rb', line 94 def code(&block) content_tag(:code, yield.html_safe).html_safe end |
#empty ⇒ Object
107 108 109 |
# File 'lib/banana_docs/helpers.rb', line 107 def empty "[]" end |
#encode(string) ⇒ Object
119 120 121 122 |
# File 'lib/banana_docs/helpers.rb', line 119 def encode(string) @coder ||= HTMLEntities.new @coder.encode(string, :named).html_safe end |
#include_assets ⇒ Object
115 116 117 |
# File 'lib/banana_docs/helpers.rb', line 115 def include_assets render 'assets' end |
#key_value(key, value, show_keys = true) ⇒ Object
98 99 100 101 |
# File 'lib/banana_docs/helpers.rb', line 98 def key_value(key, value, show_keys = true) value = value.starts_with?('[') ? encode(value) : "\"#{encode value}\"" (show_keys ? "{ \"#{encode key}\":#{encode value} }" : "\"#{encode key}\": #{encode value}, ").html_safe end |
#key_with_block(key, &block) ⇒ Object
103 104 105 |
# File 'lib/banana_docs/helpers.rb', line 103 def key_with_block(key, &block) "{ \"#{encode key}\": { #{yield} } }".html_safe end |
#more ⇒ Object
111 112 113 |
# File 'lib/banana_docs/helpers.rb', line 111 def more "..." end |
#note(text) ⇒ Object
63 64 65 66 67 |
# File 'lib/banana_docs/helpers.rb', line 63 def note(text) content_tag(:blockquote) do content_tag(:p, encode(text), style: 'font-size: 14px') end end |
#param(name, description, type, format, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/banana_docs/helpers.rb', line 45 def param(name, description, type, format, = {}) obs = [:obs].nil? ? '-' : [:obs] required, clazz = [:required] ? ['Sim', 'badge badge-important'] : ['Não', 'badge badge-info'] content_tag(:tr) do content_tag(:td, build_span(name, 'label label-inverse')) + content_tag(:td, encode(description)) + content_tag(:td, encode(type)) + content_tag(:td, encode(format)) + content_tag(:td, encode(obs)) + content_tag(:td, build_span(required, clazz)) #options... end end |
#params(&block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/banana_docs/helpers.rb', line 34 def params(&block) content_tag(:table, class: 'table table-striped') do content_tag(:thead) do content_tag(:tr) do FIXED_COLUMNS.map{ |th| content_tag(:th, encode(th)) }.reduce(:+) end end + content_tag(:tbody, yield.html_safe) end end |
#render(view) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/banana_docs/helpers.rb', line 14 def render(view) view = view.sub(/(\.slim|)$/, '.slim') banana_views_dir = File.join Gem::Specification.find_by_name('banana_docs').gem_dir, 'views' view = File.join(banana_views_dir, view) unless File.exists?(view) Slim::Template.new(view).render(self).html_safe end |
#response(&block) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/banana_docs/helpers.rb', line 80 def response(&block) content_tag(:div) do content_tag(:span, content_tag(:i, "", class: 'icon-chevron-right').html_safe + 'Exemplos de respostas', class: 'response') + content_tag(:br) + yield.html_safe end end |
#section(title, options = {}) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/banana_docs/helpers.rb', line 21 def section(title, = {}) anchor = title.parameterize add_nav_item(title, anchor) title = content_tag :h3, encode(title) link_to(title, '#', name: anchor) end |
#show_nav_menu ⇒ Object
73 74 75 76 77 78 |
# File 'lib/banana_docs/helpers.rb', line 73 def content_tag(:ul, class: 'nav nav-list') do content_tag(:li, encode('Ações'), class: 'nav-header') + @navigations.map { |nav_item| content_tag(:li, link_to(encode(nav_item[:title]), nav_item[:anchor])).html_safe }.reduce(:+) end end |
#status_code(status) ⇒ Object
88 89 90 91 92 |
# File 'lib/banana_docs/helpers.rb', line 88 def status_code(status) status_codes = { '200' => 'success', '400' => 'important', '404' => 'important' } status = content_tag(:span, status, class: "label label-#{status_codes[status]}") "Status HTTP: #{status} #{content_tag(:br)}".html_safe end |