filterism 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -24
- data/.irbrc +2 -0
- data/.rspec +1 -2
- data/Gemfile +2 -2
- data/LICENSE.txt +22 -0
- data/README.md +41 -4
- data/Rakefile +1 -17
- data/exec/autospec +16 -0
- data/exec/htmldiff +16 -0
- data/exec/ldiff +16 -0
- data/exec/rake +16 -0
- data/exec/rspec +16 -0
- data/filterism.gemspec +25 -22
- data/lib/filterism.rb +3 -79
- data/lib/filterism/adapters/active_record_adapter.rb +45 -0
- data/lib/filterism/parsers/params_parser.rb +51 -0
- data/lib/filterism/version.rb +1 -1
- data/spec/adapters/active_record_adapter_spec.rb +84 -0
- data/spec/db/schema.rb +7 -0
- data/spec/factories.rb +8 -0
- data/spec/parsers/params_parser_spec.rb +52 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/support/active_record.rb +24 -0
- metadata +150 -148
- data/spec/filterism_spec.rb +0 -91
- data/spec/finders/activerecord_test_connector.rb +0 -104
- data/spec/fixtures/post.rb +0 -3
- data/spec/fixtures/posts.yml +0 -16
- data/spec/fixtures/schema.rb +0 -17
- data/spec/fixtures/user.rb +0 -2
- data/spec/fixtures/users.yml +0 -30
- data/spec/helper.rb +0 -6
data/spec/filterism_spec.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
require 'finders/activerecord_test_connector'
|
3
|
-
ActiverecordTestConnector.setup
|
4
|
-
|
5
|
-
describe Filterism do
|
6
|
-
extend ActiverecordTestConnector::FixtureSetup
|
7
|
-
fixtures :users
|
8
|
-
fixtures :posts
|
9
|
-
|
10
|
-
it "defines the COMPARATORS constant" do
|
11
|
-
Filterism::COMPARATORS.should be
|
12
|
-
Filterism::COMPARATORS.should be_a Hash
|
13
|
-
end
|
14
|
-
|
15
|
-
it "fitlers on equal_to" do
|
16
|
-
User.filter({'name_is_equal_to' => 'David'}).first.id.should == 1
|
17
|
-
User.filter({'name_is_equal_to' => 'Dave'}).first.id.should == 2
|
18
|
-
end
|
19
|
-
|
20
|
-
it "filters on not_equal_to" do
|
21
|
-
User.filter({'name_is_not_equal_to' => 'David'}).all.count.should == 4
|
22
|
-
end
|
23
|
-
|
24
|
-
it "filters on greater_than" do
|
25
|
-
User.filter({'salary_is_greater_than' => '80000'}).all.count.should == 1
|
26
|
-
end
|
27
|
-
|
28
|
-
it "filters on gt" do
|
29
|
-
User.filter({'salary_is_gt' => '70000'}).all.count.should == 2
|
30
|
-
end
|
31
|
-
|
32
|
-
it "filters on after" do
|
33
|
-
User.filter({'salary_is_after' => '9000'}).all.count.should == 4
|
34
|
-
end
|
35
|
-
|
36
|
-
it "filters on greater_than_or_equal_to" do
|
37
|
-
User.filter({'salary_is_greater_than_or_equal_to' => '80000'}).all.count.should == 2
|
38
|
-
end
|
39
|
-
|
40
|
-
it "filters on gtet" do
|
41
|
-
User.filter({'salary_is_gtet' => '80000'}).all.count.should == 2
|
42
|
-
end
|
43
|
-
|
44
|
-
it "filters on less_than" do
|
45
|
-
User.filter({'salary_is_less_than' => '80000'}).all.count.should == 3
|
46
|
-
end
|
47
|
-
|
48
|
-
it "filters on lt" do
|
49
|
-
User.filter({'salary_is_lt' => '70000'}).all.count.should == 3
|
50
|
-
end
|
51
|
-
|
52
|
-
it "filters on before" do
|
53
|
-
User.filter({'salary_is_before' => '10000'}).all.count.should == 1
|
54
|
-
end
|
55
|
-
|
56
|
-
it "filters on less_than_or_equal_to" do
|
57
|
-
User.filter({'salary_is_less_than_or_equal_to' => '80000'}).all.count.should == 4
|
58
|
-
end
|
59
|
-
|
60
|
-
it "filters on ltet" do
|
61
|
-
User.filter({'salary_is_ltet' => '80000'}).all.count.should == 4
|
62
|
-
end
|
63
|
-
|
64
|
-
it "filters on like" do
|
65
|
-
User.filter({'name_is_like' => 'Dav'}).all.count.should == 2
|
66
|
-
end
|
67
|
-
|
68
|
-
it "filters on in" do
|
69
|
-
User.filter({'id_is_in' => '1,2,3'}).all.count.should == 3
|
70
|
-
User.filter({'name_is_in' => 'Dave,Jamis'}).all.count.should == 2
|
71
|
-
end
|
72
|
-
|
73
|
-
it "fitlers on multiple expressions" do
|
74
|
-
User.filter({'name_is_in' => 'Dave,Jamis,David', 'salary_is_gt' => '10000'}).all.count.should == 2
|
75
|
-
end
|
76
|
-
|
77
|
-
it "handles booleans properly" do
|
78
|
-
User.filter({'bonus_is_equal_to' => 'true'}).all.count.should == 3
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
it "doesn't filter if passed invalid expression" do
|
83
|
-
User.filter({'name_is_garbage' => 'David'}).all.count.should == User.all.count
|
84
|
-
end
|
85
|
-
|
86
|
-
it "uses filterable fields" do
|
87
|
-
Post.filter({'title_is_equal_to' => 'Post 1 title'}).all.count.should == 1
|
88
|
-
Post.filter({'body_is_equal_to' => 'Post 1 body'}).all.count.should == 3
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
require 'active_record/version'
|
3
|
-
require 'active_record/fixtures'
|
4
|
-
require 'active_support/multibyte' # needed for Ruby 1.9.1
|
5
|
-
|
6
|
-
$query_count = $query_sql = nil
|
7
|
-
|
8
|
-
module ActiverecordTestConnector
|
9
|
-
extend self
|
10
|
-
|
11
|
-
attr_accessor :able_to_connect
|
12
|
-
attr_accessor :connected
|
13
|
-
|
14
|
-
FIXTURES_PATH = File.expand_path('../../fixtures', __FILE__)
|
15
|
-
|
16
|
-
# Set our defaults
|
17
|
-
self.connected = false
|
18
|
-
self.able_to_connect = true
|
19
|
-
|
20
|
-
def setup
|
21
|
-
unless self.connected || !self.able_to_connect
|
22
|
-
setup_connection
|
23
|
-
load_schema
|
24
|
-
add_load_path FIXTURES_PATH
|
25
|
-
self.connected = true
|
26
|
-
end
|
27
|
-
rescue Exception => e # errors from ActiveRecord setup
|
28
|
-
$stderr.puts "\nSkipping ActiveRecord tests: #{e}\n\n"
|
29
|
-
self.able_to_connect = false
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def add_load_path(path)
|
35
|
-
dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
|
36
|
-
dep.autoload_paths.unshift path
|
37
|
-
end
|
38
|
-
|
39
|
-
def setup_connection
|
40
|
-
ActiveRecord::Base.establish_connection(
|
41
|
-
:adapter => "sqlite3",
|
42
|
-
:database => ":memory:"
|
43
|
-
)
|
44
|
-
prepare ActiveRecord::Base.connection
|
45
|
-
end
|
46
|
-
|
47
|
-
def load_schema
|
48
|
-
ActiveRecord::Base.silence do
|
49
|
-
ActiveRecord::Migration.verbose = false
|
50
|
-
load File.join(FIXTURES_PATH, 'schema.rb')
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def prepare(conn)
|
55
|
-
class << conn
|
56
|
-
IGNORED_SQL = /^(?:PRAGMA|SELECT (?:currval|CAST|@@IDENTITY|@@ROWCOUNT)|SHOW FIELDS)\b/
|
57
|
-
|
58
|
-
def execute_with_counting(sql, name = nil, &block)
|
59
|
-
if $query_count and IGNORED_SQL !~ sql
|
60
|
-
$query_count += 1
|
61
|
-
$query_sql << sql
|
62
|
-
end
|
63
|
-
execute_without_counting(sql, name, &block)
|
64
|
-
end
|
65
|
-
|
66
|
-
alias_method_chain :execute, :counting
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
module FixtureSetup
|
71
|
-
def fixtures(*tables)
|
72
|
-
table_names = tables.map { |t| t.to_s }
|
73
|
-
|
74
|
-
fixtures = Fixtures.create_fixtures ActiverecordTestConnector::FIXTURES_PATH, table_names
|
75
|
-
@@loaded_fixtures = {}
|
76
|
-
@@fixture_cache = {}
|
77
|
-
|
78
|
-
unless fixtures.nil?
|
79
|
-
if fixtures.instance_of?(Fixtures)
|
80
|
-
@@loaded_fixtures[fixtures.table_name] = fixtures
|
81
|
-
else
|
82
|
-
fixtures.each { |f| @@loaded_fixtures[f.table_name] = f }
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
table_names.each do |table_name|
|
87
|
-
define_method(table_name) do |*fixtures|
|
88
|
-
@@fixture_cache[table_name] ||= {}
|
89
|
-
|
90
|
-
instances = fixtures.map do |fixture|
|
91
|
-
if @@loaded_fixtures[table_name][fixture.to_s]
|
92
|
-
@@fixture_cache[table_name][fixture] ||= @@loaded_fixtures[table_name][fixture.to_s].find
|
93
|
-
else
|
94
|
-
raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
instances.size == 1 ? instances.first : instances
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
data/spec/fixtures/post.rb
DELETED
data/spec/fixtures/posts.yml
DELETED
data/spec/fixtures/schema.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
ActiveRecord::Schema.define do
|
2
|
-
create_table "users", :force => true do |t|
|
3
|
-
t.column "name", :text
|
4
|
-
t.column "salary", :integer, :default => 70000
|
5
|
-
t.column "bonus", :boolean, :default => false
|
6
|
-
t.column "created_at", :datetime
|
7
|
-
t.column "updated_at", :datetime
|
8
|
-
end
|
9
|
-
|
10
|
-
create_table "posts", :force => true do |t|
|
11
|
-
t.column "title", :string
|
12
|
-
t.column "body", :text
|
13
|
-
t.column "published_at", :datetime
|
14
|
-
t.column "created_at", :datetime
|
15
|
-
t.column "updated_at", :datetime
|
16
|
-
end
|
17
|
-
end
|
data/spec/fixtures/user.rb
DELETED
data/spec/fixtures/users.yml
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
david:
|
2
|
-
id: 1
|
3
|
-
name: David
|
4
|
-
salary: 80000
|
5
|
-
bonus: true
|
6
|
-
|
7
|
-
dave:
|
8
|
-
id: 2
|
9
|
-
name: Dave
|
10
|
-
salary: 150000
|
11
|
-
bonus: true
|
12
|
-
|
13
|
-
poor_jamis:
|
14
|
-
id: 3
|
15
|
-
name: Jamis
|
16
|
-
salary: 9000
|
17
|
-
bonus: true
|
18
|
-
|
19
|
-
admin:
|
20
|
-
id: 12
|
21
|
-
name: admin
|
22
|
-
salary: 10000
|
23
|
-
bonus: false
|
24
|
-
|
25
|
-
goofy:
|
26
|
-
id: 13
|
27
|
-
name: Goofy
|
28
|
-
salary: 11000
|
29
|
-
bonus: false
|
30
|
-
|