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 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[id][not_eq]=1&where[author_id][eq]=2
21
+ # GET /posts?where[id__not_eq]=1&where[author_id__eq]=2
22
22
  # Params:
23
- # "where" => { "id" => { "not_eq" => "1" }, "author_id" => { "eq" => "2" } }
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[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
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[created][lt]=2011-01-01
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[author][matches]=%tom%
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 :"author_id[eq]", Author.order('name'), :id, :name
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, params)
39
- params.slice(*operators).each do |operator, value|
40
- operation = Bsm::Constrainable::Operation.new(operator, value, relation, self)
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
- relation = relation.instance_eval(&scope) if scope
44
- relation = relation.where(operation.clause)
45
- end
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).slice(*schema.keys).each do |name, part|
10
- update name => part.symbolize_keys if part.is_a?(Hash)
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 |name, part|
16
+ each do |key, value|
17
+ name, op = key.split('__')
16
18
  schema[name].each do |field|
17
- scoped = field.merge(scoped, part)
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, operator = sym.to_s.sub(NAME_OP, ''), $1
25
- super || (operator.nil? && schema.key?(name)) || valid_operator?(name, operator)
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
- name, operator = sym.to_s.sub(NAME_OP, ''), $1
33
-
34
- if (operator.nil? && schema.key?(name))
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 valid_operator?(name, operator)
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("created_at" => { "lt" => "2011-01-01" }})
6
+ # Post.constrain("created_at__lt" => "2011-01-01")
7
7
  #
8
8
  # # Use "custom" constraints
9
- # Post.constrain(:custom, "created_at" => { "lt" => "2011-01-01" }})
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: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
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-22 00:00:00 Z
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/field/common.rb
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/operation.rb
85
- - lib/bsm/constrainable/model.rb
86
- - lib/bsm/constrainable/operation/common.rb
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/between.rb
88
+ - lib/bsm/constrainable/operation/common.rb
90
89
  - lib/bsm/constrainable/operation/collection.rb
91
- - lib/bsm/constrainable/operation/not_in.rb
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