Skip to content

Commit

Permalink
Cleaning up more code
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Dec 12, 2009
1 parent 60a0470 commit 1960826
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/mongoid/associations.rb
Expand Up @@ -143,8 +143,8 @@ def reflect_on_association(name)
# end
#
def relates_to_one(name, options = {})
field "#{name.to_s}_id"
index "#{name.to_s}_id"
key = name.to_s
field "#{key}_id"; index "#{key}_id"
add_association(
Associations::RelatesToOne,
Associations::Options.new(options.merge(:name => name))
Expand Down
11 changes: 4 additions & 7 deletions lib/mongoid/criteria.rb
Expand Up @@ -360,11 +360,8 @@ def order_by(params = [])
# Either returns the page option and removes it from the options, or
# returns a default value of 1.
def page
if @options[:skip] && @options[:limit]
(@options[:skip].to_i + @options[:limit].to_i) / @options[:limit].to_i
else
1
end
skips, limits = @options[:skip], @options[:limit]
(skips && limits) ? (skips + limits) / limits : 1
end

# Executes the +Criteria+ and paginates the results.
Expand Down Expand Up @@ -498,8 +495,8 @@ def filter_options
page_num = @options.delete(:page)
per_page_num = @options.delete(:per_page)
if (page_num || per_page_num)
@options[:limit] = (per_page_num || 20).to_i
@options[:skip] = (page_num || 1).to_i * @options[:limit] - @options[:limit]
@options[:limit] = limits = (per_page_num || 20).to_i
@options[:skip] = (page_num || 1).to_i * limits - limits
end
end

Expand Down
7 changes: 4 additions & 3 deletions lib/mongoid/document.rb
Expand Up @@ -109,8 +109,9 @@ def define(name, options = {})

# Set up a default value for a field.
def default(name, options = {})
value = options[:default]
@defaults ||= {}.with_indifferent_access
@defaults[name] = options[:default] if options[:default]
@defaults[name] = value if value
end

end
Expand Down Expand Up @@ -284,8 +285,8 @@ def to_param
# This will also cause the observing +Document+ to notify it's parent if
# there is any.
def update(child, clear = false)
@attributes.insert(child.association_name, child.attributes) unless clear
@attributes.delete(child.association_name) if clear
name = child.association_name
clear ? @attributes.delete(name) : @attributes.insert(name, child.attributes)
notify
end

Expand Down

0 comments on commit 1960826

Please sign in to comment.