module MongoMapper module Plugins module Rails module InstanceMethods def to_param id.to_s end def new_record? new? end def read_attribute(name) self[name] end def read_attribute_before_typecast(name) read_key_before_typecast(name) end def write_attribute(name, value) self[name] = value end end module ClassMethods def has_one(*args) one(*args) end def has_many(*args) many(*args) end def column_names keys.keys end def self_and_descendants#nodoc: klass = self classes = [klass] while klass != klass.base_class classes << klass = klass.superclass end classes rescue [self] end # Transforms attribute key names into a more humane format, such as "First name" instead of "first_name". Example: # Person.human_attribute_name("first_name") # => "First name" # This used to be depricated in favor of humanize, but is now preferred, because it automatically uses the I18n # module now. # Specify +options+ with additional translating options. def human_attribute_name(attribute_key_name, options = {}) defaults = self_and_descendants.map do |klass| :"#{klass.name.underscore}.#{attribute_key_name}" end defaults << options[:default] if options[:default] defaults.flatten! defaults << attribute_key_name.to_s.humanize options[:count] ||= 1 I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:mongo_mapper, :attributes])) end # Transform the modelname into a more humane format, using I18n. # Defaults to the basic humanize method. # Default scope of the translation is mongo_mapper.models # Specify +options+ with additional translating options. def human_name(options = {}) defaults = self_and_descendants.map do |klass| :"#{klass.name.underscore}" end defaults << self.name.humanize I18n.translate(defaults.shift, {:scope => [:mongo_mapper, :models], :count => 1, :default => defaults}.merge(options)) end end end end end