lib/ripple/validations.rb in ripple-0.8.0.beta2 vs lib/ripple/validations.rb in ripple-0.8.0

- old
+ new

@@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. require 'ripple' module Ripple - # Raised by <tt>save!</tt> when the document is invalid. Use the # +document+ method to retrieve the document which did not validate. # begin # invalid_document.save! # rescue Ripple::DocumentInvalid => invalid @@ -30,10 +29,12 @@ errors = @document.errors.full_messages.join(", ") super(t("document_invalid", :errors => errors)) end end + # Adds validations to {Ripple::Document} models. Validations are + # executed before saving the document. module Validations extend ActiveSupport::Concern extend ActiveSupport::Autoload include ActiveModel::Validations @@ -58,14 +59,18 @@ # @private def save(options={:validate => true}) return false if options[:validate] && !valid? super() end - + + # Saves the document and raises {DocumentInvalid} exception if + # validations fail. def save! (raise Ripple::DocumentInvalid.new(self) unless save) || true end + # Sets the passed attributes and saves the document, raising a + # {DocumentInvalid} exception if the validations fail. def update_attributes!(attrs) self.attributes = attrs save! end end