WebUtils
is basically a collection of useful helper
methods that are quite common to have in a web project.
It is organized like Rack::Utils
and actually extends it.
Some of the methods are similar to methods you would have
in Active::Support
but without monkey patching. Nevertheless it
is only a coincidence. The purpose is not to create an alternative.
Here is how you would use it with Sinatra
:
require 'sinatra/base'
require 'web_utils'
class Main < Sinatra::Base
# Your frontend code...
helpers do
include WebUtils
# Your other helpers...
end
end
Some methods are also useful on the model side of your app.
Here is a list of the available methods and what they do:
Just tells you if the string is blank or not.
Find a plural version for words. It is meant to deal with most common cases and therefore is not exhaustive.
Basically the opposite of pluralize
.
Turns class names into a dasherized version (e.g. "web-utils"
).
Module separator is a double dash.
So "BlogArticle::Comment"
becomes "blog-article--comment"
.
Also for simplicity, it does not try to be clever with accronyms.
So "Net::FTP"
returns "net--f-t-p"
.
This is useful for urls or creating CSS class names or IDs.
Basically the opposite of dasherize_class_name
.
It takes the class name as a string and returns the class.
You can pass a class name with modules as well (e.g. "Net::FTP"
).
This is actually the main reason why there is a context
argument, because it uses recursion to do this.
But context
is still useful otherwise.
Same except that it takes the dasherized version of the class name as an argument and returns the class itself (not a string). Useful for resolving a class from a URL param.
It is mainly used for guessing the class name of a
children class with a plural name.
So guess_related_class_name(BlogArticle, :comments)
will return "BlogArticle::Comment"
.
It is used for example for getting a default value for something and it is passed either as:
- A direct value (e.g.
"John Doe"
) - A proc (e.g.
proc{ Time.now }
) - A symbol (e.g.
:get_last_value
)
In the case of a symbol, the message is called on the target. Therefore you would always give the target just in case it is a symbol.
If the value can only be direct or a Proc, you can ignore the second argument (target).
This makes a deeper copy of an object, since dup
does not
duplicate nested objects in a hash for example. It uses a simple
marshal/unmarshal mechanism.
It is a bit of a hack and not that web-specific, but useful if you want to avoid some nasty bugs.
If the hash does not have the key, it sets it with the default value. And this value is also returned by the method.
Same as ensure_key!
except that it does not change the original
hash. It returns a new one (following the bang convention).
This makes the strings ready to be used as a slug in a URL.
It removes the accents, replaces a lot of separators with
dashes and escapes it. By default it forces the output to
be lowercase, but if you pass false
as the second argument,
it will not change the case of letters.
Returns a human readable version of a field name.
So :label_for_field
returns "Label for field"
.
It says field
, but it could be used with any kind of symbol.
It is used to run something on all the nested stubs of an array or a hash. The second argument of the block is either a key if the object is a hash, or an index if the object is an array.
It tries to change a string value received by an HTML form
or a CSV file into an object when it can. So far it recognizes
simple things like true
, false
, integers and floats.
And an empty string is always nil
.
The second argument is the list of things you want to typecast.
By default there is everything, but if you only want to typecast
integers and floats, you can pass [:int, :float]
.
The classic nl2br
which makes sure return lines are
turned into <br>
tags. You can use the second argument if
you want to specify what the replacement tag should be.
Just in case you want self-closing tags.
This just makes sure that a link is complete. Very often
people tend to enter a URL like "www.google.com"
which is a
controversial href
for some browsers. This method would change it for
"//www.google.com"
. Already seemingly complete links are not
affected by the method.
This tells you if a link is pointing to the current site or
an external one (based on the presence of a domain name or not).
This is useful when you want to create a link
tag and want to decide if target is "_blank"
or "_self"
.
This automatically does nl2br
and links recognizable things
like email addresses and URLs. Not as good as markdown, but it
is quite useful, should it be only for turning an email into a link.
It truncates a string like what you have in blog summaries.
It automatically removes tags and line breaks. The size is
320 by default. When the original string was longer, it puts
an ellipsis at the end which can be replaced by whatever you put
as a 3rd argument. e.g. "...and more"
.
It takes a query string (potentially from a search field) and builds a regex for matching a string which contains all the words of the query, whatever order they appear in.
If you pass false
as the second argument, the regex will match
if the text contains at least one word of the query instead of all.
It changes a price in cents/pence into a formated string
like "49,425.40"
when you pass 4942540
.
It does the opposite of display_price
and parses a string in
order to return a price in cents/pence.
It takes the path to a file and add the brand/prefix and a dash
before the file name (really the file name, not the path).
By default, the brand/prefix is "WebUtils"
.
For example you have a file "/path/to/image.jpg"
and you want
to create its thumbnail
in png
, you can create the thumbnail
path with filename_variation(path, :thumbnail, :png)
and it
will return "/path/to/image.thumbnail.png"
.
You basically pass the Request
object to the method and it
looks at the referrer and returns true if it was not on the same
domain. Essentially tells you if the visitor just arrived on your website.
While this method is useful, it only checks for the presence of
these words /bot|crawl|slurp|spider/i
to determine if the user
agent is a crawler or not. So it is pretty basic. If you have a
better way, please make a pull request.
Just the usual shortcut for Rack::Utils.escape_html
.
Just the usual shortcut for Rack::Utils.escape
for escaping
what you want to put in a URL.
Returns a google maps link with a marker at address passed as argument.