2009-04-01 20:08:20 +02:00
|
|
|
# This file contains various hacks for Rails compatibility.
|
|
|
|
# To use, just require in environment.rb, like so:
|
|
|
|
#
|
|
|
|
# require 'couchrest/support/rails'
|
|
|
|
|
|
|
|
class Hash
|
|
|
|
# Hack so that CouchRest::Document, which descends from Hash,
|
|
|
|
# doesn't appear to Rails routing as a Hash of options
|
|
|
|
def self.===(other)
|
2009-04-01 23:00:36 +02:00
|
|
|
return false if self == Hash && other.is_a?(CouchRest::Document)
|
2009-04-01 20:08:20 +02:00
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
CouchRest::Document.class_eval do
|
2009-06-07 11:51:50 +02:00
|
|
|
# Need this when passing doc to a resourceful route
|
|
|
|
alias_method :to_param, :id
|
|
|
|
|
2009-04-01 20:08:20 +02:00
|
|
|
# Hack so that CouchRest::Document, which descends from Hash,
|
|
|
|
# doesn't appear to Rails routing as a Hash of options
|
|
|
|
def is_a?(o)
|
|
|
|
return false if o == Hash
|
|
|
|
super
|
|
|
|
end
|
|
|
|
alias_method :kind_of?, :is_a?
|
2009-06-07 11:51:50 +02:00
|
|
|
|
|
|
|
# Gives extended doc a seamless logger
|
|
|
|
def logger
|
|
|
|
ActiveRecord::Base.logger
|
|
|
|
end
|
2009-04-01 20:08:20 +02:00
|
|
|
end
|
|
|
|
|
2009-05-29 07:42:30 +02:00
|
|
|
CouchRest::CastedModel.class_eval do
|
|
|
|
# The to_param method is needed for rails to generate resourceful routes.
|
|
|
|
# In your controller, remember that it's actually the id of the document.
|
|
|
|
def id
|
2009-05-31 08:20:39 +02:00
|
|
|
return nil if base_doc.nil?
|
2009-05-29 07:42:30 +02:00
|
|
|
base_doc.id
|
|
|
|
end
|
|
|
|
alias_method :to_param, :id
|
|
|
|
end
|
2009-04-01 20:08:20 +02:00
|
|
|
|
|
|
|
require Pathname.new(File.dirname(__FILE__)).join('..', 'validation', 'validation_errors')
|
|
|
|
|
|
|
|
CouchRest::Validation::ValidationErrors.class_eval do
|
|
|
|
# Returns the total number of errors added. Two errors added to the same attribute will be counted as such.
|
|
|
|
# This method is called by error_messages_for
|
|
|
|
def count
|
|
|
|
errors.values.inject(0) { |error_count, errors_for_attribute| error_count + errors_for_attribute.size }
|
|
|
|
end
|
|
|
|
end
|