needle_in_a_haystack 1.0.4 → 1.0.5

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: af267c9afbbd079edbdcb7ac293721697491a7e47dac4d19963ad9af83fbd60d
4
- data.tar.gz: e100eff5dfe19e038ffb8c6e8086b96eb1a034f87d3de1b66861ac91a05493b6
3
+ metadata.gz: d0d636da24994074ca2e0d360e34eb2cbb71b18fd5a7af9b5940357069c2c99c
4
+ data.tar.gz: 04c8d8ef066aede9ef052a1aa5a619959a3b40927d89699912e462a919ce91e5
5
5
  SHA512:
6
- metadata.gz: d2870fbe87246fe0e39b6818fcd2ebffb5767af2a39a1d0b3636c2f93b8fc069ebdda4a4b0c7f15ae4af878fa5507571db6d29f367213510857a8f64d0d5f214
7
- data.tar.gz: 6e49496b73f7d4bee5ee41e1bcc34e8e322eae66ac5af71419516c8532e7b79a2066827679dcce59680d3627cd9af4c2c968501cd9c14d07a299adb628fe72fa
6
+ metadata.gz: e46870a3a6ffa25cdd61dec219bbfcd520e27a991ad8e38ba63deb69082394226e50060dae345151cbc264590a9d88cf5337509d94694e942759fe9f96ec1a89
7
+ data.tar.gz: f6ec23b578c01343a147493622f9f4e32d16dee03f92359b9fba0815dda00b04f2084b8391a96d0cc673380f452603d8811120d4652a69c1c7ad5a6c6b6b53bf
@@ -0,0 +1,38 @@
1
+ class HaystackFactory < BaseFactory
2
+ attr_accessor :tag_strategy
3
+
4
+ def initialize(tag_strategy = DefaultTagStrategy.new)
5
+ super()
6
+ @tag_strategy = tag_strategy
7
+ end
8
+
9
+ def create_tag(name, description)
10
+ @tag_strategy.create_tag(name, description)
11
+ end
12
+
13
+ def create_tagging(tag, taggable)
14
+ HaystackTagging.create(haystack_tag: tag, taggable: taggable)
15
+ end
16
+
17
+ def find_or_create_tag(name, attributes = {})
18
+ tag = HaystackTag.find_or_create_by(name: name)
19
+ @tag_strategy.update_tag(tag, attributes)
20
+ tag
21
+ end
22
+
23
+ def create_tags(tag_hash, parent_tag = nil)
24
+ tag_hash.each do |name, data|
25
+ next if %w[description children].include?(name)
26
+
27
+ tag = find_or_create_tag(name, description: data["description"], haystack_marker: data["marker"])
28
+ tag.update(parent_tag_id: parent_tag&.id)
29
+
30
+ Rails.logger.info("Created tag: #{tag.name}, Parent: #{parent_tag&.name}, Parent ID: #{parent_tag&.id}")
31
+
32
+ if data["children"]
33
+ Rails.logger.info("Processing children for tag: #{tag.name}")
34
+ create_tags(data["children"], tag)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -38,27 +38,12 @@ class HaystackOntology < ApplicationRecord
38
38
  end
39
39
 
40
40
  def self.create_tags
41
- create_tag_hierarchy(tags)
42
- end
43
-
44
- def self.create_tag_hierarchy(tag_hash, parent_tag = nil)
45
- tag_hash.each do |name, data|
46
- next if %w[description children].include?(name)
47
-
48
- tag = HaystackTag.find_or_create_by(name: name.to_s)
49
- tag.update(description: data["description"], parent_tag_id: parent_tag&.id, haystack_marker: data["marker"])
50
-
51
- Rails.logger.info("Created tag: #{tag.name}, Parent: #{parent_tag&.name}, Parent ID: #{parent_tag&.id}")
52
-
53
- if data["children"]
54
- Rails.logger.info("Processing children for tag: #{tag.name}")
55
- create_tag_hierarchy(data["children"], tag)
56
- end
57
- end
41
+ factory = HaystackFactory.new(OntologyTagStrategy.new)
42
+ factory.create_tags(tags)
58
43
  end
59
44
 
60
45
  def self.find_or_create_tag(name)
61
- factory = HaystackFactory.new
46
+ factory = HaystackFactory.new(OntologyTagStrategy.new)
62
47
  tag_data = find_tag(name)
63
48
  return nil if tag_data.nil?
64
49
 
@@ -2,7 +2,7 @@ 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
- has_many :taggables, through: :haystack_taggings
5
+ has_many :taggables, through: :haystack_taggings, source: :taggable
6
6
 
7
7
  validates :name, presence: true, uniqueness: true
8
8
  validates :description, presence: true
@@ -12,6 +12,7 @@ class HaystackTag < BaseTag
12
12
  ancestors = []
13
13
  current = self
14
14
  while current.parent_tag
15
+ break if ancestors.include?(current.parent_tag) # Voorkom oneindige lus
15
16
  ancestors << current.parent_tag
16
17
  current = current.parent_tag
17
18
  end
@@ -25,7 +26,7 @@ class HaystackTag < BaseTag
25
26
  def self.find_by_path(path)
26
27
  keys = path.split(".")
27
28
  current = nil
28
- keys.each do |key|
29
+ keys.each do |key|
29
30
  current = current ? current.children.find_by(name: key) : find_by(name: key)
30
31
  return nil unless current
31
32
  end
@@ -52,9 +53,11 @@ class HaystackTag < BaseTag
52
53
  ancestors.size
53
54
  end
54
55
 
55
- def prevent_circular_reference
56
- return unless parent_tag == self || ancestors.include?(self)
56
+ private
57
57
 
58
- errors.add(:parent_tag, "cannot contain a circular reference")
58
+ def prevent_circular_reference
59
+ if parent_tag == self || ancestors.include?(self)
60
+ errors.add(:parent_tag, "kan geen circulaire referentie bevatten")
61
+ end
59
62
  end
60
63
  end
@@ -0,0 +1,9 @@
1
+ class DefaultTagStrategy < TagStrategy
2
+ def create_tag(name, description)
3
+ HaystackTag.create(name: name, description: description)
4
+ end
5
+
6
+ def update_tag(tag, attributes)
7
+ tag.update(attributes)
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class FindByTagsStrategy < QueryStrategy
2
+ def initialize(model, tags)
3
+ super()
4
+ @model = model
5
+ @tags = tags
6
+ end
7
+
8
+ def execute
9
+ @model.joins(:haystack_tags).where(haystack_tags: { id: @tags.map(&:id) })
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class FindPointsWithTagStrategy < QueryStrategy
2
+ def initialize(model, tag)
3
+ super()
4
+ @model = model
5
+ @tag = tag
6
+ end
7
+
8
+ def execute
9
+ @model.joins(:haystack_tags).where(haystack_tags: { id: @tag.id })
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ class FindPointsWithMultipleTagsStrategy < QueryStrategy
2
+ def initialize(model, tags)
3
+ super()
4
+ @model = model
5
+ @tags = tags
6
+ end
7
+
8
+ def execute
9
+ @model.joins(:haystack_tags)
10
+ .where(haystack_tags: { id: @tags.map(&:id) })
11
+ .group("#{@model.table_name}.id")
12
+ .having("COUNT(haystack_tags.id) = ?", @tags.size)
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ class OntologyTagStrategy < TagStrategy
2
+ def create_tag(name, description)
3
+ HaystackTag.find_or_create_by(name: name).tap do |tag|
4
+ tag.update(description: description)
5
+ end
6
+ end
7
+
8
+ def update_tag(tag, attributes)
9
+ tag.update(attributes)
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class QueryContext
2
+ def initialize(strategy)
3
+ @strategy = strategy
4
+ end
5
+
6
+ def execute
7
+ @strategy.execute
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class QueryStrategy
2
+ def execute
3
+ raise NotImplementedError, "Subclasses must implement the execute method"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class TagStrategy
2
+ def create_tag(name, description)
3
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
4
+ end
5
+
6
+ def update_tag(tag, attributes)
7
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
8
+ end
9
+ end
@@ -1 +1 @@
1
- VERSION = "1.0.4".freeze
1
+ VERSION = "1.0.5".freeze
@@ -4,12 +4,20 @@ 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
+ require "needle_in_a_haystack/factories/base_factory"
8
8
 
9
9
  require "needle_in_a_haystack/models/haystack_ontology"
10
10
  require "needle_in_a_haystack/models/haystack_tag"
11
11
  require "needle_in_a_haystack/models/haystack_tagging"
12
- require "needle_in_a_haystack/services/haystack_factory"
12
+ require "needle_in_a_haystack/factories/haystack_factory"
13
+ require "needle_in_a_haystack/strategies/query_context"
14
+ require "needle_in_a_haystack/strategies/query_strategy"
15
+ require "needle_in_a_haystack/strategies/find_by_tag_strategy"
16
+ require "needle_in_a_haystack/strategies/find_point_by_tag_strategy"
17
+ require "needle_in_a_haystack/strategies/find_points_with_multiple_tags_strategy"
18
+ require "needle_in_a_haystack/strategies/tag_strategy"
19
+ require "needle_in_a_haystack/strategies/default_tag_strategy"
20
+ require "needle_in_a_haystack/strategies/ontology_tag_strategy"
13
21
 
14
22
  module NeedleInAHaystack
15
23
  class << self
metadata CHANGED
@@ -1,14 +1,14 @@
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
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frans Verberne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-28 00:00:00.000000000 Z
11
+ date: 2024-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcrypt_pbkdf
@@ -277,16 +277,24 @@ 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
281
280
  - lib/needle_in_a_haystack/concerns/base_tag.rb
282
281
  - lib/needle_in_a_haystack/concerns/base_tagging.rb
283
282
  - lib/needle_in_a_haystack/concerns/taggable.rb
284
283
  - lib/needle_in_a_haystack/configuration.rb
285
284
  - lib/needle_in_a_haystack/engine.rb
285
+ - lib/needle_in_a_haystack/factories/base_factory.rb
286
+ - lib/needle_in_a_haystack/factories/haystack_factory.rb
286
287
  - lib/needle_in_a_haystack/models/haystack_ontology.rb
287
288
  - lib/needle_in_a_haystack/models/haystack_tag.rb
288
289
  - lib/needle_in_a_haystack/models/haystack_tagging.rb
289
- - lib/needle_in_a_haystack/services/haystack_factory.rb
290
+ - lib/needle_in_a_haystack/strategies/default_tag_strategy.rb
291
+ - lib/needle_in_a_haystack/strategies/find_by_tag_strategy.rb
292
+ - lib/needle_in_a_haystack/strategies/find_point_by_tag_strategy.rb
293
+ - lib/needle_in_a_haystack/strategies/find_points_with_multiple_tags_strategy.rb
294
+ - lib/needle_in_a_haystack/strategies/ontology_tag_strategy.rb
295
+ - lib/needle_in_a_haystack/strategies/query_context.rb
296
+ - lib/needle_in_a_haystack/strategies/query_strategy.rb
297
+ - lib/needle_in_a_haystack/strategies/tag_strategy.rb
290
298
  - lib/needle_in_a_haystack/version.rb
291
299
  - lib/tasks/location_create.rake
292
300
  - spec/factories/haystack_tags.rb
@@ -1,15 +0,0 @@
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