shuber-sortable 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 2009-05-27 - Sean Huber (shuber@huberry.com)
2
+ * Use self.class.base_class when scoping records
3
+ * Fix conflicts
4
+
1
5
  2009-05-14 - Sean Huber (shuber@huberry.com)
2
6
  * Remove MIT-LICENSE from gemspec - github complaining for some reason
3
7
  * Type cast position and offset arguments as integers
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Sean Huber (shuber@huberry.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = sortable
2
2
 
3
- Allows you to sort ActiveRecord items similar to http://github.com/rails/acts_as_list but with support for multiple scopes and lists
3
+ Allows you to sort ActiveRecord items similar to http://github.com/rails/acts_as_list but with added support for multiple scopes and lists
4
4
 
5
5
 
6
6
  == Installation
data/lib/sortable.rb CHANGED
@@ -163,7 +163,7 @@ module Huberry
163
163
  # Returns the first item in a list associated with the current item
164
164
  def first_item(list_name = nil)
165
165
  options = evaluate_sortable_options(list_name)
166
- self.class.send("find_by_#{options[:column]}".to_sym, 1, :conditions => options[:conditions])
166
+ self.class.base_class.send("find_by_#{options[:column]}".to_sym, 1, :conditions => options[:conditions])
167
167
  end
168
168
 
169
169
  # Returns a boolean after determining if the current item is the first item in the specified list
@@ -207,14 +207,14 @@ module Huberry
207
207
  # Returns nil if an item at the specified offset could not be found
208
208
  def item_at_offset(offset, list_name = nil)
209
209
  options = evaluate_sortable_options(list_name)
210
- in_list?(list_name) ? self.class.send("find_by_#{options[:column]}".to_sym, send(options[:column]) + offset.to_s.to_i, :conditions => options[:conditions]) : nil
210
+ in_list?(list_name) ? self.class.base_class.send("find_by_#{options[:column]}".to_sym, send(options[:column]) + offset.to_s.to_i, :conditions => options[:conditions]) : nil
211
211
  end
212
212
 
213
213
  # Returns the last item in a list associated with the current item
214
214
  def last_item(list_name = nil)
215
215
  options = evaluate_sortable_options(list_name)
216
216
  (options[:conditions].is_a?(Array) ? options[:conditions].first : options[:conditions]) << " AND #{self.class.table_name}.#{options[:column]} IS NOT NULL "
217
- self.class.find(:last, :conditions => options[:conditions], :order => options[:column].to_s)
217
+ self.class.base_class.find(:last, :conditions => options[:conditions], :order => options[:column].to_s)
218
218
  end
219
219
 
220
220
  # Returns a boolean after determining if the current item is the last item in the specified list
@@ -305,7 +305,7 @@ module Huberry
305
305
  # Returns the evaluated options
306
306
  def evaluate_sortable_options(list_name = nil)
307
307
  self.class.assert_sortable_list_exists!(list_name)
308
- options = self.class.sortable_lists[list_name.to_s].inject({}) { |hash, pair| hash[pair.first] = pair.last.nil? || pair.last.is_a?(Symbol) ? pair.last : pair.last.dup; hash }
308
+ options = self.class.sortable_lists[list_name.to_s].inject({}) { |hash, pair| hash.merge! pair.first => (pair.last.dup rescue pair.last) }
309
309
  options[:scope].each do |scope|
310
310
  value = send(scope)
311
311
  if value.nil?
@@ -315,7 +315,7 @@ module Huberry
315
315
  options[:conditions] << value
316
316
  end
317
317
  end
318
- options
318
+ options
319
319
  end
320
320
 
321
321
  # Moves items with a position lower than a certain <tt>position</tt> by an offset of 1 in the specified
@@ -323,7 +323,7 @@ module Huberry
323
323
  def move_lower_items(direction, position, list_name = nil)
324
324
  options = evaluate_sortable_options(list_name)
325
325
  (options[:conditions].is_a?(Array) ? options[:conditions].first : options[:conditions]) << " AND #{self.class.table_name}.#{options[:column]} > '#{position}' AND #{self.class.table_name}.#{options[:column]} IS NOT NULL "
326
- self.class.update_all "#{options[:column]} = #{options[:column]} #{direction == :up ? '-' : '+'} 1", options[:conditions]
326
+ self.class.base_class.update_all "#{options[:column]} = #{options[:column]} #{direction == :up ? '-' : '+'} 1", options[:conditions]
327
327
  end
328
328
 
329
329
  # Removes the current item from the specified list
@@ -6,22 +6,28 @@ require File.dirname(__FILE__) + '/../lib/sortable'
6
6
 
7
7
  ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
8
8
 
9
- def create_todos_table
9
+ def create_tables
10
10
  silence_stream(STDOUT) do
11
11
  ActiveRecord::Schema.define(:version => 1) do
12
12
  create_table :todos do |t|
13
13
  t.integer :project_id
14
+ t.string :type
14
15
  t.string :action
15
16
  t.integer :client_priority
16
17
  t.integer :developer_priority
17
18
  t.integer :position
18
19
  end
20
+
21
+ create_table :users do |t|
22
+ t.string :type
23
+ t.integer :position
24
+ end
19
25
  end
20
26
  end
21
27
  end
22
28
 
23
29
  # The table needs to exist before defining the class
24
- create_todos_table
30
+ create_tables
25
31
 
26
32
  class Todo < ActiveRecord::Base
27
33
  sortable :scope => :project_id, :conditions => 'todos.action IS NOT NULL'
@@ -29,11 +35,21 @@ class Todo < ActiveRecord::Base
29
35
  sortable :scope => :project_id, :column => :developer_priority, :list_name => :developer
30
36
  end
31
37
 
38
+ class TodoChild < Todo
39
+ end
40
+
41
+ class User < ActiveRecord::Base
42
+ sortable :scope => :type
43
+ end
44
+
45
+ class Admin < User
46
+ end
47
+
32
48
  class SortableTest < Test::Unit::TestCase
33
49
 
34
50
  def setup
35
51
  ActiveRecord::Base.connection.tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
36
- create_todos_table
52
+ create_tables
37
53
  end
38
54
 
39
55
  def test_should_add_to_lists
@@ -257,4 +273,24 @@ class SortableTest < Test::Unit::TestCase
257
273
  assert_equal 3, @todo_6.position
258
274
  end
259
275
 
276
+ def test_should_scope_with_base_class
277
+ @todo = Todo.create :action => 'test'
278
+ @todo_2 = TodoChild.create :action => 'test'
279
+ @todo_3 = Todo.create :action => 'test'
280
+ assert_equal 1, @todo.position
281
+ assert_equal 2, @todo_2.position
282
+ assert_equal 3, @todo_3.position
283
+ end
284
+
285
+ def test_should_not_scope_with_base_class
286
+ @user = User.create
287
+ @admin = Admin.create
288
+ @user_2 = User.create
289
+ @admin_2 = Admin.create
290
+ assert_equal 1, @user.position
291
+ assert_equal 2, @user_2.position
292
+ assert_equal 1, @admin.position
293
+ assert_equal 2, @admin_2.position
294
+ end
295
+
260
296
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shuber-sortable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Huber
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-14 00:00:00 -07:00
12
+ date: 2009-05-27 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,7 @@ files:
25
25
  - CHANGELOG
26
26
  - init.rb
27
27
  - lib/sortable.rb
28
+ - MIT-LICENSE
28
29
  - Rakefile
29
30
  - README.rdoc
30
31
  has_rdoc: false