make_model_searchable 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -5
- data/lib/make_model_searchable.rb +7 -7
- data/lib/make_model_searchable/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19fd45656ce822e0504e5a13573d18f0a8830b1d
|
4
|
+
data.tar.gz: d47a63dbe9b19e9dd1d12ad15160a0f097908d16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 542ec1f765ae022d7f40c1b493ba2af6357487e2c1045329f5a7f94ba567958bc2c07b927e941e87067739e4e3ab28b59c85eacf403016f89f1c5bc49eade9ae
|
7
|
+
data.tar.gz: e605d9ce316df5acd3fb478cdcde439e6cab75b15569218d2f98af2f608de9ba68b13363e95bfc1250e1d738d72afc2779d2547b0714ca30c24ca0155fa6505d
|
data/README.md
CHANGED
@@ -21,24 +21,52 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
## Usage
|
23
23
|
|
24
|
-
To add search functionality to a model, for example User add following line to your model.
|
24
|
+
###### To add search functionality to a model, for example User add following line to your model.
|
25
25
|
|
26
26
|
```ruby
|
27
27
|
class User < ActiveRecord::Base
|
28
|
-
searchable_attributes :
|
28
|
+
searchable_attributes :first_name, :last_name
|
29
29
|
end
|
30
30
|
```
|
31
31
|
Then use
|
32
32
|
```ruby
|
33
33
|
def index
|
34
|
-
|
34
|
+
User.search("something")
|
35
35
|
end
|
36
36
|
```
|
37
37
|
|
38
38
|
searchable_attributes line makes model searchable by adding search method to User model.
|
39
|
-
Specifying attributes by :
|
39
|
+
Specifying attributes by :first_name, :last_name enables searching in specied fields. It is optional to specify attributes, if no attributes are specified then all attributes are searched.
|
40
40
|
|
41
41
|
|
42
|
+
###### To enable searching from associations, you can mention asssociation name with attributes to be searched from that association as below:
|
43
|
+
|
44
|
+
Suppose our user has many posts, then in our model
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class User < ActiveRecord::Base
|
48
|
+
has_many :posts
|
49
|
+
searchable_attributes :name, posts: [:title]
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
or if you want to search from all text or string fields,
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class User < ActiveRecord::Base
|
57
|
+
has_many :posts
|
58
|
+
searchable_attributes :name, posts: []
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
|
63
|
+
Then use,
|
64
|
+
```ruby
|
65
|
+
def index
|
66
|
+
User.search("something")
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
42
70
|
## Development
|
43
71
|
|
44
72
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -47,7 +75,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
47
75
|
|
48
76
|
## Contributing
|
49
77
|
|
50
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/8parth/make_model_searchable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
51
79
|
|
52
80
|
|
53
81
|
## License
|
@@ -5,9 +5,9 @@ require 'models/searchable_field'
|
|
5
5
|
module MakeModelSearchable
|
6
6
|
|
7
7
|
def searchable_attributes(*options)
|
8
|
+
# self.connection
|
8
9
|
joined_options = options.select{ |option| option.is_a? (Hash) }
|
9
10
|
options = options.select{ |option| !option.is_a?(Hash) }
|
10
|
-
self.connection
|
11
11
|
setup_fields(options)
|
12
12
|
if joined_options.present?
|
13
13
|
setup_joined_fields(joined_options)
|
@@ -49,22 +49,22 @@ module MakeModelSearchable
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
if searchable_columns.present?
|
52
|
-
options.select
|
52
|
+
valid_fields = options.select{ |option| searchable_columns.include?(option.to_s) }
|
53
53
|
else
|
54
|
-
|
54
|
+
valid_fields = []
|
55
55
|
end
|
56
|
-
|
56
|
+
valid_fields
|
57
57
|
end
|
58
58
|
|
59
59
|
def get_valid_joined_fields(key, columns_array)
|
60
60
|
associated_model = self.reflect_on_association(key).klass
|
61
61
|
associated_model.connection
|
62
62
|
if columns_array.empty?
|
63
|
-
|
63
|
+
valid_joied_fields = associated_model.columns.select{ |col| col.type == :string or col.type == :text }
|
64
64
|
else
|
65
|
-
|
65
|
+
valid_joied_fields = associated_model.columns.select{ |col| (col.type == :string or col.type == :text) and columns_array.include?(col.name.to_sym) }
|
66
66
|
end
|
67
|
-
|
67
|
+
valid_joied_fields
|
68
68
|
end
|
69
69
|
|
70
70
|
module ClassMethods
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: make_model_searchable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 8parth
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -176,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
176
|
version: '0'
|
177
177
|
requirements: []
|
178
178
|
rubyforge_project:
|
179
|
-
rubygems_version: 2.
|
179
|
+
rubygems_version: 2.6.8
|
180
180
|
signing_key:
|
181
181
|
specification_version: 4
|
182
182
|
summary: Adds simple search functionality to your models.
|