needle_in_a_haystack 1.0.3 → 1.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47f3b28b275f615fb0cbfabb20d76cf70ef0adbf63875c6a2aae2c0b19e27e39
4
- data.tar.gz: 9807a09a01410b9f299a268c828880df1e5fbbcf9cf6c7c16769b29da1fbb366
3
+ metadata.gz: af267c9afbbd079edbdcb7ac293721697491a7e47dac4d19963ad9af83fbd60d
4
+ data.tar.gz: e100eff5dfe19e038ffb8c6e8086b96eb1a034f87d3de1b66861ac91a05493b6
5
5
  SHA512:
6
- metadata.gz: a940a2d003014274c368e4daf453f1ab35711057eaec59a465d61ff3b319395806486fa95d3d19c7e4071f6c2e0e49cc6514079ce14b7921df722345ba9c651b
7
- data.tar.gz: 29b755d8403392d44cb9561d2a119b1c5b61bb3ebbb269ea4b7f78c241280e396c34c52bb4a1d895e5474d9cf90bae4a74945214f31803724d4734b318a28239
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
@@ -1,11 +1,31 @@
1
1
  class BaseTag < ApplicationRecord
2
2
  self.abstract_class = true
3
3
 
4
- def name
5
- raise NotImplementedError, "Subclasses must implement a name-method"
4
+ def full_path
5
+ raise NotImplementedError, "Subclasses must implement a full_path-method"
6
6
  end
7
7
 
8
- def description
9
- raise NotImplementedError, "Subclasses must implement a description-method"
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"
10
30
  end
11
31
  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
- create_tag_hierarchy(data["children"], tag) if data["children"]
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
- tag = HaystackTag.find_or_create_by(name: name)
62
- tag.update(description: tag_data["description"], haystack_marker: tag_data["marker"])
63
- Rails.logger.info("Tag aangemaakt of gevonden: #{tag.name}")
64
- tag
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
@@ -8,14 +8,6 @@ class HaystackTag < BaseTag
8
8
  validates :description, presence: true
9
9
  validate :prevent_circular_reference
10
10
 
11
- def name
12
- self[:name]
13
- end
14
-
15
- def description
16
- self[:description]
17
- end
18
-
19
11
  def ancestors
20
12
  ancestors = []
21
13
  current = self
@@ -26,23 +18,26 @@ class HaystackTag < BaseTag
26
18
  ancestors
27
19
  end
28
20
 
21
+ def full_path
22
+ ancestors.reverse.map(&:name).join(" > ") + " > " + name
23
+ end
24
+
29
25
  def self.find_by_path(path)
30
26
  keys = path.split(".")
31
27
  current = nil
32
28
  keys.each do |key|
33
- current = current ? current.child_tags.find_by(name: key) : find_by(name: key)
29
+ current = current ? current.children.find_by(name: key) : find_by(name: key)
34
30
  return nil unless current
35
31
  end
36
32
  current
37
33
  end
38
34
 
39
35
  def descendants
40
- children = child_tags
41
36
  children + children.flat_map(&:descendants)
42
37
  end
43
38
 
44
39
  def siblings
45
- parent_tag ? parent_tag.child_tags.where.not(id: id) : self.class.where(parent_tag_id: nil).where.not(id: id)
40
+ parent_tag ? parent_tag.children.where.not(id: id) : self.class.where(parent_tag_id: nil).where.not(id: id)
46
41
  end
47
42
 
48
43
  def root?
@@ -50,7 +45,7 @@ class HaystackTag < BaseTag
50
45
  end
51
46
 
52
47
  def leaf?
53
- child_tags.empty?
48
+ children.empty?
54
49
  end
55
50
 
56
51
  def depth
@@ -60,6 +55,6 @@ class HaystackTag < BaseTag
60
55
  def prevent_circular_reference
61
56
  return unless parent_tag == self || ancestors.include?(self)
62
57
 
63
- errors.add(:parent_tag, "kan geen circulaire referentie bevatten")
58
+ errors.add(:parent_tag, "cannot contain a circular reference")
64
59
  end
65
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.3".freeze
1
+ VERSION = "1.0.4".freeze
@@ -4,10 +4,12 @@ require "needle_in_a_haystack/application_record"
4
4
  require "needle_in_a_haystack/configuration"
5
5
  require "needle_in_a_haystack/concerns/base_tagging"
6
6
  require "needle_in_a_haystack/concerns/base_tag"
7
+ require "needle_in_a_haystack/concerns/base_factory"
7
8
 
8
9
  require "needle_in_a_haystack/models/haystack_ontology"
9
10
  require "needle_in_a_haystack/models/haystack_tag"
10
11
  require "needle_in_a_haystack/models/haystack_tagging"
12
+ require "needle_in_a_haystack/services/haystack_factory"
11
13
 
12
14
  module NeedleInAHaystack
13
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.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frans Verberne
@@ -277,6 +277,7 @@ files:
277
277
  - config/locales/en.yml
278
278
  - lib/needle_in_a_haystack.rb
279
279
  - lib/needle_in_a_haystack/application_record.rb
280
+ - lib/needle_in_a_haystack/concerns/base_factory.rb
280
281
  - lib/needle_in_a_haystack/concerns/base_tag.rb
281
282
  - lib/needle_in_a_haystack/concerns/base_tagging.rb
282
283
  - lib/needle_in_a_haystack/concerns/taggable.rb
@@ -285,6 +286,7 @@ files:
285
286
  - lib/needle_in_a_haystack/models/haystack_ontology.rb
286
287
  - lib/needle_in_a_haystack/models/haystack_tag.rb
287
288
  - lib/needle_in_a_haystack/models/haystack_tagging.rb
289
+ - lib/needle_in_a_haystack/services/haystack_factory.rb
288
290
  - lib/needle_in_a_haystack/version.rb
289
291
  - lib/tasks/location_create.rake
290
292
  - spec/factories/haystack_tags.rb