Module: EmberCliDeployRedis::WebHelpers
- Defined in:
- lib/ember_cli_deploy_redis/web_helpers.rb
Overview
This is not a public API
Instance Method Summary collapse
-
#add_to_head(&block) ⇒ Object
This view helper provide ability display you html code in to head of page.
- #application_path(application) ⇒ Object
-
#capture(&block) ⇒ Object
Simple capture method for erb templates.
- #csrf_tag ⇒ Object
- #css_class_name(text) ⇒ Object
- #display_custom_head ⇒ Object
- #find_locale_files(lang) ⇒ Object
- #get_locale ⇒ Object
- #h(text) ⇒ Object
-
#locale ⇒ Object
Given a browser request Accept-Language header like “fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2”, this function will return “fr” since that’s the first code with a matching locale in web/locales.
- #locale_files ⇒ Object
- #location ⇒ Object
- #namespace ⇒ Object
- #product_version ⇒ Object
-
#redirect_with_query(url) ⇒ Object
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
- #redis_connection ⇒ Object
- #redis_connection_and_namespace ⇒ Object
- #redis_info ⇒ Object
- #relative_time(time) ⇒ Object
- #revision_activate_path(revision) ⇒ Object
- #revision_specifier_query_param ⇒ Object
- #revision_test_url(revision) ⇒ Object
- #root_path ⇒ Object
- #strings(lang) ⇒ Object
- #t(msg, options = {}) ⇒ Object
- #u(text) ⇒ Object
Instance Method Details
#add_to_head(&block) ⇒ Object
This view helper provide ability display you html code in to head of page. Example:
<% add_to_head do %>
<link rel="stylesheet" .../>
< .../>
<% end %>
38 39 40 41 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 38 def add_to_head(&block) @head_html ||= [] @head_html << block if block_given? end |
#application_path(application) ⇒ Object
166 167 168 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 166 def application_path(application) "applications/#{u application.name}" end |
#capture(&block) ⇒ Object
Simple capture method for erb templates. The origin was capture method from sinatra-contrib library.
50 51 52 53 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 50 def capture(&block) block.call eval('', block.binding) end |
#csrf_tag ⇒ Object
119 120 121 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 119 def csrf_tag "<input type='hidden' name='authenticity_token' value='#{session[:csrf]}'/>" end |
#css_class_name(text) ⇒ Object
178 179 180 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 178 def css_class_name(text) text.gsub(/\W+/, '-') end |
#display_custom_head ⇒ Object
43 44 45 46 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 43 def display_custom_head return unless defined?(@head_html) @head_html.map { |block| capture(&block) }.join end |
#find_locale_files(lang) ⇒ Object
26 27 28 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 26 def find_locale_files(lang) locale_files.select { |file| file =~ /\/#{lang}\.yml$/ } end |
#get_locale ⇒ Object
72 73 74 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 72 def get_locale strings(locale) end |
#h(text) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 123 def h(text) ::Rack::Utils.escape_html(text) rescue ArgumentError => e raise unless e..eql?('invalid byte sequence in UTF-8') text.encode!('UTF-16', 'UTF-8', invalid: :replace, replace: '').encode!('UTF-8', 'UTF-16') retry end |
#locale ⇒ Object
Given a browser request Accept-Language header like “fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2”, this function will return “fr” since that’s the first code with a matching locale in web/locales
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 59 def locale @locale ||= begin locale = 'en'.freeze languages = request.env['HTTP_ACCEPT_LANGUAGE'.freeze] || 'en'.freeze languages.downcase.split(','.freeze).each do |lang| next if lang == '*'.freeze lang = lang.split(';'.freeze)[0] break locale = lang if find_locale_files(lang).any? end locale end end |
#locale_files ⇒ Object
20 21 22 23 24 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 20 def locale_files @@locale_files = settings.locales.flat_map do |path| Dir["#{path}/*.yml"] end end |
#location ⇒ Object
85 86 87 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 85 def location EmberCliDeployRedis.configuration.redis do |conn| conn.client.location end end |
#namespace ⇒ Object
93 94 95 96 97 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 93 def namespace @@ns ||= EmberCliDeployRedis.configuration.redis do |conn| conn.respond_to?(:namespace) ? conn.namespace : nil end end |
#product_version ⇒ Object
151 152 153 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 151 def product_version "ember_cli_deploy_redis-ruby v#{EmberCliDeployRedis::VERSION}" end |
#redirect_with_query(url) ⇒ Object
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
141 142 143 144 145 146 147 148 149 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 141 def redirect_with_query(url) r = request.referer if r && r =~ /\?/ ref = URI(r) redirect("#{url}?#{ref.query}") else redirect url end end |
#redis_connection ⇒ Object
89 90 91 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 89 def redis_connection EmberCliDeployRedis.configuration.redis do |conn| conn.client.id end end |
#redis_connection_and_namespace ⇒ Object
155 156 157 158 159 160 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 155 def redis_connection_and_namespace @redis_connection_and_namespace ||= begin namespace_suffix = namespace == nil ? '' : "##{namespace}" "#{redis_connection}#{namespace_suffix}" end end |
#redis_info ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 99 def redis_info EmberCliDeployRedis.configuration.redis do |conn| # admin commands can't go through redis-namespace starting # in redis-namespace 2.0 if conn.respond_to?(:namespace) conn.redis.info else conn.info end end end |
#relative_time(time) ⇒ Object
115 116 117 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 115 def relative_time(time) %{<time datetime="#{time.getutc.iso8601}">#{time}</time>} end |
#revision_activate_path(revision) ⇒ Object
174 175 176 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 174 def revision_activate_path(revision) "applications/#{u revision.application.name}/activate/#{u revision.name}" end |
#revision_specifier_query_param ⇒ Object
162 163 164 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 162 def revision_specifier_query_param EmberCliDeployRedis.configuration.revision_specifier_query_param end |
#revision_test_url(revision) ⇒ Object
170 171 172 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 170 def revision_test_url(revision) "/?#{u revision_specifier_query_param}[#{u revision.application.name}]=#{u revision.name}" end |
#root_path ⇒ Object
111 112 113 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 111 def root_path "#{env['SCRIPT_NAME']}/" end |
#strings(lang) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 6 def strings(lang) @@strings ||= {} @@strings[lang] ||= begin # Allow sidekiq-web extensions to add locale paths # so extensions can be localized settings.locales.each_with_object({}) do |path, global| find_locale_files(lang).each do |file| strs = YAML.load(File.open(file)) global.merge!(strs[lang]) end end end end |
#t(msg, options = {}) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 76 def t(msg, ={}) string = get_locale[msg] || msg if .empty? string else string % end end |
#u(text) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/ember_cli_deploy_redis/web_helpers.rb', line 131 def u(text) ::Rack::Utils.escape(text) rescue ArgumentError => e raise unless e..eql?('invalid byte sequence in UTF-8') text.encode!('UTF-16', 'UTF-8', invalid: :replace, replace: '').encode!('UTF-8', 'UTF-16') retry end |