tagliani 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/README.md +35 -5
- data/lib/tagliani.rb +2 -1
- data/lib/tagliani/concerns/taggable.rb +12 -0
- data/lib/tagliani/concerns/taggable/tags.rb +36 -3
- data/lib/tagliani/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e389b80498b4068d2f0ff76a753238ebcfeadd0f8ebba7e54888c1c6c746d114
|
4
|
+
data.tar.gz: eb8003c90bef2fe855e01ab9acc07a5347ea4797d713d6fcd76b90b14b07b8fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d855d2f48083d6aedaf07e22afce9cd099aa00e0df9e8d56262b9f3370a6a8fd060419683a13f5f408bc063f4ca79f2fc5da372766677e7eb50610e60c265250
|
7
|
+
data.tar.gz: c7b3e17649598aa49e560c125ed4a15bf8a893bce402b4003acce78d51ea98c00b2bc9c75933b3e6d55dae5bfe7b37226f8915a309e917489be429de72df21b8
|
data/README.md
CHANGED
@@ -12,6 +12,30 @@ gem 'tagliani'
|
|
12
12
|
|
13
13
|
And run `bundle install` command.
|
14
14
|
|
15
|
+
### Requirements
|
16
|
+
|
17
|
+
* ActiveRecord
|
18
|
+
* redis-client
|
19
|
+
* elasticsearch
|
20
|
+
|
21
|
+
To run this gem it is required to have ActiveRecord.
|
22
|
+
|
23
|
+
## Configuration
|
24
|
+
|
25
|
+
In your rails app `config/initializers` create `tagliani.rb` with the following content
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Tagliani.configure do |config|
|
29
|
+
config.elasticsearch.url = "http://localhost:9200" # URL of your ElasticSearch service, by default set to this endpoint
|
30
|
+
config.elasticsearch.index = "tagliani_#{Rails.env}" # Index name
|
31
|
+
config.elasticsearch.refresh = true # false by default
|
32
|
+
config.elasticsearch.log = true # false by default
|
33
|
+
config.redis.url = "redis://localhost:6379/tagliani" # By default set to this endpoint
|
34
|
+
config.redis.queue = "tagliani" # By default set to this queue name
|
35
|
+
config.redis.length = 200 # By default set to 200. It is a queue length per bulk that is going to be sent to ElasticSearch
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
15
39
|
## Search
|
16
40
|
|
17
41
|
Let's say inside the Rails application you have a model with a name "Hashtag", that represents all the tags attached to the model "Tweet".
|
@@ -78,13 +102,19 @@ To index in bulks, simply execute:
|
|
78
102
|
Tagliani::Search::Index.bulk!
|
79
103
|
```
|
80
104
|
|
81
|
-
|
105
|
+
## Inherit tags from models
|
82
106
|
|
83
|
-
|
84
|
-
* redis-client
|
85
|
-
* elasticsearch
|
107
|
+
You can specify the models you want to inherit tags from by adding an option `:inherit` with a list of models in the `taggable` method.
|
86
108
|
|
87
|
-
|
109
|
+
```ruby
|
110
|
+
class Album < ActiveRecord::Base
|
111
|
+
has_many :songs
|
112
|
+
belongs_to :artist
|
113
|
+
belongs_to :producer
|
114
|
+
|
115
|
+
taggable inherit: %i[artist producer]
|
116
|
+
end
|
117
|
+
```
|
88
118
|
|
89
119
|
## Contributing
|
90
120
|
|
data/lib/tagliani.rb
CHANGED
@@ -27,9 +27,10 @@ module Tagliani
|
|
27
27
|
def taggable(options = {})
|
28
28
|
Models.tagged << to_s
|
29
29
|
|
30
|
-
class_attribute :_tag_kls, :_async
|
30
|
+
class_attribute :_tag_kls, :_async, :_inherit
|
31
31
|
|
32
32
|
self._tag_kls = options[:tag_kls] || "Tag"
|
33
|
+
self._inherit = options[:inherit]
|
33
34
|
self._async = options[:async]
|
34
35
|
|
35
36
|
class_eval do
|
@@ -5,9 +5,21 @@ module Tagliani
|
|
5
5
|
module Taggable
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
+
def self.included(base)
|
9
|
+
base.class_eval do
|
10
|
+
after_save :inherit_tags, if: proc { self.tags.root }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
8
14
|
def tags
|
9
15
|
Tags.new(self)
|
10
16
|
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def inherit_tags
|
21
|
+
tags.inherit
|
22
|
+
end
|
11
23
|
end
|
12
24
|
end
|
13
25
|
end
|
@@ -23,7 +23,7 @@ module Tagliani
|
|
23
23
|
end
|
24
24
|
|
25
25
|
@index.add!({
|
26
|
-
object_kls:
|
26
|
+
object_kls: parent_kls,
|
27
27
|
object_id: @parent.id,
|
28
28
|
created_at: @parent.try(:created_at),
|
29
29
|
tag_id: record.id,
|
@@ -36,11 +36,11 @@ module Tagliani
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def search(body: {}, where: nil)
|
39
|
-
body.deep_merge!({
|
39
|
+
body.deep_merge!({
|
40
40
|
query: {
|
41
41
|
bool: {
|
42
42
|
must: [
|
43
|
-
{ match: { object_kls:
|
43
|
+
{ match: { object_kls: parent_kls } },
|
44
44
|
{ term: { object_id: @parent.id } }
|
45
45
|
]
|
46
46
|
}
|
@@ -49,6 +49,39 @@ module Tagliani
|
|
49
49
|
|
50
50
|
Tagliani::Search.new(body: body, where: where).serialize(type: 'tag')
|
51
51
|
end
|
52
|
+
|
53
|
+
def inherit
|
54
|
+
root.each do |object|
|
55
|
+
next if object.nil?
|
56
|
+
|
57
|
+
self.class.new(object).each do |ref|
|
58
|
+
add(name: ref.name)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def root
|
64
|
+
klasses = []
|
65
|
+
|
66
|
+
if @parent._inherit.respond_to?(:each)
|
67
|
+
klasses = @parent._inherit
|
68
|
+
else
|
69
|
+
klasses << @parent._inherit
|
70
|
+
end
|
71
|
+
|
72
|
+
objects = klasses.flat_map do |method|
|
73
|
+
next if method.nil?
|
74
|
+
@parent.send(method)
|
75
|
+
end
|
76
|
+
|
77
|
+
objects.compact
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def parent_kls
|
83
|
+
@parent.class.base_class.to_s
|
84
|
+
end
|
52
85
|
end
|
53
86
|
end
|
54
87
|
end
|
data/lib/tagliani/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tagliani
|
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
|
- Aidan Rudkovskyi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|