Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

test refactored, rails 3 query syntax and new feature #13

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.redcar
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "http://rubygems.org"
gemspec
gem 'rake'
gem 'sqlite3'
gem 'activerecord',">=3.0.0"
gem 'yard'
38 changes: 38 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
PATH
remote: .
specs:
acts_as_list (0.2.5)
activerecord (>= 3.0.0)

GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.3)
activesupport (= 3.2.3)
builder (~> 3.0.0)
activerecord (3.2.3)
activemodel (= 3.2.3)
activesupport (= 3.2.3)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.0)
i18n (0.6.0)
multi_json (1.3.4)
rake (0.9.2.2)
sqlite3 (1.3.6)
tzinfo (0.3.33)
yard (0.8.1)

PLATFORMS
ruby

DEPENDENCIES
activerecord (>= 3.0.0)
acts_as_list!
rake
sqlite3
yard
27 changes: 27 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
require 'bundler'
Bundler::GemHelper.install_tasks
require 'rake'
require 'rake/testtask'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "acts_as_list"
gem.summary = %Q{Gem version of acts_as_list Rails plugin}
gem.description = %Q{Gem version of acts_as_list Rails plugin}
gem.email = "[email protected]"
gem.homepage = "http://github.com/vpereira/acts_as_list"
gem.authors = ["Victor Pereira", "Ryan Bates", "Rails Core"]
gem.add_dependency "activerecord", ">= 3.0.0"
gem.add_development_dependency "yard"
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end

desc 'Default: run acts_as_list unit tests.'
task :default => :test

desc 'Test the acts_as_ordered_tree plugin.'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

require 'yard'
YARD::Rake::YardocTask.new do |t|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
t.options += ['--title', "acts_as_list #{version} Documentation"]
end

1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.1
51 changes: 51 additions & 0 deletions acts_as_list.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{acts_as_list}
s.version = "0.2.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Victor Pereira", "Ryan Bates", "Rails Core"]
s.date = %q{2011-01-24}
s.description = %q{Gem version of acts_as_list Rails plugin}
s.email = %q{[email protected]}
s.extra_rdoc_files = [
"README"
]
s.files = [
"README",
"Rakefile",
"VERSION",
"acts_as_list.gemspec",
"init.rb",
"lib/active_record/acts/list.rb",
"test/list_test.rb"
]
s.homepage = %q{http://github.com/vpereira/acts_as_list}
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{Gem version of acts_as_list Rails plugin}
s.test_files = [
"test/list_test.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
s.add_development_dependency(%q<yard>, [">= 0"])
else
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
s.add_dependency(%q<yard>, [">= 0"])
end
else
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
s.add_dependency(%q<yard>, [">= 0"])
end
end

2 changes: 2 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$:.unshift "#{File.dirname(__FILE__)}/lib"
require 'bundler/setup'
require 'active_record'
require 'active_record/acts/list'
ActiveRecord::Base.class_eval { include ActiveRecord::Acts::List }
37 changes: 27 additions & 10 deletions lib/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ module ClassMethods
# to give it an entire string that is interpolated if you need a tighter scope than just a foreign key.
# Example: <tt>acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
def acts_as_list(options = {})
configuration = { :column => "position", :scope => "1 = 1" }
configuration = { :column => "position", :scope => "1=1",:limited_list=>false }
configuration.update(options) if options.is_a?(Hash)



self.class.send(:define_method,"limited_list?") { configuration[:limited_list] }

#here if your scope receives a :symbol, it will try to add "_id" to the symbol, unless it has already an id in the end
configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/


if configuration[:scope].is_a?(Symbol)
scope_condition_method = %(
def scope_condition
Expand All @@ -55,9 +61,16 @@ def scope_condition
scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end"
end


self.class_eval do
send(:scope,:list,order('position asc'))
end


class_eval <<-EOV
include ActiveRecord::Acts::List::InstanceMethods



def acts_as_list_class
::#{self.name}
end
Expand All @@ -66,8 +79,10 @@ def position_column
'#{configuration[:column]}'
end


#{scope_condition_method}


before_destroy :decrement_positions_on_lower_items
before_create :add_to_list_bottom
EOV
Expand All @@ -80,7 +95,13 @@ def position_column
# the first in the list of all chapters.
module InstanceMethods
# Insert the item at the given position (defaults to the top position of 1).
#i just changed here to verify if the limited_list is true
def insert_at(position = 1)
if self.class.limited_list?
if position > bottom_position_in_list + 1
position = bottom_position_in_list
end
end
insert_at_position(position)
end

Expand All @@ -93,7 +114,7 @@ def move_lower
increment_position
end
end

# Swap positions with the next higher item, if one exists.
def move_higher
return unless higher_item
Expand Down Expand Up @@ -159,17 +180,13 @@ def last?
# Return the next higher item in the list.
def higher_item
return nil unless in_list?
acts_as_list_class.find(:first, :conditions =>
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}"
)
acts_as_list_class.where("#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}").first
end

# Return the next lower item in the list.
def lower_item
return nil unless in_list?
acts_as_list_class.find(:first, :conditions =>
"#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}"
)
acts_as_list_class.where("#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}").first
end

# Test if this record is in a list
Expand Down Expand Up @@ -200,7 +217,7 @@ def bottom_position_in_list(except = nil)
def bottom_item(except = nil)
conditions = scope_condition
conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
acts_as_list_class.find(:first, :conditions => conditions, :order => "#{position_column} DESC")
acts_as_list_class.where(conditions).order("#{position_column} DESC").first
end

# Forces item to assume the bottom position in the list.
Expand Down
Loading