active_filtr 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/active_filtr.gemspec +23 -0
- data/lib/active_filtr.rb +176 -0
- data/lib/active_filtr/version.rb +3 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3f4cea79004cb0c4ae081e92beea2838ccf12b32
|
4
|
+
data.tar.gz: ac9dc49213802e5022d26eee17aca7c997ebf04a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bcce1361268bdfebc24fa6ab491f80d7c78e48ef0c3d74a5e274c38c0ab9d5862a42b36ae9c924ebe00a95da290716ffc0849c3e3a0cdb1d4cd6c274b5741f22
|
7
|
+
data.tar.gz: 8f9851ad4e03e02a82ba92331b853316f40ae8c2a94f96442f043fb24a7202bf40a5b398065d2558804987b3e67ccff40c307b0bc9c76a5fa1f14203520ec7ac
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Jannes Köhler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ActiveFiltr
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'active_filtr'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install active_filtr
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_filtr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_filtr"
|
8
|
+
spec.version = ActiveFiltr::VERSION
|
9
|
+
spec.authors = ["Jannes Köhler"]
|
10
|
+
spec.email = ["janneskoehler@gmail.com"]
|
11
|
+
spec.description = %q{Add filters to active record.}
|
12
|
+
spec.summary = %q{Filters for active record.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/active_filtr.rb
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
require "active_filtr/version"
|
2
|
+
|
3
|
+
module ActiveFiltr
|
4
|
+
# Your code goes here...
|
5
|
+
end
|
6
|
+
|
7
|
+
module ActiveRecord
|
8
|
+
class Base
|
9
|
+
|
10
|
+
# todo: auslagern in eigenes gem
|
11
|
+
def self.filter(filter)
|
12
|
+
relation = self
|
13
|
+
# self.joins_for_filter(filter).each do |join_table|
|
14
|
+
# relation = relation.joins(join_table)
|
15
|
+
# end
|
16
|
+
return relation.where(*self.filter_to_sql(filter))
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.joins_for_filter(filter)
|
20
|
+
join_tables = []
|
21
|
+
filter.each do |attr, filter|
|
22
|
+
if assoc = self.reflect_on_association(attr) and assoc.macro != :belongs_to
|
23
|
+
join_tables << assoc.plural_name.to_sym # todo
|
24
|
+
end
|
25
|
+
end
|
26
|
+
return join_tables.uniq
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.filter_to_sql(filter)
|
30
|
+
if filter.is_a? Hash # todo
|
31
|
+
result = hash_filter_to_sql(filter)
|
32
|
+
elsif filter.respond_to? :each
|
33
|
+
result = filter_combination_to_sql(filter)
|
34
|
+
end # todo: else
|
35
|
+
return result
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.filter_combination_to_sql(filter)
|
39
|
+
if [:and, :or].include? filter.first
|
40
|
+
combination = filter.first
|
41
|
+
filter = filter[1..-1]
|
42
|
+
end
|
43
|
+
combination ||= :or
|
44
|
+
sqls = []
|
45
|
+
values = []
|
46
|
+
filter.each do |f|
|
47
|
+
f = self.filter_to_sql(f)
|
48
|
+
sqls << f.first
|
49
|
+
values += f[1..-1]
|
50
|
+
end
|
51
|
+
sql = '(' + sqls.join(" #{combination.to_s.upcase} ") + ')'
|
52
|
+
return [sql] + values
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.hash_filter_to_sql(filter)
|
56
|
+
sqls = []
|
57
|
+
values = []
|
58
|
+
filter.each do |attr, filter|
|
59
|
+
if self.reflect_on_association(attr)
|
60
|
+
result = self.assoc_filter_to_sql(attr, filter)
|
61
|
+
elsif column = self.columns_hash[attr.to_s]
|
62
|
+
result = case column.type
|
63
|
+
when :string
|
64
|
+
self.string_filter_to_sql(attr, filter)
|
65
|
+
when :integer, :decimal
|
66
|
+
self.number_filter_to_sql(attr, filter)
|
67
|
+
when :date
|
68
|
+
self.date_filter_to_sql(attr, filter)
|
69
|
+
when :boolean
|
70
|
+
self.boolean_filter_to_sql(attr, filter)
|
71
|
+
end
|
72
|
+
# todo: erweitern
|
73
|
+
else
|
74
|
+
raise "Not possible to set a filter for #{attr}"
|
75
|
+
end
|
76
|
+
sqls << result.first
|
77
|
+
values += result[1..-1]
|
78
|
+
end
|
79
|
+
sql = sqls.join(' AND ')
|
80
|
+
return [sql] + values
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.string_filter_to_sql(attr, filter, negation=false)
|
84
|
+
negation = negation ? 'NOT ' : ''
|
85
|
+
if filter.respond_to?(:each)
|
86
|
+
return self.value_combination_to_sql(attr, filter, :string)
|
87
|
+
else
|
88
|
+
sql = "#{negation}#{attr} = ?"
|
89
|
+
end
|
90
|
+
return [sql, filter]
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.number_filter_to_sql(attr, filter, negation=false)
|
94
|
+
negation = negation ? 'NOT ' : ''
|
95
|
+
if filter.respond_to?(:each)
|
96
|
+
return self.value_combination_to_sql(attr, filter, :number)
|
97
|
+
elsif filter.is_a? String
|
98
|
+
unless result = filter.match(/^([!<>=]*)(\d+\.?\d*)$/)
|
99
|
+
raise "invalid number filter: '#{filter}'"
|
100
|
+
end
|
101
|
+
connective = result[1].blank? ? '=' : result[1]
|
102
|
+
value = result[2].include?('.') ? result[2].to_f : result[2].to_i
|
103
|
+
sql = "#{negation}#{attr} #{connective} ?"
|
104
|
+
values = [value]
|
105
|
+
else
|
106
|
+
sql = "#{negation}#{attr} = ?"
|
107
|
+
values = [filter]
|
108
|
+
end
|
109
|
+
return [sql] + values
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.date_filter_to_sql(attr, filter, negation=false)
|
113
|
+
negation = negation ? 'NOT ' : ''
|
114
|
+
if filter.respond_to?(:each)
|
115
|
+
if filter.size != 2 # todo: value_combination einbauen
|
116
|
+
raise "invalid date filter: #{filter.inspect} 2 elements expected, #{filter.size} given"
|
117
|
+
end
|
118
|
+
sql = "#{negation}#{attr} BETWEEN ? AND ?"
|
119
|
+
values = filter
|
120
|
+
else
|
121
|
+
sql = "#{negation}#{attr} = ?"
|
122
|
+
values = [filter]
|
123
|
+
end
|
124
|
+
return [sql] + values
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.boolean_filter_to_sql(attr, filter, negation=false)
|
128
|
+
if filter.respond_to?(:each)
|
129
|
+
return self.value_combination_to_sql(attr, filter, :boolean)
|
130
|
+
end
|
131
|
+
negation = negation ? 'NOT ' : ''
|
132
|
+
return ["#{negation}#{attr} = ?", filter]
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.assoc_filter_to_sql(attr, filter, negation=false)
|
136
|
+
if filter.respond_to?(:each)
|
137
|
+
return self.value_combination_to_sql(attr, filter, :assoc)
|
138
|
+
end
|
139
|
+
negation = negation ? 'NOT ' : ''
|
140
|
+
assoc = self.reflect_on_association(attr)
|
141
|
+
if assoc.macro == :has_and_belongs_to_many
|
142
|
+
number_filter = self.number_filter_to_sql(assoc.association_foreign_key, filter)
|
143
|
+
values = number_filter[1..-1]
|
144
|
+
sql = "id #{negation}IN (SELECT #{assoc.foreign_key} FROM #{assoc.join_table} WHERE #{number_filter.first})"
|
145
|
+
end # todo: erweitern
|
146
|
+
return [sql] + values
|
147
|
+
end
|
148
|
+
|
149
|
+
def self.value_combination_to_sql(attr, filter, filter_type)
|
150
|
+
# todo: check if set is possible
|
151
|
+
if filter.first == :not
|
152
|
+
return self.send("#{filter_type}_filter_to_sql", attr, filter[1], true)
|
153
|
+
elsif [:and, :or].include? filter.first
|
154
|
+
combination = filter.first
|
155
|
+
filter = filter[1..-1]
|
156
|
+
end
|
157
|
+
combination ||= :or
|
158
|
+
sqls = []
|
159
|
+
values = []
|
160
|
+
filter.each do |f|
|
161
|
+
f = self.send("#{filter_type}_filter_to_sql", attr, f)
|
162
|
+
sqls << f.first
|
163
|
+
values += f[1..-1]
|
164
|
+
end
|
165
|
+
sql = '(' + sqls.join(" #{combination.to_s.upcase} ") + ')'
|
166
|
+
return [sql] + values
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
module ActiveRecord::Associations::Builder
|
173
|
+
class CollectionAssociation < Association
|
174
|
+
include AssociationMethods
|
175
|
+
end
|
176
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_filtr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jannes Köhler
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Add filters to active record.
|
42
|
+
email:
|
43
|
+
- janneskoehler@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- active_filtr.gemspec
|
54
|
+
- lib/active_filtr.rb
|
55
|
+
- lib/active_filtr/version.rb
|
56
|
+
homepage: ''
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.0.3
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Filters for active record.
|
80
|
+
test_files: []
|