Module: HasHelpers::Helpers::Application::InstanceMethods
- Defined in:
- lib/has_helpers/helpers/application.rb
Instance Method Summary collapse
- #days_ago(date) ⇒ Object
- #encode(s) ⇒ Object
- #format_account(account, n = account.to_s.length) ⇒ Object
- #format_boolean(boolean) ⇒ Object
- #format_date(date, standard_format: false) ⇒ Object (also: #format_dob)
- #format_datetime(datetime, standard_format: false) ⇒ Object
- #format_delimited(number, **options) ⇒ Object
- #format_dollar(currency, **options) ⇒ Object (also: #format_currency)
-
#format_filename(filename, limit: nil) ⇒ Object
Creates a more human-readable name from the filename.
- #format_html(markdown) ⇒ Object
- #format_human_dollar(currency) ⇒ Object
- #format_interval(interval) ⇒ Object
- #format_label(label) ⇒ Object
- #format_map_url(address) ⇒ Object
- #format_nickname(nickname) ⇒ Object
- #format_percent(percent, **options) ⇒ Object
- #format_phone_number_url(web_dial, extension, phone_number, phone_extension) ⇒ Object
-
#format_short_date(date) ⇒ Object
Examples format_short_date(Date.current) # => "October 2" format_short_date(Date.current + 1.year) # => "October 2, 2015".
-
#format_short_dollar(currency, **options) ⇒ Object
Formats a currency value without a decimal place, i.e.
- #format_ssn(ssn) ⇒ Object
-
#format_tel(number) ⇒ Object
(also: #format_phone)
TODO: use this instead number.to_s(:phone, area_code: true, extension: 555).
- #format_tel_extension(extension) ⇒ Object
- #format_time(time) ⇒ Object
- #format_zip_code(zip_code) ⇒ Object
-
#image_io_to_data_uri(io, content_type) ⇒ Object
Takes an IO wrapping image data and the image content type, and returns a Base64 inline image data URI (a String) for that image like: data:img/png;base64,R0lGODlhyAAiAL...
- #mask(id_to_mask, display_mode = "Masked") ⇒ Object
- #mask_birth_date(birth_date, display_mode = nil) ⇒ Object
- #mask_ssn(ssn, display_mode = "Masked SSN") ⇒ Object
-
#photo_info_service(photo_info, application: Rails.application.class.name.deconstantize.underscore.dasherize, env: ::HasUtils::Env.current_environment, style: :thumbnail) ⇒ Object
Accepts a photo info string formatted as "resource_typish, resource_id, photo_filename, unix_timestamp".
- #valid_mask_or_get_default(ssn_mask) ⇒ Object
Instance Method Details
#days_ago(date) ⇒ Object
62 63 64 |
# File 'lib/has_helpers/helpers/application.rb', line 62 def days_ago(date) Date.current.jd - date.jd end |
#encode(s) ⇒ Object
226 227 228 229 |
# File 'lib/has_helpers/helpers/application.rb', line 226 def encode(s) return nil unless s.class == String s.encode(xml: :text) end |
#format_account(account, n = account.to_s.length) ⇒ Object
16 17 18 |
# File 'lib/has_helpers/helpers/application.rb', line 16 def format_account(account, n = account.to_s.length) account.to_s.split(//).last(4).join.rjust(n, "X") end |
#format_boolean(boolean) ⇒ Object
73 74 75 |
# File 'lib/has_helpers/helpers/application.rb', line 73 def format_boolean(boolean) boolean ? "Yes" : "No" end |
#format_date(date, standard_format: false) ⇒ Object Also known as: format_dob
20 21 22 23 24 25 |
# File 'lib/has_helpers/helpers/application.rb', line 20 def format_date(date, standard_format: false) if date.is_a?(String) date = standard_format ? Date.parse(date) : Chronic.parse(date) end date.strftime("%m/%d/%Y") if date end |
#format_datetime(datetime, standard_format: false) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/has_helpers/helpers/application.rb', line 66 def format_datetime(datetime, standard_format: false) if datetime.is_a?(String) datetime = standard_format ? Time.zone.parse(datetime) : Chronic.parse(datetime) end datetime.strftime("%m/%d/%Y %l:%M %p") if datetime end |
#format_delimited(number, **options) ⇒ Object
77 78 79 80 |
# File 'lib/has_helpers/helpers/application.rb', line 77 def format_delimited(number, **) # TODO: why Rails? number.to_s(:delimited, strip_insignificant_zeros: true, delimiter: ",", separator: ".") ActionController::Base.helpers.number_with_precision(number, .merge(precision: 2, strip_insignificant_zeros: true, delimiter: ",")) end |
#format_dollar(currency, **options) ⇒ Object Also known as: format_currency
93 94 95 96 97 98 99 100 |
# File 'lib/has_helpers/helpers/application.rb', line 93 def format_dollar(currency, **) return nil if currency.blank? ActiveSupport::NumberHelper.number_to_currency( currency.to_d, ) end |
#format_filename(filename, limit: nil) ⇒ Object
Creates a more human-readable name from the filename. It also truncates the name if it is longer than the size limit specified. NOTE: Assumes the filename has already been sanitized to remove control characters.
218 219 220 221 222 223 224 |
# File 'lib/has_helpers/helpers/application.rb', line 218 def format_filename(filename, limit: nil) # Remove the file extension if one is present and titleize att_name = File.basename(filename, ".*").titleize # Truncate if too long (limit && att_name.size > limit) ? (att_name.slice(0, limit - 3) + "...") : att_name end |
#format_html(markdown) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/has_helpers/helpers/application.rb', line 43 def format_html(markdown) return unless markdown render = Redcarpet::Render::HTML.new(filter_html: true, safe_links_only: true) parser = Redcarpet::Markdown.new(render) parser.render(markdown).html_safe # rubocop:disable Rails/OutputSafety end |
#format_human_dollar(currency) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/has_helpers/helpers/application.rb', line 111 def format_human_dollar(currency) return nil if currency.blank? ActiveSupport::NumberHelper.number_to_human( currency.to_d, precision: 3, significant: true, units: { thousand: "Thousand", million: "Million", billion: "Billion" } ) end |
#format_interval(interval) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/has_helpers/helpers/application.rb', line 130 def format_interval(interval) new_interval = interval.is_a?(String) ? ActiveSupport::Duration.parse(interval) : ::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Interval.new.deserialize(interval) parts = [] new_interval.parts.each do |part| unit_of_time = part[1] > 1 ? part[0] : part[0].to_s.singularize parts << "#{part[1]} #{unit_of_time}" end parts.join(" ") end |
#format_label(label) ⇒ Object
209 210 211 212 |
# File 'lib/has_helpers/helpers/application.rb', line 209 def format_label(label) label.dup. sub(/[^\A]percent\z/i, " %") # E.g. "Commission Percent" => "Commission %" end |
#format_map_url(address) ⇒ Object
201 202 203 |
# File 'lib/has_helpers/helpers/application.rb', line 201 def format_map_url(address) "http://maps.google.com?daddr=#{ ::CGI.escape(address.display_address) }" end |
#format_nickname(nickname) ⇒ Object
205 206 207 |
# File 'lib/has_helpers/helpers/application.rb', line 205 def format_nickname(nickname) nickname && %Q["#{nickname}"] end |
#format_percent(percent, **options) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/has_helpers/helpers/application.rb', line 82 def format_percent(percent, **) return nil if percent.blank? formatted = ActiveSupport::NumberHelper.number_to_percentage( percent.to_d, .merge(precision: 6, strip_insignificant_zeros: true) ) formatted.partition(".").last.length == 2 ? formatted.insert(-2, "0") : formatted end |
#format_phone_number_url(web_dial, extension, phone_number, phone_extension) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/has_helpers/helpers/application.rb', line 173 def format_phone_number_url(web_dial, extension, phone_number, phone_extension) if web_dial && extension "<a class=\"value\" href=\"#{Mustache.render(web_dial, extension: extension, phone_number: phone_number)}\"><span>#{phone_number.gsub(/([0-9]{3})([0-9]{3})([0-9]{4})$/, "(\\1) \\2-\\3") if phone_number}</span></a>" elsif phone_number %Q[<span class="value">#{format_tel(phone_number)}#{ " #{format_tel_extension(phone_extension)}" if phone_extension }</span>] end end |
#format_short_date(date) ⇒ Object
Examples
format_short_date(Date.current) # => "October 2" format_short_date(Date.current + 1.year) # => "October 2, 2015"
54 55 56 |
# File 'lib/has_helpers/helpers/application.rb', line 54 def format_short_date(date) date.strftime("%B %-d#{ ", %Y" unless date.year == Date.current.year }") if date end |
#format_short_dollar(currency, **options) ⇒ Object
Formats a currency value without a decimal place, i.e. it omits the cents
Examples
format_short_dollar(2_345_678.90) # => "$2,345,678"
107 108 109 |
# File 'lib/has_helpers/helpers/application.rb', line 107 def format_short_dollar(currency, **) format_dollar(currency, precision: 0, **) end |
#format_ssn(ssn) ⇒ Object
126 127 128 |
# File 'lib/has_helpers/helpers/application.rb', line 126 def format_ssn(ssn) ssn && [ssn[0..2], ssn[3..4], ssn[5..8]].compact.join("-") end |
#format_tel(number) ⇒ Object Also known as: format_phone
TODO: use this instead number.to_s(:phone, area_code: true, extension: 555)
182 183 184 |
# File 'lib/has_helpers/helpers/application.rb', line 182 def format_tel(number) number && number.sub(/([0-9]{3})([0-9]{3})([0-9]{4})$/, "(\\1) \\2-\\3") end |
#format_tel_extension(extension) ⇒ Object
188 189 190 |
# File 'lib/has_helpers/helpers/application.rb', line 188 def format_tel_extension(extension) "x#{extension}" if extension.present? end |
#format_time(time) ⇒ Object
58 59 60 |
# File 'lib/has_helpers/helpers/application.rb', line 58 def format_time(time) time.strftime("%l:%M %p") if time end |
#format_zip_code(zip_code) ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/has_helpers/helpers/application.rb', line 192 def format_zip_code(zip_code) if zip_code.present? && zip_code.length == 9 && zip_code.scan(/^\d+$/).any? # US regex /(?<zip>\d{5})(?<ext>\d{4})?/ =~ zip_code # Name regex groups zip and ext will be set as local variables [zip, ext].reject(&:blank?).join("-") else # other country cases zip_code end end |
#image_io_to_data_uri(io, content_type) ⇒ Object
Takes an IO wrapping image data and the image content type, and returns a Base64 inline image data URI (a String) for that image like:
data:img/png;base64,R0lGODlhyAAiAL...
Note that io doesn't have to be an IO object; it just needs to support a read method that returns the image binary data.
266 267 268 |
# File 'lib/has_helpers/helpers/application.rb', line 266 def image_io_to_data_uri(io, content_type) "data:#{ content_type };base64,#{ Rack::Utils.escape(Base64.strict_encode64(io.read)) }" end |
#mask(id_to_mask, display_mode = "Masked") ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/has_helpers/helpers/application.rb', line 157 def mask(id_to_mask, display_mode = "Masked") case display_mode when "Full" id_to_mask when "Masked" visible_count = (id_to_mask.length * 0.4).ceil masked_part = "*" * (id_to_mask.length - visible_count) visible_part = id_to_mask[-visible_count, visible_count] masked_part + visible_part when "Hide" "*" * id_to_mask.length else "Invalid Option" end end |
#mask_birth_date(birth_date, display_mode = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/has_helpers/helpers/application.rb', line 29 def mask_birth_date(birth_date, display_mode = nil) default = { value: birth_date ? format_dob(birth_date) : nil, disabled: false, hide_age: false } return default unless birth_date case display_mode when "Masked birth date" { value: "**/**/#{birth_date.year}", disabled: true, hide_age: false } when "Hide birth date" { value: "**/**/****", disabled: true, hide_age: true } else default end end |
#mask_ssn(ssn, display_mode = "Masked SSN") ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/has_helpers/helpers/application.rb', line 140 def mask_ssn(ssn, display_mode = "Masked SSN") case display_mode when "Full SSN" format_ssn(ssn) when "Masked SSN" ssn && "***-**-#{ssn[5..8]}" when "Hide SSN" "***-**-****" else "Invalid Option" end end |
#photo_info_service(photo_info, application: Rails.application.class.name.deconstantize.underscore.dasherize, env: ::HasUtils::Env.current_environment, style: :thumbnail) ⇒ Object
Accepts a photo info string formatted as "resource_typish, resource_id, photo_filename, unix_timestamp". Returns a Service to the corresponding photo.
Examples
photo_info_service("carriers,1291,photo.png,1445445346")
Options
application: This may be assigned as a string or a Proc. When a proc is given, it is called and passed
the pieces extracted from the photo info string. The value returned is used as the application name.
photo_info_service("carriers,1291,photo.png,1445445346", application: "agencieshq")
photo_info_service("carriers,1291,photo.png,1445445346", application: proc(resource_typish, id, file_name, unix_timestamp") { resource_typish == "users" ? "hqadmin" : "agencieshq" })
env: The environment name to use when generating the service. Defaults to the current Rails environment.
photo_info_service("carriers,1291,photo.png,1445445346", env: "production")
style: The photo style, e.g. :thumbnail, :original, to request. Defaults to :thumbnail.
photo_info_service("carriers,1291,photo.png,1445445346", style: :original)
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/has_helpers/helpers/application.rb', line 250 def photo_info_service(photo_info, application: Rails.application.class.name.deconstantize.underscore.dasherize, env: ::HasUtils::Env.current_environment, style: :thumbnail) if photo_info organization_id, resource_typish, resource_id, filename, , photo_category = photo_info.split(/\s*,\s*/) photo_category ||= :photos if filename application_name = application.respond_to?(:call) ? application.call(resource_typish, resource_id, filename, ) : application "https://s3.amazonaws.com/#{ application_name }-#{ env }/#{ organization_id }/#{ photo_category }/#{ resource_typish }/#{ resource_id }/#{ style }#{ File.extname(filename) }?#{ }".to_service end end end |
#valid_mask_or_get_default(ssn_mask) ⇒ Object
153 154 155 |
# File 'lib/has_helpers/helpers/application.rb', line 153 def valid_mask_or_get_default(ssn_mask) ssn_mask ? Option.find(ssn_mask).name : "Masked SSN" end |