![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
This gem adds a simple class method validation_scope
to ActiveRecord. This generates a new collection of
ActiveRecord::Errors
that can be manipulated independently of the standard errors
, valid?
and save
methods. The
full power of ActiveRecord validations are preserved in these distinct error collections, including all the macros.
For example, in addition to standard errors that prevent an object from being saved to the database, you may want a second collection of warnings that you display to the user or otherwise shape the control flow:
class Film < ActiveRecord::Base
validates_presence_of :title # Standard errors
validation_scope :warnings do |s|
s.validate :ensure_title_is_capitalized
s.validate { |r| r.warnings.add_to_base("Inline warning") }
s.validates_presence_of…
s.validates_inclusion_of…
s.validates_each…
s.validates_on_create…
end
def ensure_title_is_capitalized
warnings.add(:title, "should be capitalized") unless title =~ %r{\A[A-Z]}
end
end
The generated scope produces 3 helper methods based on the symbol passed to the validation_scope method. Continuing the previous example:
film = Film.new(:title => 'lowercase title')
film.valid?
=> true
film.no_warnings? # analagous to valid?
=> false
film.has_warnings? # analagous to invalid?
=> true
film.warnings # analagous to film.errors
=> #<ActiveRecord::Errors>
film.warnings.full_messages
=> ["Title should be capitalized", "Inline warning"]
film.errors.full_messages
=> []
film.class.all_scopes
=> [:warnings]
film.save
=> true
One rough edge at the moment is when you want to use the builtin error_messages_for
helper in your views. That helper
does not accept an ActiveRecord::Errors
object directly. Instead you need to pass it the proxy object that
ValidationScopes
creates to encapsulate the generated error set:
error_messages_for :object => film.validation_scope_proxy_for_warnings
The current version should work for Rails >= 3.0 and Ruby >= 1.9.2.
For Rails 3 and Ruby 1.8.x use version 0.4.x, however beware there is a memory leak in this version as described here
For Rails 2 see the 0.3.x version of the gem which is maintained on the rails2 branch
The usual:
gem install validation_scopes
In your Gemfile:
gem 'validation_scopes'
Or without Bundler:
require 'validation_scopes'
Because the any validation method supplied as a symbol (eg. validate :verify_something
) is actually running in the
context of a delegate class, private methods won't work as they would in standard validations.
Copyright (c) 2010-2021 Gabe da Silveira. See LICENSE for details.
FAQs
Unknown package
We found that validation_scopes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.