hario 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +1 -1
- data/README.md +69 -2
- data/Rakefile +1 -1
- data/hario.gemspec +3 -2
- data/lib/hario/version.rb +1 -1
- data/test/filter_test.rb +20 -9
- data/test/fixtures.rb +19 -0
- data/test/hario_test_class.rb +7 -0
- data/test/models.rb +11 -0
- data/test/schema.rb +1 -1
- data/test/test_helper.rb +11 -2
- metadata +22 -6
- data/test/debug.log +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f50d53adcbb963fc89944af16e00690535a7c6ab
|
4
|
+
data.tar.gz: 45235182715628de22cdde31fa12a65556c5fcca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a2f840423cda8d11c7c702b2512646bbddbfe55022b4b173262f3fcd404b80cfd2adbbf6f0891dee7fbde5297889758b58cf6622d37eed5816d3b7036bc73a8
|
7
|
+
data.tar.gz: d0017e3e65ae1f31e4cbf46d56b12092f056fcddfbda1703f059ce2b2beaa5373ed75a71abee4225ba28675e4f4d8037639fcedf691f0620afff6829ba930443
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -18,15 +18,82 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
### Setup
|
22
|
+
|
23
|
+
Add `include Hario::Filterable` to your AR model to add the `search` method, for instance (we'll use these classes as examples throughout):
|
24
|
+
|
21
25
|
```ruby
|
22
|
-
def
|
26
|
+
def Brand < ActiveRecord::Base
|
23
27
|
include Hario::Filterable
|
28
|
+
|
29
|
+
has_many :products
|
30
|
+
end
|
31
|
+
|
32
|
+
def Product < ActiveRecord::Base
|
33
|
+
belongs_to :brand
|
34
|
+
belongs_to :category, class_name: "ProductCategory"
|
35
|
+
end
|
36
|
+
|
37
|
+
def ProductCategory < ActiveRecord::Base
|
38
|
+
has_many :products
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
Then in your BrandsController you can use the `search` method in your index action:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class BrandsController < ApplicationController
|
46
|
+
respond_to :json
|
47
|
+
|
48
|
+
def index
|
49
|
+
@brands = Brand.search(params[:filters])
|
50
|
+
|
51
|
+
respond_with(@brands)
|
52
|
+
end
|
24
53
|
end
|
25
54
|
```
|
26
55
|
|
56
|
+
### Filters
|
57
|
+
|
58
|
+
The format of `params[:filters]`'s keys is a dot-seperated chain of association(s) -> attribute -> operator, for instance if you wanted to return brands that have products of type "Shoe" you could add the filter:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
{ 'products.category.name.equals' => 'Shoe' }
|
62
|
+
```
|
63
|
+
|
64
|
+
Which would result in the SQL query something like:
|
65
|
+
|
66
|
+
```sql
|
67
|
+
SELECT "brands".* FROM "brands"
|
68
|
+
INNER JOIN "products" ON "products"."brand_id" = "brands"."id"
|
69
|
+
INNER JOIN "product_categories" ON "product_categories"."id" = "products"."category_id"
|
70
|
+
WHERE "product_categories"."name" = 'Shoe'
|
71
|
+
```
|
72
|
+
|
73
|
+
The available operators are:
|
74
|
+
|
75
|
+
- lt (less than)
|
76
|
+
- gt (greater than)
|
77
|
+
- lte (less than or equal)
|
78
|
+
- gte (greater than or equal)
|
79
|
+
- like (sql like)
|
80
|
+
- equals
|
81
|
+
|
82
|
+
## Todo
|
83
|
+
|
84
|
+
- Migrate our application-specific tests across to the gem
|
85
|
+
|
86
|
+
## Tests
|
87
|
+
|
88
|
+
To run tests:
|
89
|
+
|
90
|
+
```
|
91
|
+
rake test
|
92
|
+
```
|
93
|
+
|
27
94
|
## Contributing
|
28
95
|
|
29
|
-
1. Fork it ( https://github.com/
|
96
|
+
1. Fork it ( https://github.com/meritec/hario/fork )
|
30
97
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
98
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
99
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
data/hario.gemspec
CHANGED
@@ -17,9 +17,10 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_runtime_dependency "activerecord", "~>
|
20
|
+
spec.add_runtime_dependency "activerecord", "~> 4.0"
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.6"
|
23
23
|
spec.add_development_dependency "rake", "~> 0"
|
24
|
-
spec.add_development_dependency "sqlite3", "~>
|
24
|
+
spec.add_development_dependency "sqlite3", "~> 1.3.5"
|
25
|
+
spec.add_development_dependency "database_rewinder", "~> 0"
|
25
26
|
end
|
data/lib/hario/version.rb
CHANGED
data/test/filter_test.rb
CHANGED
@@ -1,19 +1,30 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
1
|
require_relative 'test_helper'
|
3
2
|
|
4
|
-
class FilterTest <
|
5
|
-
def
|
6
|
-
|
3
|
+
class FilterTest < Hario::Test
|
4
|
+
def test_simple_filter
|
5
|
+
filters = { 'name.equals' => "Adidas" }
|
6
|
+
brands = Brand.search(filters)
|
7
|
+
|
8
|
+
assert !brands.any?{ |b| b.name != "Adidas" }
|
7
9
|
end
|
8
10
|
|
9
|
-
def
|
10
|
-
|
11
|
+
def test_filter_with_join
|
12
|
+
filters = { 'products.name.equals' => "Gazelle OG" }
|
13
|
+
brands = Brand.search(filters)
|
14
|
+
|
15
|
+
assert_equal 1, brands.count
|
16
|
+
assert brands.first.name == "Adidas"
|
11
17
|
end
|
12
18
|
|
13
|
-
def
|
14
|
-
filters = {
|
19
|
+
def test_filter_with_double_join
|
20
|
+
filters = {
|
21
|
+
'products.category.name.equals' => "T-shirt",
|
22
|
+
'products.name.equals' => "Hamburg"
|
23
|
+
}
|
15
24
|
brands = Brand.search(filters)
|
25
|
+
brand = brands.first
|
16
26
|
|
17
|
-
|
27
|
+
assert_equal 1, brands.count
|
28
|
+
assert brand.name == "Adidas"
|
18
29
|
end
|
19
30
|
end
|
data/test/fixtures.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
@adidas = Brand.create!(name: "Adidas")
|
2
|
+
@shoe = ProductCategory.create!(name: "Shoe")
|
3
|
+
@tee = ProductCategory.create!(name: "T-shirt")
|
4
|
+
|
5
|
+
Product.create!(
|
6
|
+
brand_id: @adidas.id,
|
7
|
+
category_id: @shoe.id,
|
8
|
+
name: "Gazelle OG"
|
9
|
+
)
|
10
|
+
Product.create!(
|
11
|
+
brand_id: @adidas.id,
|
12
|
+
category_id: @shoe.id,
|
13
|
+
name: "Hamburg"
|
14
|
+
)
|
15
|
+
Product.create!(
|
16
|
+
brand_id: @adidas.id,
|
17
|
+
category_id: @tee.id,
|
18
|
+
name: "Hamburg"
|
19
|
+
)
|
data/test/models.rb
CHANGED
@@ -2,4 +2,15 @@ require 'hario'
|
|
2
2
|
|
3
3
|
class Brand < ActiveRecord::Base
|
4
4
|
include Hario::Filterable
|
5
|
+
|
6
|
+
has_many :products
|
7
|
+
end
|
8
|
+
|
9
|
+
class Product < ActiveRecord::Base
|
10
|
+
belongs_to :brand
|
11
|
+
belongs_to :category, class_name: "ProductCategory"
|
12
|
+
end
|
13
|
+
|
14
|
+
class ProductCategory < ActiveRecord::Base
|
15
|
+
has_many :products
|
5
16
|
end
|
data/test/schema.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,12 +1,21 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
2
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
3
|
|
4
|
+
require "logger"
|
4
5
|
require "active_record"
|
5
6
|
require "hario"
|
6
7
|
require "models"
|
7
8
|
|
8
9
|
ActiveRecord::Base.configurations = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))
|
9
10
|
ActiveRecord::Base.establish_connection(ENV['DB'] || :sqlite3)
|
10
|
-
load(File.join(File.dirname(__FILE__), "
|
11
|
+
load(File.join(File.dirname(__FILE__), "schema.rb"))
|
11
12
|
|
12
|
-
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__)
|
13
|
+
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
14
|
+
|
15
|
+
require "active_support"
|
16
|
+
require "database_rewinder"
|
17
|
+
|
18
|
+
DatabaseRewinder[ENV['DB'] || 'sqlite3']
|
19
|
+
DatabaseRewinder.clean_all
|
20
|
+
require "fixtures"
|
21
|
+
require "hario_test_class"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hario
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Campbell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -54,6 +54,20 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.3.5
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.3.5
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: database_rewinder
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
73
|
- - "~>"
|
@@ -85,8 +99,9 @@ files:
|
|
85
99
|
- lib/hario/behaviours/utils.rb
|
86
100
|
- lib/hario/version.rb
|
87
101
|
- test/database.yml
|
88
|
-
- test/debug.log
|
89
102
|
- test/filter_test.rb
|
103
|
+
- test/fixtures.rb
|
104
|
+
- test/hario_test_class.rb
|
90
105
|
- test/models.rb
|
91
106
|
- test/pluck_test.rb
|
92
107
|
- test/schema.rb
|
@@ -117,8 +132,9 @@ specification_version: 4
|
|
117
132
|
summary: Hario provides ActiveRecord filtering for Rails APIs.
|
118
133
|
test_files:
|
119
134
|
- test/database.yml
|
120
|
-
- test/debug.log
|
121
135
|
- test/filter_test.rb
|
136
|
+
- test/fixtures.rb
|
137
|
+
- test/hario_test_class.rb
|
122
138
|
- test/models.rb
|
123
139
|
- test/pluck_test.rb
|
124
140
|
- test/schema.rb
|
data/test/debug.log
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# Logfile created on 2015-06-25 23:10:26 +0100 by logger.rb/44203
|
2
|
-
D, [2015-06-25T23:10:27.058465 #74888] DEBUG -- : [1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
3
|
-
D, [2015-06-25T23:10:27.088174 #74888] DEBUG -- : [1m[35mSQL (0.2ms)[0m INSERT INTO "brands" ("name") VALUES (?) [["name", "Adidas"]]
|
4
|
-
D, [2015-06-25T23:10:27.104100 #74888] DEBUG -- : [1m[36m (0.2ms)[0m [1mcommit transaction[0m
|
5
|
-
D, [2015-06-25T23:10:27.127906 #74888] DEBUG -- : [1m[35mBrand Load (0.2ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."name" = ? [["name", "Adidas"]]
|
6
|
-
D, [2015-06-25T23:10:27.145343 #74888] DEBUG -- : [1m[36mSQL (15.2ms)[0m [1mDELETE FROM "brands"[0m
|
7
|
-
D, [2015-06-25T23:11:17.882386 #74899] DEBUG -- : [1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
8
|
-
D, [2015-06-25T23:11:17.912442 #74899] DEBUG -- : [1m[35mSQL (0.3ms)[0m INSERT INTO "brands" ("name") VALUES (?) [["name", "Adidas"]]
|
9
|
-
D, [2015-06-25T23:11:17.913038 #74899] DEBUG -- : [1m[36m (0.1ms)[0m [1mcommit transaction[0m
|
10
|
-
D, [2015-06-25T23:11:17.939361 #74899] DEBUG -- : [1m[35mBrand Load (0.2ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."name" = ? [["name", "Adidas"]]
|
11
|
-
D, [2015-06-25T23:11:17.941683 #74899] DEBUG -- : [1m[36mSQL (0.1ms)[0m [1mDELETE FROM "brands"[0m
|
12
|
-
D, [2015-06-25T23:11:27.792300 #74906] DEBUG -- : [1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
13
|
-
D, [2015-06-25T23:11:27.857213 #74906] DEBUG -- : [1m[35mSQL (0.2ms)[0m INSERT INTO "brands" ("name") VALUES (?) [["name", "Adidas"]]
|
14
|
-
D, [2015-06-25T23:11:27.857824 #74906] DEBUG -- : [1m[36m (0.1ms)[0m [1mcommit transaction[0m
|
15
|
-
D, [2015-06-25T23:11:27.885282 #74906] DEBUG -- : [1m[35mBrand Load (0.2ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."name" = ? [["name", "Adidas"]]
|
16
|
-
D, [2015-06-25T23:11:27.887789 #74906] DEBUG -- : [1m[36mSQL (0.1ms)[0m [1mDELETE FROM "brands"[0m
|