# Validations based on negations of regular expressions.
# validates_not_format_of takes an array of attributes,
# a regular expression to apply to them,
# and an optional hash of configuration options.
#
# All other validations take an array of attributes,
# a word or list of words to act upon,
# and an optional hash of configuration options.
#
# Examples:
# validates_does_have_the_words :username, :description, %w(chicken yellow coward), :ignore_case => true
# In this case, the model will not be valid if the username or description attributes contain any of the
# restricted words, regardless of case.
#
# Negates a regular expression match against an attribute.
# Similar to grep -v
#
"A regular expression must be supplied as the :with option of the configuration hash""^#{word}"# Validates an attribute does not end with the provided string, or strings.
# Examples:
# validates_does_not_end_with_the_word :username, '2000'
# or
# validates_does_not_end_with_the_words :username, %w(xxx ooo 666 McFly)
#
"#{word}$"# Validates an attribute does not contain disallowed words
# Examples:
# validates_does_not_have_the_word :username, 'admin'
# or
# validates_does_not_have_the_words :username, %w(admin guest moderator)
#
"^#{word}$"# The block supplied to validates_attributes_not_format_of_block must yield a string
# that can be converted into a regular expression by Regexp.new
#
# The argument supplied to the block is a string that has been regular-expression-quoted,
# and is safe to use without further escaping.
#
# You can pass options for how the regular expression is created in the final options hash.
# Either use :ignore_case => true
# or
# :regexp_options => Fixnum
# either of which will be passed as the 2nd argument to Regexp.new, with :regexp_options taking precedence
# See documentation on Regexp.new for more information on allowable values for :regexp_options
#
"(#{yield Regexp.quote(w.to_s)})"'|'# Overridable default configuration
:message => 'is not valid'# Mandatory default configuration
span class="st0">"Please log in"
redirect_to(:controller => "login", :action => "login"
span class="co1"># Step through and instantiate each member of the class and execute on it,
# but instantiate no more than per_page instances at any given time.
# Safe for destructive actions or actions that modify the fields
# your :order or :conditions clauses operate on.
# By-id for model-modifying blocks
# Build SQL to get ids of all matching records using the options provided by the user
"`#{table_name}`.#{primary_key} AS id" }))
# Get the results as an array of tiny hashes { "id" => "1" } and flatten them out to just the ids
'id'# chop apart the all_ids array a segment at a time
"WHEN #{id} THEN #{i}"' ')
# Do the deed on this page of results
"#{primary_key} IN (?)""CASE id #{ids_cases} END"
This piece of code is perhaps named poorly. It arose from a need to operate on a large number of models during a migration.
Many migrations that operate on a large number of rows can accomplish their operation en-masse using an update_all.
span class="st0">'filename = \'default.png\'', 'default_avatar = true')
However, sometimes the operation being performed is more complex. Perhaps its a destructive migration. Perhaps it operates on a complex graph of objects, or over a polymorphic association, or perhaps you’re relying on the propagation of ActiveRecord’s callbacks.
In any of these cases, you can easily exhaust available RAM with code such as this:
ActiveRecord will first do a SQL SELECT with the provided parameters, instantiate each row of the result as an object in memory, and once all rows are instantiated, will iterate over the set, applying the block to each element.
Except if the number of rows returned is large enough, it never gets to the iterating part.
Instead of instantiating all the models at once, we instead instantiate them in bite-sized pieces.