mislav-will_paginate 2.3.4 → 2.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ == 2.3.5, released 2008-10-07
2
+
3
+ * update the backported named_scope implementation for Rails versions older than 2.1
4
+ * break out of scope of paginated_each() yielded block when used on named scopes
5
+ * fix paginate(:from)
6
+
1
7
  == 2.3.4, released 2008-09-16
2
8
 
3
9
  * Removed gem dependency to Active Support (causes trouble with vendored rails).
@@ -104,7 +104,10 @@ module WillPaginate
104
104
 
105
105
  begin
106
106
  collection = paginate(options)
107
- total += collection.each(&block).size
107
+ with_exclusive_scope(:find => {}) do
108
+ # using exclusive scope so that the block is yielded in scope-free context
109
+ total += collection.each(&block).size
110
+ end
108
111
  options[:page] += 1
109
112
  end until collection.size < collection.per_page
110
113
 
@@ -181,6 +184,7 @@ module WillPaginate
181
184
  # in the database. It relies on the ActiveRecord +count+ method.
182
185
  def wp_count(options, args, finder)
183
186
  excludees = [:count, :order, :limit, :offset, :readonly]
187
+ excludees << :from unless ActiveRecord::Calculations::CALCULATIONS_OPTIONS.include?(:from)
184
188
 
185
189
  # we may be in a model or an association proxy
186
190
  klass = (@owner and @reflection) ? @reflection.klass : self
@@ -1,27 +1,22 @@
1
- ## stolen from: http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/named_scope.rb?rev=9084
2
-
3
1
  module WillPaginate
4
2
  # This is a feature backported from Rails 2.1 because of its usefullness not only with will_paginate,
5
3
  # but in other aspects when managing complex conditions that you want to be reusable.
6
4
  module NamedScope
7
5
  # All subclasses of ActiveRecord::Base have two named_scopes:
8
6
  # * <tt>all</tt>, which is similar to a <tt>find(:all)</tt> query, and
9
- # * <tt>scoped</tt>, which allows for the creation of anonymous scopes, on the fly:
10
- #
11
- # Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)
7
+ # * <tt>scoped</tt>, which allows for the creation of anonymous scopes, on the fly: <tt>Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)</tt>
12
8
  #
13
9
  # These anonymous scopes tend to be useful when procedurally generating complex queries, where passing
14
10
  # intermediate values (scopes) around as first-class objects is convenient.
15
11
  def self.included(base)
16
12
  base.class_eval do
17
13
  extend ClassMethods
18
- named_scope :all
19
14
  named_scope :scoped, lambda { |scope| scope }
20
15
  end
21
16
  end
22
17
 
23
18
  module ClassMethods
24
- def scopes #:nodoc:
19
+ def scopes
25
20
  read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
26
21
  end
27
22
 
@@ -46,7 +41,7 @@ module WillPaginate
46
41
  # Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments
47
42
  # for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
48
43
  #
49
- # All scopes are available as class methods on the ActiveRecord descendent upon which the scopes were defined. But they are also available to
44
+ # All scopes are available as class methods on the ActiveRecord::Base descendent upon which the scopes were defined. But they are also available to
50
45
  # <tt>has_many</tt> associations. If,
51
46
  #
52
47
  # class Person < ActiveRecord::Base
@@ -76,7 +71,20 @@ module WillPaginate
76
71
  # end
77
72
  # end
78
73
  #
74
+ #
75
+ # For testing complex named scopes, you can examine the scoping options using the
76
+ # <tt>proxy_options</tt> method on the proxy itself.
77
+ #
78
+ # class Shirt < ActiveRecord::Base
79
+ # named_scope :colored, lambda { |color|
80
+ # { :conditions => { :color => color } }
81
+ # }
82
+ # end
83
+ #
84
+ # expected_options = { :conditions => { :colored => 'red' } }
85
+ # assert_equal expected_options, Shirt.colored('red').proxy_options
79
86
  def named_scope(name, options = {}, &block)
87
+ name = name.to_sym
80
88
  scopes[name] = lambda do |parent_scope, *args|
81
89
  Scope.new(parent_scope, case options
82
90
  when Hash
@@ -93,9 +101,15 @@ module WillPaginate
93
101
  end
94
102
  end
95
103
 
96
- class Scope #:nodoc:
104
+ class Scope
97
105
  attr_reader :proxy_scope, :proxy_options
98
- [].methods.each { |m| delegate m, :to => :proxy_found unless m =~ /(^__|^nil\?|^send|class|extend|find|count|sum|average|maximum|minimum|paginate)/ }
106
+
107
+ [].methods.each do |m|
108
+ unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|^find$|count|sum|average|maximum|minimum|paginate|first|last|empty\?|respond_to\?)/
109
+ delegate m, :to => :proxy_found
110
+ end
111
+ end
112
+
99
113
  delegate :scopes, :with_scope, :to => :proxy_scope
100
114
 
101
115
  def initialize(proxy_scope, options, &block)
@@ -108,6 +122,30 @@ module WillPaginate
108
122
  load_found; self
109
123
  end
110
124
 
125
+ def first(*args)
126
+ if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
127
+ proxy_found.first(*args)
128
+ else
129
+ find(:first, *args)
130
+ end
131
+ end
132
+
133
+ def last(*args)
134
+ if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
135
+ proxy_found.last(*args)
136
+ else
137
+ find(:last, *args)
138
+ end
139
+ end
140
+
141
+ def empty?
142
+ @found ? @found.empty? : count.zero?
143
+ end
144
+
145
+ def respond_to?(method, include_private = false)
146
+ super || @proxy_scope.respond_to?(method, include_private)
147
+ end
148
+
111
149
  protected
112
150
  def proxy_found
113
151
  @found || load_found
@@ -1,5 +1,3 @@
1
- ## based on http://dev.rubyonrails.org/changeset/9084
2
-
3
1
  ActiveRecord::Associations::AssociationProxy.class_eval do
4
2
  protected
5
3
  def with_scope(*args, &block)
@@ -2,7 +2,7 @@ module WillPaginate
2
2
  module VERSION
3
3
  MAJOR = 2
4
4
  MINOR = 3
5
- TINY = 3
5
+ TINY = 5
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -2,6 +2,9 @@ require 'helper'
2
2
  require 'will_paginate/array'
3
3
 
4
4
  class ArrayPaginationTest < Test::Unit::TestCase
5
+
6
+ def setup ; end
7
+
5
8
  def test_simple
6
9
  collection = ('a'..'e').to_a
7
10
 
@@ -17,6 +17,6 @@ mysql:
17
17
  postgres:
18
18
  adapter: postgresql
19
19
  username: mislav
20
- password: mislav
20
+ password:
21
21
  database: will_paginate_unittest
22
22
  min_messages: warning
@@ -261,6 +261,12 @@ class FinderTest < ActiveRecordTestCase
261
261
  assert_equal 1, entries.total_entries, 'only one topic should be found'
262
262
  end
263
263
  end
264
+
265
+ def test_named_scope_with_include
266
+ project = projects(:active_record)
267
+ entries = project.topics.with_replies_starting_with('AR ').paginate(:page => 1, :per_page => 1)
268
+ assert_equal 1, entries.size
269
+ end
264
270
 
265
271
  ## misc ##
266
272
 
@@ -427,6 +433,12 @@ class FinderTest < ActiveRecordTestCase
427
433
  assert_equal 14, Developer.paginated_each(:page => '2') { }
428
434
  end
429
435
 
436
+ def test_paginated_each_with_named_scope
437
+ assert_equal 2, Developer.poor.paginated_each(:per_page => 1) {
438
+ assert_equal 11, Developer.count
439
+ }
440
+ end
441
+
430
442
  # detect ActiveRecord 2.1
431
443
  if ActiveRecord::Base.private_methods.include?('references_eager_loaded_tables?')
432
444
  def test_removes_irrelevant_includes_in_count
@@ -444,5 +456,21 @@ class FinderTest < ActiveRecordTestCase
444
456
  :include => :projects, :conditions => 'projects.id > 2'
445
457
  end
446
458
  end
459
+
460
+ def test_paginate_from
461
+ result = Developer.paginate(:from => 'users', :page => 1, :per_page => 1)
462
+ assert_equal 1, result.size
463
+ end
464
+
465
+ def test_hmt_with_include
466
+ # ticket #220
467
+ reply = projects(:active_record).replies.find(:first, :order => 'replies.id')
468
+ assert_equal replies(:decisive), reply
469
+
470
+ # ticket #223
471
+ Project.find(1, :include => :replies)
472
+
473
+ # I cannot reproduce any of the failures from those reports :(
474
+ end
447
475
  end
448
476
  end
@@ -3,4 +3,8 @@ class Topic < ActiveRecord::Base
3
3
  belongs_to :project
4
4
 
5
5
  named_scope :mentions_activerecord, :conditions => ['topics.title LIKE ?', '%ActiveRecord%']
6
+
7
+ named_scope :with_replies_starting_with, lambda { |text|
8
+ { :conditions => "replies.content LIKE '#{text}%' ", :include => :replies }
9
+ }
6
10
  end
@@ -35,7 +35,7 @@ task :test_full => %w(test test_mysql test_postgres)
35
35
  desc %{Test everything with Rails 2.1.x, 2.0.x & 1.2.x gems}
36
36
  task :test_all do
37
37
  all = Rake::Task['test_full']
38
- versions = %w(2.1.0 2.0.2 1.2.6)
38
+ versions = %w(2.1.0 2.0.4 1.2.6)
39
39
  versions.each do |version|
40
40
  ENV['RAILS_VERSION'] = "~> #{version}"
41
41
  all.invoke
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mislav-will_paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.4
4
+ version: 2.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Mislav Marohni\xC4\x87"
@@ -10,10 +10,18 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-09-16 00:00:00 -07:00
13
+ date: 2008-10-07 00:00:00 -07:00
14
14
  default_executable:
15
- dependencies: []
16
-
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activesupport
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.4.4
24
+ version:
17
25
  description: The will_paginate library provides a simple, yet powerful and extensible API for ActiveRecord pagination and rendering of pagination links in ActionView templates.
18
26
  email: mislav.marohnic@gmail.com
19
27
  executables: []