module Zhong::WebHelpers
Most of the following helpers are copied from a previous version of the Sidekiq project available here: github.com/mperham/sidekiq/blob/2c9f7662fcdcb52d59b72ba0fe7dc5f963de4904/lib/sidekiq/web_helpers.rb
Public Instance Methods
capture(&block)
click to toggle source
Simple capture method for erb templates. The origin was capture method from sinatra-contrib library.
# File lib/zhong/web_helpers.rb, line 10 def capture(&block) block.call eval("", block.binding) end
csrf_tag()
click to toggle source
# File lib/zhong/web_helpers.rb, line 41 def csrf_tag "<input type='hidden' name='authenticity_token' value='#{session[:csrf]}'/>" end
current_path()
click to toggle source
# File lib/zhong/web_helpers.rb, line 19 def current_path @current_path ||= request.path_info.gsub(%r(^\/),"") end
display_args(args, truncate_after_chars = 2000)
click to toggle source
# File lib/zhong/web_helpers.rb, line 35 def display_args(args, truncate_after_chars = 2000) args.map do |arg| h(truncate(to_display(arg), truncate_after_chars)) end.join(", ") end
environment_title_prefix()
click to toggle source
# File lib/zhong/web_helpers.rb, line 76 def environment_title_prefix environment = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" "[#{environment.upcase}] " unless environment == "production" end
h(text)
click to toggle source
# File lib/zhong/web_helpers.rb, line 68 def h(text) ::Rack::Utils.escape_html(text) rescue ArgumentError => e raise unless e.message.eql?("invalid byte sequence in UTF-8") text.encode!("UTF-16", "UTF-8", invalid: :replace, replace: "").encode!("UTF-8", "UTF-16") retry end
number_with_delimiter(number)
click to toggle source
# File lib/zhong/web_helpers.rb, line 55 def number_with_delimiter(number) begin Float(number) rescue ArgumentError, TypeError return number end options = {delimiter: ",", separator: "."} parts = number.to_s.to_str.split(".") parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}") parts.join(options[:separator]) end
product_version()
click to toggle source
# File lib/zhong/web_helpers.rb, line 82 def product_version "Zhong v#{Zhong::VERSION}" end
relative_time(time)
click to toggle source
# File lib/zhong/web_helpers.rb, line 23 def relative_time(time) if time %(<time datetime="#{time.getutc.iso8601}">#{time}</time>) else "never" end end
root_path()
click to toggle source
# File lib/zhong/web_helpers.rb, line 15 def root_path "#{env['SCRIPT_NAME']}/" end
to_display(arg)
click to toggle source
# File lib/zhong/web_helpers.rb, line 45 def to_display(arg) arg.inspect rescue begin arg.to_s rescue => ex "Cannot display argument: [#{ex.class.name}] #{ex.message}" end end
truncate(text, truncate_after_chars = 2000)
click to toggle source
# File lib/zhong/web_helpers.rb, line 31 def truncate(text, truncate_after_chars = 2000) truncate_after_chars && text.size > truncate_after_chars ? "#{text[0..truncate_after_chars]}..." : text end