acts_as_explorable 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/Guardfile +1 -0
- data/README.md +127 -10
- data/lib/acts_as_explorable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca6b20cded2582f1fa6dca8e87fd30615b9c4512
|
4
|
+
data.tar.gz: dd8e130814dc5e150a70078f183768cde2283e9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4919b4d6661210538aeffa136d2457e55bc3e2cdf100f92a1fe04ba765b87b9821c87d6e2f1d3f07d146a0e6634a36d17e05501137ef469bf86048906ac619af
|
7
|
+
data.tar.gz: 89c32aba539deaaa7c69ddc6f5e438446d1fff6375e66a84c5139b3508cfce068de26207f66dbc3adc62012444826e86f322601cce8a30573d78fc0949e935a0
|
data/Guardfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# Acts As Explorable
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/hiasinho/acts_as_explorable.svg?branch=develop)](https://travis-ci.org/hiasinho/acts_as_explorable) [![Code Climate](https://codeclimate.com/github/hiasinho/acts_as_explorable/badges/gpa.svg)](https://codeclimate.com/github/hiasinho/acts_as_explorable) [![Inline docs](http://inch-ci.org/github/hiasinho/acts_as_explorable.svg?branch=develop)](http://inch-ci.org/github/hiasinho/acts_as_explorable)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/acts_as_explorable.svg)](http://badge.fury.io/rb/acts_as_explorable) [![Build Status](https://travis-ci.org/hiasinho/acts_as_explorable.svg?branch=develop)](https://travis-ci.org/hiasinho/acts_as_explorable) [![Code Climate](https://codeclimate.com/github/hiasinho/acts_as_explorable/badges/gpa.svg)](https://codeclimate.com/github/hiasinho/acts_as_explorable) [![Inline docs](http://inch-ci.org/github/hiasinho/acts_as_explorable.svg?branch=develop)](http://inch-ci.org/github/hiasinho/acts_as_explorable)
|
4
4
|
|
5
|
-
Acts As Explorable
|
5
|
+
Acts As Explorable extends ActiveRecord models with a `search` class method. This method can be fed with a quer like `Madrid in:city position:MF sort:club`. Which means *"Get all players who play in Madrid on the midfielder position and sort the results by the club names"*.
|
6
|
+
|
7
|
+
Acts As Explorable is a Ruby Gem specifically written for ActiveRecord models. It uses [Arel](https://github.com/rails/arel) to build query parts.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -13,19 +15,137 @@ Acts As Explorable is a Ruby Gem specifically written for ActiveRecord models.
|
|
13
15
|
|
14
16
|
### Install
|
15
17
|
|
16
|
-
THIS GEM IS NOT RELEASED YET. SO, THIS WON'T WORK!
|
17
|
-
|
18
18
|
Just add the following to your Gemfile.
|
19
19
|
|
20
20
|
```ruby
|
21
|
-
gem 'acts_as_explorable', '~> 0.
|
21
|
+
gem 'acts_as_explorable', '~> 0.1.1'
|
22
22
|
```
|
23
23
|
|
24
24
|
And follow that up with a ``bundle install``.
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
28
|
-
|
28
|
+
To enable the explorable plugin, just include the following lines into your model:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class Foo < ActiveRecord::Base
|
32
|
+
extend ActsAsExplorable
|
33
|
+
explorable
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Now you have the `.search` method on your model, which can be invoked like
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
Foo.search('Awesome in:bar')
|
41
|
+
```
|
42
|
+
|
43
|
+
A query string consists of two different types: The values (random words) and filters (starting with a word followed by a `:` and again a word -> `in:bar`). As of writing this, there are three different kinds of filters.
|
44
|
+
|
45
|
+
### Filters
|
46
|
+
|
47
|
+
A filter always consists of an element (the string before the `:`) and the *"modifiers"*, which can be appended with a `,`. Optional you can add a `-` for options (only used in `sort:` at the moment).
|
48
|
+
|
49
|
+
#### In
|
50
|
+
|
51
|
+
The `in:` filter makes use of the values given in the query string. It looks up these values in the given columns. For example the following query will look up all posts with the word *"Zlatan"* in the title or the body:
|
52
|
+
|
53
|
+
`Zlatan in:title,body`
|
54
|
+
|
55
|
+
You can do that by defining the `in:` filter on your model:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class Post < ActiveRecord::Base
|
59
|
+
extend ActsAsExplorable
|
60
|
+
explorable in: [:title, :body]
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
As you see, it is possible to look up the value in different columns. This generates some SQL like:
|
65
|
+
|
66
|
+
```sql
|
67
|
+
SELECT posts.* FROM posts
|
68
|
+
WHERE (posts.title ILIKE '%Zlatan%' OR posts.body ILIKE '%Zlatan%')
|
69
|
+
```
|
70
|
+
|
71
|
+
#### Sort
|
72
|
+
|
73
|
+
The `sort:` filter does (guess what?!) sorting. Just write `sort:` followed by the columns you want to sort. You can also append a `-desc` or `-asc` for the direction. So the query `sort:created_at-asc` sorts all posts be the `created_at` column in ascending direction.
|
74
|
+
|
75
|
+
Just define the `sort:` filter for your model:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class Player < ActiveRecord::Base
|
79
|
+
extend ActsAsExplorable
|
80
|
+
explorable sort: [:title, :created_at]
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
You can add more sorts just by addding the with a comma. `sort:created_at-asc,title-desc`. This produces the following SQL:
|
85
|
+
|
86
|
+
```sql
|
87
|
+
SELECT posts.* FROM posts
|
88
|
+
ORDER BY posts.created_at ASC, posts.title DESC
|
89
|
+
```
|
90
|
+
|
91
|
+
#### Dynamic Filters
|
92
|
+
|
93
|
+
This is where the *"magic"* happens. Say you have a model that represents a football player. And this player plays on a specific position. If you want to find all midfielders you could do `MF in:position` or you could use a dynamic filter.
|
94
|
+
|
95
|
+
You can define these filters on your model and assign options to them like this:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class Player < ActiveRecord::Base
|
99
|
+
extend ActsAsExplorable
|
100
|
+
explorable position: ['GK', 'DF', 'MF', 'FW']
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
Now, given a quer string `position:MF,FW` will give you all midfielders an forwards. Nice! This is the SQL:
|
105
|
+
|
106
|
+
```sql
|
107
|
+
SELECT players.* FROM players
|
108
|
+
WHERE (players.position IN ('MF','FW'))
|
109
|
+
```
|
110
|
+
|
111
|
+
#### Wrap Up
|
112
|
+
|
113
|
+
So, using all these examples, assuming you have this model:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
class Post < ActiveRecord::Base
|
117
|
+
extend ActsAsExplorable
|
118
|
+
explorable in: [:title, :body],
|
119
|
+
sort: [:title, :created_at],
|
120
|
+
state: ['draft', 'published', 'trash']
|
121
|
+
|
122
|
+
scope :older_than, -> (date) { where(%q{created_at <= ?}, date) }
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
126
|
+
You could query all posts in *draft* state with *"Zlatan"* in the title or body and sort the ascending by their creation date.
|
127
|
+
|
128
|
+
`Zlatan in:title,body sort:created_at-asc state:draft`
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
Post.search('Zlatan in:title,body sort:created_at-asc state:draft')
|
132
|
+
```
|
133
|
+
|
134
|
+
Now we have a SQL like this:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
SELECT posts.* FROM posts
|
138
|
+
WHERE (posts.title ILIKE '%Zlatan%' OR posts.body ILIKE '%Zlatan%')
|
139
|
+
AND (posts.state IN ('draft'))
|
140
|
+
ORDER BY
|
141
|
+
posts.created_at ASC
|
142
|
+
```
|
143
|
+
|
144
|
+
You can also append your scopes:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
Post.search('Zlatan in:title,body sort:created_at-asc state:draft').older_than(DateTime.now)
|
148
|
+
```
|
29
149
|
|
30
150
|
## Testing
|
31
151
|
|
@@ -42,11 +162,8 @@ Acts as explorable is released under the [MIT License](http://www.opensource.org
|
|
42
162
|
|
43
163
|
## TODO
|
44
164
|
|
45
|
-
### v0.1
|
46
|
-
- Fill this README file
|
47
|
-
- Release v0.0.1 to rubygems
|
48
|
-
|
49
165
|
###v0.x
|
50
166
|
- Add tests for postgres and mysql
|
51
167
|
- Query string validation helper for use in forms
|
52
168
|
- Use methods in addition to fields
|
169
|
+
- Use named scopes
|