kkantena-progress_adapter 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +40 -0
- data/lib/progress_adapter.rb +58 -0
- data/test/progress_adapter_tests.rb +29 -0
- metadata +55 -0
data/README
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
KANTENA Paris France
|
2
|
+
May 2009
|
3
|
+
|
4
|
+
1 - Purpose
|
5
|
+
|
6
|
+
PROGRESS ADAPTER is for use when DBMS engine doesn't support LIMIT and OFFSET SQL instructions,
|
7
|
+
as PROGRESS DBMS don't (until v 10.2 ).
|
8
|
+
With such DBMS and rails you load all records and instantiate each object for , which can be such longer !
|
9
|
+
Tough , no pagination is possible .
|
10
|
+
With this ProgressAdapter module , you easily can !
|
11
|
+
|
12
|
+
|
13
|
+
2 - Install
|
14
|
+
|
15
|
+
Install plugin : script/plugin git://github.com/kkantena/progress_adapter.git
|
16
|
+
|
17
|
+
|
18
|
+
3 - Use it !
|
19
|
+
|
20
|
+
Add "include ProgressAdapter" in models where you need it.
|
21
|
+
|
22
|
+
You can use ActiveRecord::Base.find as usual , and can now pass two more keys :
|
23
|
+
:page => <number for page you need>
|
24
|
+
:per_page => < number of object instanciation records you want>
|
25
|
+
|
26
|
+
To get number of records : number_of_records method
|
27
|
+
To get number of pages : number_of_pages method
|
28
|
+
|
29
|
+
Example :
|
30
|
+
---------
|
31
|
+
class Foo < ActiveRecord::Base
|
32
|
+
include ProgressAdapter
|
33
|
+
|
34
|
+
def list
|
35
|
+
list = find :all , :page => 4 , :per_page => 10
|
36
|
+
nb_records = number_of_records
|
37
|
+
nb_pages = number_of_pages
|
38
|
+
[nb_records,nb_pages,list]
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#Author : KANTENA Paris France
|
2
|
+
#May 2009
|
3
|
+
|
4
|
+
|
5
|
+
module ProgressAdapter
|
6
|
+
class ActiveRecord::Base
|
7
|
+
|
8
|
+
PROGRESS_PAGINATE_OPTIONS=[:page,:per_page]
|
9
|
+
VALID_FIND_OPTIONS=[ :conditions, :include, :joins, :limit, :offset,:order, :select, :readonly, :group, :having, :from, :lock]
|
10
|
+
|
11
|
+
def self.validate_find_options(options)
|
12
|
+
options.assert_valid_keys(VALID_FIND_OPTIONS+PROGRESS_PAGINATE_OPTIONS)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.find_every(options)
|
16
|
+
include_associations = merge_includes(scope(:find, :include), options[:include])
|
17
|
+
if include_associations.any? && references_eager_loaded_tables?(options)
|
18
|
+
records = find_with_associations(options)
|
19
|
+
else
|
20
|
+
records = find_by_sql(construct_finder_sql(options),options[:page],options[:per_page])
|
21
|
+
if include_associations.any?
|
22
|
+
preload_associations(records, include_associations)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
records.each { |record| record.readonly! } if options[:readonly]
|
26
|
+
records
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.number_of_pages
|
30
|
+
@@nb_pages
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.number_of_records
|
34
|
+
@@nb_records
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.find_by_sql(sql,page=nil,per_page=nil)
|
38
|
+
records = connection.select_all(sanitize_sql(sql), "#{name} Load")
|
39
|
+
@@nb_records = records.length
|
40
|
+
nb_record_per_page = 15
|
41
|
+
nb_record_per_page = per_page if per_page
|
42
|
+
@@nb_pages = records.length/nb_record_per_page + 1
|
43
|
+
page = @@nb_pages if page && page > @@nb_pages
|
44
|
+
page = 1 if page && page < 1
|
45
|
+
if page
|
46
|
+
from = (page - 1) * nb_record_per_page
|
47
|
+
to = (page) * nb_record_per_page -1
|
48
|
+
else
|
49
|
+
from = 0
|
50
|
+
to = @@nb_records
|
51
|
+
end
|
52
|
+
to = records.length if to > records.length
|
53
|
+
records[from..to].collect! { |record| instantiate(record) }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.dirname(__FILE__)+'/../test_helper'
|
2
|
+
|
3
|
+
class ProgressAdapterTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
#We use a Progress Database for these tests and an 'Adherents''s table
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@adherents = Adherent.find :all,
|
9
|
+
:select => "codaux",
|
10
|
+
:conditions => ["codsoc='CAV' and codaux <> ''"] ,
|
11
|
+
:page =>2 ,
|
12
|
+
:per_page=>10
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def test_length_of_result
|
17
|
+
assert @adherents.length <= 10
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_number_of_records
|
21
|
+
assert_equal 6352, Adherent.number_of_records
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_number_of_pages
|
25
|
+
assert_equal Adherent.number_of_records/10 + 1,Adherent.number_of_pages
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kkantena-progress_adapter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.0"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Cantin Philippe @ kantena "
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-01 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Gem for Rails and ActiveRecord with Progress databases which not have LIMIT and OFFSET SQL instructions
|
17
|
+
email: pca@kantena.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/progress_adapter.rb
|
26
|
+
- README
|
27
|
+
- test/progress_adapter_tests.rb
|
28
|
+
has_rdoc: false
|
29
|
+
homepage: http://github.com/kkantena/progress_adapter
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project: progress_adapter
|
50
|
+
rubygems_version: 1.2.0
|
51
|
+
signing_key:
|
52
|
+
specification_version: 2
|
53
|
+
summary: Gem for Rails and ActiveRecord with Progress databases which not have LIMIT and OFFSET SQL instructions
|
54
|
+
test_files: []
|
55
|
+
|