Skip to content

Commit

Permalink
fixed dependency issues and added ReferencedDocument spec by saberma
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Aug 8, 2010
1 parent dac83e1 commit f663f0a
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Gemfile
@@ -1,4 +1,4 @@
source :rubygems

gem 'mongoid', '>= 2.0.0.beta.7'
gem "mongoid_embedded_helper", '>= 0.2.3'
gem 'mongoid', '>= 2.0.0.beta.14'
gem "mongoid_embedded_helper", '>= 0.2.5'
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -11,8 +11,8 @@ begin
gem.email = "kmandrup@gmail.com"
gem.homepage = "http://github.com/rails/acts_as_list"
gem.authors = ["Kristian Mandrup"]
gem.add_dependency "mongoid", ">= 2.0.0.beta.7"
gem.add_dependency "mongoid_embedded_helper", ">= 0.2.3"
gem.add_dependency "mongoid", ">= 2.0.0.beta.14"
gem.add_dependency "mongoid_embedded_helper", ">= 0.2.5"
# gem.add_development_dependency "yard"
end
Jeweler::GemcutterTasks.new
Expand Down
7 changes: 4 additions & 3 deletions lib/mongoid/acts_as_list.rb
@@ -1,5 +1,6 @@
require "mongoid"
require 'mongoid_embedded_helper'
require 'mongoid_adjust'

module ActsAsList
module Mongoid
Expand Down Expand Up @@ -99,7 +100,7 @@ def order_by_position conditions, extras = []
sub_collection = if embedded?
sub_collection.sort { |x,y| x.my_position <=> y.my_position }
else
sub_collection.order_by([position_key, :desc])
sub_collection.order_by(position_key.to_sym.asc)
end

if !extras.empty?
Expand Down Expand Up @@ -340,7 +341,7 @@ def increment_positions_on_higher_items(min_pos = nil)
end

def adjust_all! collection, number
collection.adjust! position_key => number
collection.adjust!(position_key => number).each{|doc| doc.save}
end

def increase_all! collection
Expand Down Expand Up @@ -406,7 +407,7 @@ def [](field_name)
def []=(key, value)
if set_allowed?(key)
@attributes[key.to_s] = value
elsif write_allowed?(key)
else
self.send("#{key}=", value)
end
save!
Expand Down
17 changes: 17 additions & 0 deletions model/referenced_category.rb
@@ -0,0 +1,17 @@
class Category
include Mongoid::Document
include Mongoid::Timestamps
include ActsAsList::Mongoid

field :number, :type => Integer

field :pos, :type => Integer
acts_as_list :column => :pos

references_many :categories
referenced_in :category

def scope_condition
{:category_id => category.id, :pos.ne => nil}
end
end
177 changes: 177 additions & 0 deletions spec/acts_as_list/referenced_category_spec.rb
@@ -0,0 +1,177 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

require 'acts_as_list_mongoid'
require 'referenced_category'

describe 'ActsAsList for Mongoid' do

before :each do
@category = Category.create!
@category.categories = []
(1..4).each do |counter|
@category.categories << Category.new(:number => counter)
end
@category.save!

@category.categories.init_list!
end

after :each do
Mongoid.database.collections.each do |coll|
coll.remove
end
end

def get_positions category
category.reload.categories.sort { |x,y| x.my_position <=> y.my_position }.map(&:number)
end

context "4 category categories (1,2,3,4) that have parent_id pointing to first category container" do
describe '# initial configuration' do
it "should category categories 1 to 4 in order" do
positions = get_positions @category
positions.should == [1, 2, 3, 4]
end
end

describe '#reordering' do
it "should move item 2 to position 3" do
@category.categories[1].increment_position
@category.categories[2].decrement_position
get_positions(@category).should == [1, 3, 2, 4]
end


it "should move item 2 to position 3 directly" do
Category.where(:number => 2).first.move_lower
get_positions(@category).should == [1, 3, 2, 4]
end

it "move :down should move item 2 to position 3" do
Category.where(:number => 2).first.move(:down)
get_positions(@category).should == [1, 3, 2, 4]
end

it "move :lower should move item 2 to position 3" do
Category.where(:number => 2).first.move(:lower)
get_positions(@category).should == [1, 3, 2, 4]
end

it "should move item 2 to position 1" do
Category.where(:number => 2).first.move_higher
get_positions(@category).should == [2, 1, 3, 4]
end

it "move :up should move item 2 to position 1" do
Category.where(:number => 2).first.move(:up)
get_positions(@category).should == [2, 1, 3, 4]
end

it "move :higher should move item 2 to position 1" do
Category.where(:number => 2).first.move(:higher)
get_positions(@category).should == [2, 1, 3, 4]
end

it "should move item 1 to bottom" do
Category.where(:number => 1).first.move_to_bottom
get_positions(@category).should == [2, 3, 4, 1]
end

it "move :lowest should move item 1 to bottom" do
Category.where(:number => 1).first.move(:lowest)
get_positions(@category).should == [2, 3, 4, 1]
end

it "should move item 1 to top" do
Category.where(:number => 1).first.move_to_top
get_positions(@category).should == [1, 2, 3, 4]
end

it "move :highest should move item 1 to top" do
Category.where(:number => 1).first.move(:highest)
get_positions(@category).should == [1, 2, 3, 4]
end

it "move :unknown should cause argument error" do
lambda {Category.where(:number => 1).first.move(:unknown)}.should raise_error
end


it "should move item 2 to bottom" do
Category.where(:number => 2).first.move_to_bottom
get_positions(@category).should == [1, 3, 4, 2]
end

it "should move item 4 to top" do
Category.where(:number => 4).first.move_to_top
get_positions(@category).should == [4, 1, 2, 3]
end

it "should move item 3 to bottom" do
Category.where(:number => 3).first.move_to_bottom
get_positions(@category).should == [1, 2, 4, 3]

end

it "categories[2].move_to(4) should move item 2 to position 4" do
Category.where(:number => 2).first.move_to(4)
get_positions(@category).should == [1, 3, 4, 2]
end

it "categories[2].insert_at(3) should move item 2 to position 3" do
Category.where(:number => 2).first.insert_at(3)
get_positions(@category).should == [1, 3, 2, 4]
end

it "categories[2].move(:to => 3) should move item 2 to position 3" do
Category.where(:number => 2).first.move(:to => 3)
get_positions(@category).should == [1, 3, 2, 4]
end

it "categories[1].move_below(item[2]) should move item 1 to position 2" do
item2 = Category.where(:number => 2).first
Category.where(:number => 1).first.move_below(item2)
get_positions(@category).should == [2, 1, 3, 4]
end

it "categories[3].move_below(item[1]) should move item 3 to position 2" do
item1 = Category.where(:number => 1).first
Category.where(:number => 3).first.move_below(item1)
get_positions(@category).should == [1, 3, 2, 4]
end

it "categories[3].move_above(item[2]) should move item 3 to position 2" do
item2 = Category.where(:number => 2).first
Category.where(:number => 3).first.move_above(item2)
get_positions(@category).should == [1, 3, 2, 4]
end

it "categories[1].move_above(item[3]) should move item 1 to position 2" do
item3 = Category.where(:number => 3).first
Category.where(:number => 1).first.move_above(item3)
get_positions(@category).should == [2, 1, 3, 4]
end

end

describe 'relative position queries' do
it "should find item 2 to be lower item of item 1" do
expected = Category.where(:pos => 2).first
Category.where(:pos => 1).first.lower_item.should == expected
end

it "should not find any item higher than nr 1" do
Category.where(:pos => 1).first.higher_item.should == nil
end

it "should find item 3 to be higher item of item 4" do
expected = Category.where(:pos => 3).first
Category.where(:pos => 4).first.higher_item.should == expected
end

it "should not find item lower than item 4" do
Category.where(:pos => 4).first.lower_item.should == nil
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -2,6 +2,7 @@
require 'rspec/autorun'
require 'mongoid'
require 'mongoid_embedded_helper'
require 'mongoid_adjust'

$:.unshift "#{File.dirname(__FILE__)}/../model/"

Expand Down

0 comments on commit f663f0a

Please sign in to comment.