needle_in_a_haystack 1.0.2 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/needle_in_a_haystack/concerns/base_factory.rb +13 -0
- data/lib/needle_in_a_haystack/concerns/base_tag.rb +31 -0
- data/lib/needle_in_a_haystack/concerns/base_tagging.rb +11 -0
- data/lib/{concerns → needle_in_a_haystack/concerns}/taggable.rb +0 -1
- data/lib/needle_in_a_haystack/{haystack_ontology.rb → models/haystack_ontology.rb} +12 -6
- data/lib/needle_in_a_haystack/{haystack_tag.rb → models/haystack_tag.rb} +10 -7
- data/lib/needle_in_a_haystack/models/haystack_tagging.rb +4 -0
- data/lib/needle_in_a_haystack/services/haystack_factory.rb +15 -0
- data/lib/needle_in_a_haystack/version.rb +1 -1
- data/lib/needle_in_a_haystack.rb +8 -2
- metadata +9 -5
- data/lib/needle_in_a_haystack/haystack_tagging.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af267c9afbbd079edbdcb7ac293721697491a7e47dac4d19963ad9af83fbd60d
|
4
|
+
data.tar.gz: e100eff5dfe19e038ffb8c6e8086b96eb1a034f87d3de1b66861ac91a05493b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2870fbe87246fe0e39b6818fcd2ebffb5767af2a39a1d0b3636c2f93b8fc069ebdda4a4b0c7f15ae4af878fa5507571db6d29f367213510857a8f64d0d5f214
|
7
|
+
data.tar.gz: 6e49496b73f7d4bee5ee41e1bcc34e8e322eae66ac5af71419516c8532e7b79a2066827679dcce59680d3627cd9af4c2c968501cd9c14d07a299adb628fe72fa
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class BaseFactory
|
2
|
+
def create_tag(name, description)
|
3
|
+
raise NotImplementedError, "Subclasses must implement a create_tag-method"
|
4
|
+
end
|
5
|
+
|
6
|
+
def create_tagging(tag, taggable)
|
7
|
+
raise NotImplementedError, "Subclasses must implement a create_tagging-method"
|
8
|
+
end
|
9
|
+
|
10
|
+
def find_or_create_tag(name, attributes = {})
|
11
|
+
raise NotImplementedError, "Subclasses must implement a find_or_create_tag-method"
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class BaseTag < ApplicationRecord
|
2
|
+
self.abstract_class = true
|
3
|
+
|
4
|
+
def full_path
|
5
|
+
raise NotImplementedError, "Subclasses must implement a full_path-method"
|
6
|
+
end
|
7
|
+
|
8
|
+
def prevent_circular_reference
|
9
|
+
raise NotImplementedError, "Subclasses must implement a prevent_circular_reference-method"
|
10
|
+
end
|
11
|
+
|
12
|
+
def ancestors
|
13
|
+
raise NotImplementedError, "Subclasses must implement an ancestors-method"
|
14
|
+
end
|
15
|
+
|
16
|
+
def descendants
|
17
|
+
raise NotImplementedError, "Subclasses must implement a descendants-method"
|
18
|
+
end
|
19
|
+
|
20
|
+
def siblings
|
21
|
+
raise NotImplementedError, "Subclasses must implement a siblings-method"
|
22
|
+
end
|
23
|
+
|
24
|
+
def root?
|
25
|
+
raise NotImplementedError, "Subclasses must implement a root?-method"
|
26
|
+
end
|
27
|
+
|
28
|
+
def leaf?
|
29
|
+
raise NotImplementedError, "Subclasses must implement a leaf?-method"
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class BaseTagging < ApplicationRecord
|
2
|
+
self.abstract_class = true
|
3
|
+
|
4
|
+
def tag
|
5
|
+
raise NotImplementedError, "Subclasses must implement a tag-method"
|
6
|
+
end
|
7
|
+
|
8
|
+
def taggable
|
9
|
+
raise NotImplementedError, "Subclasses must implement a taggable-method"
|
10
|
+
end
|
11
|
+
end
|
@@ -48,19 +48,25 @@ class HaystackOntology < ApplicationRecord
|
|
48
48
|
tag = HaystackTag.find_or_create_by(name: name.to_s)
|
49
49
|
tag.update(description: data["description"], parent_tag_id: parent_tag&.id, haystack_marker: data["marker"])
|
50
50
|
|
51
|
-
Rails.logger.info("Created tag: #{tag.name}, Parent: #{parent_tag&.name}")
|
51
|
+
Rails.logger.info("Created tag: #{tag.name}, Parent: #{parent_tag&.name}, Parent ID: #{parent_tag&.id}")
|
52
52
|
|
53
|
-
|
53
|
+
if data["children"]
|
54
|
+
Rails.logger.info("Processing children for tag: #{tag.name}")
|
55
|
+
create_tag_hierarchy(data["children"], tag)
|
56
|
+
end
|
54
57
|
end
|
55
58
|
end
|
56
59
|
|
57
60
|
def self.find_or_create_tag(name)
|
61
|
+
factory = HaystackFactory.new
|
58
62
|
tag_data = find_tag(name)
|
59
63
|
return nil if tag_data.nil?
|
60
64
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
+
attributes = { description: tag_data["description"], haystack_marker: tag_data["marker"] }
|
66
|
+
factory.find_or_create_tag(name, attributes)
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.import_full_ontology
|
70
|
+
create_tags
|
65
71
|
end
|
66
72
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
class HaystackTag <
|
1
|
+
class HaystackTag < BaseTag
|
2
2
|
belongs_to :parent_tag, class_name: "HaystackTag", optional: true
|
3
3
|
has_many :children, class_name: "HaystackTag", foreign_key: "parent_tag_id", dependent: :destroy, inverse_of: :parent_tag
|
4
4
|
has_many :haystack_taggings, dependent: :destroy
|
5
5
|
has_many :taggables, through: :haystack_taggings
|
6
6
|
|
7
|
-
validates :name, presence: true
|
7
|
+
validates :name, presence: true, uniqueness: true
|
8
8
|
validates :description, presence: true
|
9
9
|
validate :prevent_circular_reference
|
10
10
|
|
@@ -18,23 +18,26 @@ class HaystackTag < ApplicationRecord
|
|
18
18
|
ancestors
|
19
19
|
end
|
20
20
|
|
21
|
+
def full_path
|
22
|
+
ancestors.reverse.map(&:name).join(" > ") + " > " + name
|
23
|
+
end
|
24
|
+
|
21
25
|
def self.find_by_path(path)
|
22
26
|
keys = path.split(".")
|
23
27
|
current = nil
|
24
28
|
keys.each do |key|
|
25
|
-
current = current ? current.
|
29
|
+
current = current ? current.children.find_by(name: key) : find_by(name: key)
|
26
30
|
return nil unless current
|
27
31
|
end
|
28
32
|
current
|
29
33
|
end
|
30
34
|
|
31
35
|
def descendants
|
32
|
-
children = child_tags
|
33
36
|
children + children.flat_map(&:descendants)
|
34
37
|
end
|
35
38
|
|
36
39
|
def siblings
|
37
|
-
parent_tag ? parent_tag.
|
40
|
+
parent_tag ? parent_tag.children.where.not(id: id) : self.class.where(parent_tag_id: nil).where.not(id: id)
|
38
41
|
end
|
39
42
|
|
40
43
|
def root?
|
@@ -42,7 +45,7 @@ class HaystackTag < ApplicationRecord
|
|
42
45
|
end
|
43
46
|
|
44
47
|
def leaf?
|
45
|
-
|
48
|
+
children.empty?
|
46
49
|
end
|
47
50
|
|
48
51
|
def depth
|
@@ -52,6 +55,6 @@ class HaystackTag < ApplicationRecord
|
|
52
55
|
def prevent_circular_reference
|
53
56
|
return unless parent_tag == self || ancestors.include?(self)
|
54
57
|
|
55
|
-
errors.add(:parent_tag, "
|
58
|
+
errors.add(:parent_tag, "cannot contain a circular reference")
|
56
59
|
end
|
57
60
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class HaystackFactory < BaseFactory
|
2
|
+
def create_tag(name, description)
|
3
|
+
HaystackTag.create(name: name, description: description)
|
4
|
+
end
|
5
|
+
|
6
|
+
def create_tagging(tag, taggable)
|
7
|
+
HaystackTagging.create(haystack_tag: tag, taggable: taggable)
|
8
|
+
end
|
9
|
+
|
10
|
+
def find_or_create_tag(name, attributes = {})
|
11
|
+
tag = HaystackTag.find_or_create_by(name: name)
|
12
|
+
tag.update(attributes)
|
13
|
+
tag
|
14
|
+
end
|
15
|
+
end
|
@@ -1 +1 @@
|
|
1
|
-
VERSION = "1.0.
|
1
|
+
VERSION = "1.0.4".freeze
|
data/lib/needle_in_a_haystack.rb
CHANGED
@@ -2,8 +2,14 @@ require "needle_in_a_haystack/version"
|
|
2
2
|
require "needle_in_a_haystack/engine"
|
3
3
|
require "needle_in_a_haystack/application_record"
|
4
4
|
require "needle_in_a_haystack/configuration"
|
5
|
-
require "needle_in_a_haystack/
|
6
|
-
require "needle_in_a_haystack/
|
5
|
+
require "needle_in_a_haystack/concerns/base_tagging"
|
6
|
+
require "needle_in_a_haystack/concerns/base_tag"
|
7
|
+
require "needle_in_a_haystack/concerns/base_factory"
|
8
|
+
|
9
|
+
require "needle_in_a_haystack/models/haystack_ontology"
|
10
|
+
require "needle_in_a_haystack/models/haystack_tag"
|
11
|
+
require "needle_in_a_haystack/models/haystack_tagging"
|
12
|
+
require "needle_in_a_haystack/services/haystack_factory"
|
7
13
|
|
8
14
|
module NeedleInAHaystack
|
9
15
|
class << self
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: needle_in_a_haystack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frans Verberne
|
@@ -275,14 +275,18 @@ files:
|
|
275
275
|
- config/initializers/redis.rb
|
276
276
|
- config/initializers/wrap_parameters.rb
|
277
277
|
- config/locales/en.yml
|
278
|
-
- lib/concerns/taggable.rb
|
279
278
|
- lib/needle_in_a_haystack.rb
|
280
279
|
- lib/needle_in_a_haystack/application_record.rb
|
280
|
+
- lib/needle_in_a_haystack/concerns/base_factory.rb
|
281
|
+
- lib/needle_in_a_haystack/concerns/base_tag.rb
|
282
|
+
- lib/needle_in_a_haystack/concerns/base_tagging.rb
|
283
|
+
- lib/needle_in_a_haystack/concerns/taggable.rb
|
281
284
|
- lib/needle_in_a_haystack/configuration.rb
|
282
285
|
- lib/needle_in_a_haystack/engine.rb
|
283
|
-
- lib/needle_in_a_haystack/haystack_ontology.rb
|
284
|
-
- lib/needle_in_a_haystack/haystack_tag.rb
|
285
|
-
- lib/needle_in_a_haystack/haystack_tagging.rb
|
286
|
+
- lib/needle_in_a_haystack/models/haystack_ontology.rb
|
287
|
+
- lib/needle_in_a_haystack/models/haystack_tag.rb
|
288
|
+
- lib/needle_in_a_haystack/models/haystack_tagging.rb
|
289
|
+
- lib/needle_in_a_haystack/services/haystack_factory.rb
|
286
290
|
- lib/needle_in_a_haystack/version.rb
|
287
291
|
- lib/tasks/location_create.rake
|
288
292
|
- spec/factories/haystack_tags.rb
|