Skip to content

Commit

Permalink
Make fields and defaults class_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Dec 7, 2011
1 parent e9fe860 commit 0fb7da6
Showing 1 changed file with 6 additions and 70 deletions.
76 changes: 6 additions & 70 deletions lib/mongoid/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,19 @@ module Fields
extend ActiveSupport::Concern

included do
class_attribute :defaults
class_attribute :fields

self.defaults = []
self.fields = {}

field(:_type, :type => String)
field(:_id, :type => BSON::ObjectId)

alias :id :_id
alias :id= :_id=
end

# Get the default fields.
#
# @note Refactored from using delegate for class load performance.
#
# @example Get the defaults.
# model.defaults
#
# @return [ Array<String> ] The default field names.
def defaults
self.class.defaults
end

# Get the document's fields.
#
# @note Refactored from using delegate for class load performance.
#
# @example Get the fields.
# model.fields
#
# @return [ Hash ] The fields.
def fields
self.class.fields
end

class << self

# Stores the provided block to be run when the option name specified is
Expand Down Expand Up @@ -103,28 +85,6 @@ def options

module ClassMethods #:nodoc

# Returns the default values for the fields on the document.
#
# @example Get the defaults.
# Person.defaults
#
# @return [ Hash ] The field defaults.
def defaults
@defaults ||= []
end

# Set the defaults for the class.
#
# @example Set the defaults.
# Person.defaults = defaults
#
# @param [ Array ] defaults The array of defaults to set.
#
# @since 2.0.0.rc.6
def defaults=(defaults)
@defaults = defaults
end

# Defines all the fields that are accessible on the Document
# For each field that is defined, a getter and setter will be
# added as an instance method to the Document.
Expand All @@ -145,30 +105,6 @@ def field(name, options = {})
add_field(name.to_s, options)
end

# Return the fields for this class.
#
# @example Get the fields.
# Person.fields
#
# @return [ Hash ] The fields for this document.
#
# @since 2.0.0.rc.6
def fields
@fields ||= {}
end

# Set the fields for the class.
#
# @example Set the fields.
# Person.fields = fields
#
# @param [ Hash ] fields The hash of fields to set.
#
# @since 2.0.0.rc.6
def fields=(fields)
@fields = fields
end

# When inheriting, we want to copy the fields from the parent class and
# set the on the child to start, mimicking the behaviour of the old
# class_inheritable_accessor that was deprecated in Rails edge.
Expand Down

0 comments on commit 0fb7da6

Please sign in to comment.