constrainable 0.3.1 → 0.4.0
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/README.markdown +9 -9
- data/lib/bsm/constrainable/field/base.rb +8 -8
- data/lib/bsm/constrainable/filter_set.rb +12 -14
- data/lib/bsm/constrainable/relation.rb +2 -2
- metadata +16 -16
data/README.markdown
CHANGED
@@ -18,9 +18,9 @@ In the simplest possible case you can define a few attributes and start filterin
|
|
18
18
|
end
|
19
19
|
|
20
20
|
# Examle request:
|
21
|
-
# GET /posts?where[
|
21
|
+
# GET /posts?where[id__not_eq]=1&where[author_id__eq]=2
|
22
22
|
# Params:
|
23
|
-
# "where" => { "
|
23
|
+
# "where" => { "id__not_eq" => "1", "author_id__eq" => "2" }
|
24
24
|
|
25
25
|
Post.constrain(params[:where])
|
26
26
|
# => SELECT posts.* FROM posts WHERE id != 1 AND author_id = 2
|
@@ -38,10 +38,10 @@ By default, only *eq* and *not_eq* operations are enabled, but there are plenty
|
|
38
38
|
|
39
39
|
# Example request (various notations are accepted):
|
40
40
|
# GET /posts?
|
41
|
-
# where[
|
42
|
-
# where[
|
43
|
-
# where[
|
44
|
-
# where[
|
41
|
+
# where[id__not_in]=1|2|3|4&
|
42
|
+
# where[author_id__in][]=1&
|
43
|
+
# where[author_id__in][]=2&
|
44
|
+
# where[created_at__between]=2011-01-01...2011-02-01
|
45
45
|
|
46
46
|
Want to *alias* a column? Try this:
|
47
47
|
|
@@ -53,7 +53,7 @@ Want to *alias* a column? Try this:
|
|
53
53
|
|
54
54
|
end
|
55
55
|
# Example request:
|
56
|
-
# GET /posts?where[
|
56
|
+
# GET /posts?where[created__lt]=2011-01-01
|
57
57
|
|
58
58
|
What about associations?
|
59
59
|
|
@@ -65,7 +65,7 @@ What about associations?
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
# Example request:
|
68
|
-
# GET /posts?where[
|
68
|
+
# GET /posts?where[author__matches]=%tom%
|
69
69
|
|
70
70
|
Post.constrain(params[:where])
|
71
71
|
# => SELECT posts.* FROM posts LEFT OUTER JOIN authors ON authors.id = posts.author_id WHERE authors.name LIKE '%tom%'
|
@@ -92,4 +92,4 @@ Integration with controllers, views & filter forms:
|
|
92
92
|
|
93
93
|
# In app/views/posts/index.html.haml
|
94
94
|
= form_for @filters, :as => :where do
|
95
|
-
= f.collection_select :
|
95
|
+
= f.collection_select :author_id__eq, Author.order('name'), :id, :name
|
@@ -35,15 +35,15 @@ class Bsm::Constrainable::Field::Base
|
|
35
35
|
end
|
36
36
|
|
37
37
|
# Merge params into a relation
|
38
|
-
def merge(relation,
|
39
|
-
|
40
|
-
|
41
|
-
next if operation.clause.nil?
|
38
|
+
def merge(relation, operator, value)
|
39
|
+
operator = operator.to_sym
|
40
|
+
return relation unless operators.include?(operator)
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
relation
|
42
|
+
operation = Bsm::Constrainable::Operation.new(operator, value, relation, self)
|
43
|
+
return relation if operation.clause.nil?
|
44
|
+
|
45
|
+
relation = relation.instance_eval(&scope) if scope
|
46
|
+
relation.where(operation.clause)
|
47
47
|
end
|
48
48
|
|
49
49
|
def convert(value)
|
@@ -6,41 +6,39 @@ class Bsm::Constrainable::FilterSet < Hash
|
|
6
6
|
def initialize(schema, params = {})
|
7
7
|
@schema = schema
|
8
8
|
|
9
|
-
normalized_hash(params).
|
10
|
-
|
9
|
+
normalized_hash(params).each do |key, value|
|
10
|
+
name, op = key.split('__')
|
11
|
+
update(key => value) if valid_operation?(name, op)
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
15
|
def merge(scoped)
|
15
|
-
each do |
|
16
|
+
each do |key, value|
|
17
|
+
name, op = key.split('__')
|
16
18
|
schema[name].each do |field|
|
17
|
-
scoped = field.merge(scoped,
|
19
|
+
scoped = field.merge(scoped, op, value)
|
18
20
|
end
|
19
21
|
end
|
20
22
|
scoped
|
21
23
|
end
|
22
24
|
|
23
25
|
def respond_to?(sym, *)
|
24
|
-
name,
|
25
|
-
super ||
|
26
|
+
name, op = sym.to_s.split('__')
|
27
|
+
super || valid_operation?(name, op)
|
26
28
|
end
|
27
29
|
|
28
30
|
private
|
29
|
-
NAME_OP = /\[(\w+)\]$/.freeze
|
30
31
|
|
31
32
|
def method_missing(sym, *args)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
self[name]
|
36
|
-
elsif valid_operator?(name, operator)
|
37
|
-
self[name].try(:[], operator.to_sym)
|
33
|
+
name, op = sym.to_s.split('__')
|
34
|
+
if valid_operation?(name, op)
|
35
|
+
self[sym.to_s]
|
38
36
|
else
|
39
37
|
super
|
40
38
|
end
|
41
39
|
end
|
42
40
|
|
43
|
-
def
|
41
|
+
def valid_operation?(name, operator)
|
44
42
|
return false unless operator.present? && schema.key?(name.to_s)
|
45
43
|
|
46
44
|
schema[name].any? do |field|
|
@@ -3,10 +3,10 @@ module Bsm::Constrainable::Relation
|
|
3
3
|
|
4
4
|
# Apply contraints. Example:
|
5
5
|
#
|
6
|
-
# Post.constrain("
|
6
|
+
# Post.constrain("created_at__lt" => "2011-01-01")
|
7
7
|
#
|
8
8
|
# # Use "custom" constraints
|
9
|
-
# Post.constrain(:custom, "
|
9
|
+
# Post.constrain(:custom, "created_at__lt" => "2011-01-01")
|
10
10
|
#
|
11
11
|
# # Combine it with relations & scopes
|
12
12
|
# Post.archived.includes(:author).constrain(params[:where]).paginate :page => 1
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: constrainable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dimitrij Denissenko
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-07-
|
18
|
+
date: 2011-07-25 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: abstract
|
@@ -73,23 +73,23 @@ extra_rdoc_files: []
|
|
73
73
|
|
74
74
|
files:
|
75
75
|
- README.markdown
|
76
|
-
- lib/bsm/constrainable
|
76
|
+
- lib/bsm/constrainable.rb
|
77
|
+
- lib/bsm/constrainable/relation.rb
|
77
78
|
- lib/bsm/constrainable/field/base.rb
|
78
|
-
- lib/bsm/constrainable/field.rb
|
79
|
+
- lib/bsm/constrainable/field/common.rb
|
80
|
+
- lib/bsm/constrainable/operation.rb
|
79
81
|
- lib/bsm/constrainable/filter_set.rb
|
80
|
-
- lib/bsm/constrainable/relation.rb
|
81
|
-
- lib/bsm/constrainable/registry.rb
|
82
|
-
- lib/bsm/constrainable/schema.rb
|
83
82
|
- lib/bsm/constrainable/util.rb
|
84
|
-
- lib/bsm/constrainable/
|
85
|
-
- lib/bsm/constrainable/
|
86
|
-
- lib/bsm/constrainable/operation/
|
83
|
+
- lib/bsm/constrainable/field.rb
|
84
|
+
- lib/bsm/constrainable/operation/between.rb
|
85
|
+
- lib/bsm/constrainable/operation/not_in.rb
|
87
86
|
- lib/bsm/constrainable/operation/in.rb
|
88
87
|
- lib/bsm/constrainable/operation/base.rb
|
89
|
-
- lib/bsm/constrainable/operation/
|
88
|
+
- lib/bsm/constrainable/operation/common.rb
|
90
89
|
- lib/bsm/constrainable/operation/collection.rb
|
91
|
-
- lib/bsm/constrainable/
|
92
|
-
- lib/bsm/constrainable.rb
|
90
|
+
- lib/bsm/constrainable/registry.rb
|
91
|
+
- lib/bsm/constrainable/model.rb
|
92
|
+
- lib/bsm/constrainable/schema.rb
|
93
93
|
homepage: https://github.com/bsm/constrainable
|
94
94
|
licenses: []
|
95
95
|
|