Tidying up rails support

This commit is contained in:
Peter Gumeson 2009-06-13 14:51:15 -07:00
parent 62e8709df7
commit a47de6aaf5
2 changed files with 38 additions and 44 deletions

View file

@ -46,7 +46,7 @@ module CouchRest
autoload :CastedModel, 'couchrest/more/casted_model' autoload :CastedModel, 'couchrest/more/casted_model'
require File.join(File.dirname(__FILE__), 'couchrest', 'mixins') require File.join(File.dirname(__FILE__), 'couchrest', 'mixins')
require File.join(File.dirname(__FILE__), 'couchrest', 'support', 'rails') require File.join(File.dirname(__FILE__), 'couchrest', 'support', 'rails') if defined?(Rails)
# The CouchRest module methods handle the basic JSON serialization # The CouchRest module methods handle the basic JSON serialization
# and deserialization, as well as query parameters. The module also includes # and deserialization, as well as query parameters. The module also includes

View file

@ -1,53 +1,47 @@
# This file contains various hacks for Rails compatibility. # This file contains various hacks for Rails compatibility.
class Hash
if defined?(Rails) # Hack so that CouchRest::Document, which descends from Hash,
# doesn't appear to Rails routing as a Hash of options
class Hash def self.===(other)
# Hack so that CouchRest::Document, which descends from Hash, return false if self == Hash && other.is_a?(CouchRest::Document)
# doesn't appear to Rails routing as a Hash of options super
def self.===(other)
return false if self == Hash && other.is_a?(CouchRest::Document)
super
end
end end
end
CouchRest::Document.class_eval do
# Need this when passing doc to a resourceful route
alias_method :to_param, :id
CouchRest::Document.class_eval do # Hack so that CouchRest::Document, which descends from Hash,
# Need this when passing doc to a resourceful route # doesn't appear to Rails routing as a Hash of options
alias_method :to_param, :id def is_a?(o)
return false if o == Hash
# Hack so that CouchRest::Document, which descends from Hash, super
# 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?
# Gives extended doc a seamless logger
def logger
ActiveRecord::Base.logger
end
end end
alias_method :kind_of?, :is_a?
CouchRest::CastedModel.class_eval do # Gives extended doc a seamless logger
# The to_param method is needed for rails to generate resourceful routes. def logger
# In your controller, remember that it's actually the id of the document. ActiveRecord::Base.logger
def id
return nil if base_doc.nil?
base_doc.id
end
alias_method :to_param, :id
end end
end
require Pathname.new(File.dirname(__FILE__)).join('..', 'validation', 'validation_errors') CouchRest::CastedModel.class_eval do
# The to_param method is needed for rails to generate resourceful routes.
CouchRest::Validation::ValidationErrors.class_eval do # In your controller, remember that it's actually the id of the document.
# Returns the total number of errors added. Two errors added to the same attribute will be counted as such. def id
# This method is called by error_messages_for return nil if base_doc.nil?
def count base_doc.id
errors.values.inject(0) { |error_count, errors_for_attribute| error_count + errors_for_attribute.size }
end
end end
alias_method :to_param, :id
end
end 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