needle_in_a_haystack 1.0.0 → 1.0.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/config/initializers/config.rb +3 -3
- data/config/initializers/redis.rb +1 -1
- data/config/locales/en.yml +1 -32
- data/lib/needle_in_a_haystack/application_record.rb +2 -4
- data/lib/needle_in_a_haystack/configuration.rb +12 -6
- data/lib/needle_in_a_haystack/engine.rb +5 -8
- data/lib/needle_in_a_haystack/haystack_ontology.rb +45 -47
- data/lib/needle_in_a_haystack/haystack_tag.rb +47 -53
- data/lib/needle_in_a_haystack/haystack_tagging.rb +3 -7
- data/lib/needle_in_a_haystack/version.rb +1 -3
- metadata +2 -8
- data/config/initializers/application_controller_renderer.rb +0 -8
- data/config/initializers/backtrace_silencers.rb +0 -7
- data/config/initializers/cors.rb +0 -16
- data/config/initializers/inflections.rb +0 -16
- data/config/initializers/lograge.rb +0 -0
- data/config/initializers/mime_types.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: 7e365c25451da6fb9d1ab929ece76bfea7e5753544818eb5e8650642ed5d10b6
|
4
|
+
data.tar.gz: d926c7e2ad775dc46d5c38a48b352dfd1ffd369196f3c3aab7aabe40c915d142
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cfd26955113913e7ef3f5f24435485f45d83dac53f54b92d10cc7199f6c48358c1aa8072cdd911bc4e700acbd184ccdab808a987775addd438654e88218c363
|
7
|
+
data.tar.gz: 20f66d4f920165380f95fd2f981207131034f96a14b86d6019e62f9c77cc83127669bae046ef02c599a7dd210c716d9a6510582d4f6157fb5b34317e0bd40796
|
@@ -1,2 +1,2 @@
|
|
1
1
|
require "connection_pool"
|
2
|
-
Redis::Objects.redis = ConnectionPool.new(size: 8, timeout: 5) { Redis.new(path: "/tmp/redis.sock")}
|
2
|
+
Redis::Objects.redis = ConnectionPool.new(size: 8, timeout: 5) { Redis.new(path: "/tmp/redis.sock") }
|
data/config/locales/en.yml
CHANGED
@@ -1,33 +1,2 @@
|
|
1
|
-
# Files in the config/locales directory are used for internationalization
|
2
|
-
# and are automatically loaded by Rails. If you want to use locales other
|
3
|
-
# than English, add the necessary files in this directory.
|
4
|
-
#
|
5
|
-
# To use the locales, use `I18n.t`:
|
6
|
-
#
|
7
|
-
# I18n.t 'hello'
|
8
|
-
#
|
9
|
-
# In views, this is aliased to just `t`:
|
10
|
-
#
|
11
|
-
# <%= t('hello') %>
|
12
|
-
#
|
13
|
-
# To use a different locale, set it with `I18n.locale`:
|
14
|
-
#
|
15
|
-
# I18n.locale = :es
|
16
|
-
#
|
17
|
-
# This would use the information in config/locales/es.yml.
|
18
|
-
#
|
19
|
-
# The following keys must be escaped otherwise they will not be retrieved by
|
20
|
-
# the default I18n backend:
|
21
|
-
#
|
22
|
-
# true, false, on, off, yes, no
|
23
|
-
#
|
24
|
-
# Instead, surround them with single quotes.
|
25
|
-
#
|
26
|
-
# en:
|
27
|
-
# 'true': 'foo'
|
28
|
-
#
|
29
|
-
# To learn more, please read the Rails Internationalization guide
|
30
|
-
# available at https://guides.rubyonrails.org/i18n.html.
|
31
|
-
|
32
1
|
en:
|
33
|
-
|
2
|
+
circular_reference: Circular reference detected
|
@@ -1,9 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
attr_accessor :taggable_models
|
1
|
+
class Configuration
|
2
|
+
attr_accessor :taggable_models
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
end
|
4
|
+
def initialize
|
5
|
+
@taggable_models = []
|
8
6
|
end
|
9
7
|
end
|
8
|
+
|
9
|
+
def self.configuration
|
10
|
+
@configuration ||= Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.configure
|
14
|
+
yield(configuration)
|
15
|
+
end
|
@@ -1,11 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
isolate_namespace NeedleInAHaystack
|
4
|
-
end
|
1
|
+
class Engine < ::Rails::Engine
|
2
|
+
end
|
5
3
|
|
6
|
-
|
4
|
+
raise "Gem 'needle_in_a_haystack' is not loaded" unless Gem.loaded_specs["needle_in_a_haystack"]
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
end
|
6
|
+
Gem.loaded_specs["needle_in_a_haystack"].dependencies.each do |d|
|
7
|
+
require d.name
|
11
8
|
end
|
@@ -1,68 +1,66 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.find_tag(path)
|
8
|
-
return nil if path.nil?
|
9
|
-
|
10
|
-
path.include?(".") ? find_tag_in_hierarchy(tags, path.split(".").last) : find_tag_in_hierarchy(tags, path)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.find_tag_in_hierarchy(current_hash, target_key, path = [])
|
14
|
-
return nil unless current_hash.is_a?(Hash)
|
1
|
+
class HaystackOntology < ApplicationRecord
|
2
|
+
def self.tags
|
3
|
+
@tags ||= YAML.load_file(Rails.root.join("config/haystack_ontology.yml"))
|
4
|
+
end
|
15
5
|
|
16
|
-
|
17
|
-
|
6
|
+
def self.find_tag(path)
|
7
|
+
return nil if path.nil?
|
18
8
|
|
19
|
-
|
20
|
-
|
21
|
-
children.each do |key, value|
|
22
|
-
new_path = path + [key]
|
23
|
-
return value.merge("path" => new_path.join(".")) if key == target_key
|
9
|
+
path.include?(".") ? find_tag_in_hierarchy(tags, path.split(".").last) : find_tag_in_hierarchy(tags, path)
|
10
|
+
end
|
24
11
|
|
25
|
-
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
12
|
+
def self.find_tag_in_hierarchy(current_hash, target_key, path = [])
|
13
|
+
return nil unless current_hash.is_a?(Hash)
|
29
14
|
|
30
|
-
|
31
|
-
|
15
|
+
result = current_hash[target_key]
|
16
|
+
return result.is_a?(Hash) ? result.merge("path" => path.push(target_key).join(".")) : result if result
|
32
17
|
|
18
|
+
children = current_hash["children"]
|
19
|
+
if children.is_a?(Hash)
|
20
|
+
children.each do |key, value|
|
33
21
|
new_path = path + [key]
|
22
|
+
return value.merge("path" => new_path.join(".")) if key == target_key
|
23
|
+
|
34
24
|
result = find_tag_in_hierarchy(value, target_key, new_path)
|
35
25
|
return result if result
|
36
26
|
end
|
37
|
-
|
38
|
-
nil
|
39
27
|
end
|
40
28
|
|
41
|
-
|
42
|
-
|
29
|
+
current_hash.each do |key, value|
|
30
|
+
next if %w[description children].include?(key) || !value.is_a?(Hash)
|
31
|
+
|
32
|
+
new_path = path + [key]
|
33
|
+
result = find_tag_in_hierarchy(value, target_key, new_path)
|
34
|
+
return result if result
|
43
35
|
end
|
44
36
|
|
45
|
-
|
46
|
-
|
47
|
-
next if %w[description children].include?(name)
|
37
|
+
nil
|
38
|
+
end
|
48
39
|
|
49
|
-
|
50
|
-
|
40
|
+
def self.create_tags
|
41
|
+
create_tag_hierarchy(tags)
|
42
|
+
end
|
51
43
|
|
52
|
-
|
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)
|
53
47
|
|
54
|
-
|
55
|
-
|
56
|
-
end
|
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"])
|
57
50
|
|
58
|
-
|
59
|
-
tag_data = find_tag(name)
|
60
|
-
return nil if tag_data.nil?
|
51
|
+
Rails.logger.info("Created tag: #{tag.name}, Parent: #{parent_tag&.name}")
|
61
52
|
|
62
|
-
tag
|
63
|
-
tag.update(description: tag_data["description"], haystack_marker: tag_data["marker"])
|
64
|
-
Rails.logger.info("Tag aangemaakt of gevonden: #{tag.name}")
|
65
|
-
tag
|
53
|
+
create_tag_hierarchy(data["children"], tag) if data["children"]
|
66
54
|
end
|
67
55
|
end
|
56
|
+
|
57
|
+
def self.find_or_create_tag(name)
|
58
|
+
tag_data = find_tag(name)
|
59
|
+
return nil if tag_data.nil?
|
60
|
+
|
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
|
+
end
|
68
66
|
end
|
@@ -1,69 +1,63 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
class HaystackTag < ApplicationRecord
|
2
|
+
belongs_to :parent_tag, class_name: "NeedleInAHaystack::HaystackTag", optional: true
|
3
|
+
has_many :children, class_name: "NeedleInAHaystack::HaystackTag", foreign_key: "parent_tag_id", dependent: :destroy, inverse_of: :parent_tag
|
4
|
+
has_many :haystack_taggings, dependent: :destroy
|
5
|
+
has_many :taggables, through: :haystack_taggings
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
has_many :taggables, through: :haystack_taggings
|
9
|
-
|
10
|
-
if NeedleInAHaystack.respond_to?(:configuration) && NeedleInAHaystack.configuration.taggable_models
|
11
|
-
NeedleInAHaystack.configuration.taggable_models.each do |model|
|
12
|
-
has_many "taggable_#{model.name.underscore.pluralize}".to_sym, through: :haystack_taggings, source: :taggable, source_type: model.name
|
13
|
-
end
|
7
|
+
if NeedleInAHaystack.respond_to?(:configuration) && NeedleInAHaystack.configuration&.taggable_models
|
8
|
+
NeedleInAHaystack.configuration.taggable_models.each do |model|
|
9
|
+
has_many "taggable_#{model.name.underscore.pluralize}".to_sym, through: :haystack_taggings, source: :taggable, source_type: model.name
|
14
10
|
end
|
11
|
+
end
|
15
12
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
def ancestors
|
21
|
-
ancestors = []
|
22
|
-
current = self
|
23
|
-
while current.parent_tag
|
24
|
-
ancestors << current.parent_tag
|
25
|
-
current = current.parent_tag
|
26
|
-
end
|
27
|
-
ancestors
|
28
|
-
end
|
13
|
+
validates :name, presence: true
|
14
|
+
validates :description, presence: true
|
15
|
+
validate :prevent_circular_reference
|
29
16
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
37
|
-
current
|
17
|
+
def ancestors
|
18
|
+
ancestors = []
|
19
|
+
current_tag = self
|
20
|
+
while current_tag.parent_tag
|
21
|
+
ancestors << current_tag.parent_tag
|
22
|
+
current_tag = current_tag.parent_tag
|
38
23
|
end
|
24
|
+
ancestors
|
25
|
+
end
|
39
26
|
|
40
|
-
|
41
|
-
|
42
|
-
|
27
|
+
def self.find_by_path(path)
|
28
|
+
keys = path.split(".")
|
29
|
+
current = nil
|
30
|
+
keys.each do |key|
|
31
|
+
current = current ? current.child_tags.find_by(name: key) : find_by(name: key)
|
32
|
+
return nil unless current
|
43
33
|
end
|
34
|
+
current
|
35
|
+
end
|
44
36
|
|
45
|
-
|
46
|
-
|
47
|
-
|
37
|
+
def descendants
|
38
|
+
children = child_tags
|
39
|
+
children + children.flat_map(&:descendants)
|
40
|
+
end
|
48
41
|
|
49
|
-
|
50
|
-
|
51
|
-
|
42
|
+
def siblings
|
43
|
+
parent_tag ? parent_tag.child_tags.where.not(id: id) : self.class.where(parent_tag_id: nil).where.not(id: id)
|
44
|
+
end
|
52
45
|
|
53
|
-
|
54
|
-
|
55
|
-
|
46
|
+
def root?
|
47
|
+
parent_tag.nil?
|
48
|
+
end
|
56
49
|
|
57
|
-
|
58
|
-
|
59
|
-
|
50
|
+
def leaf?
|
51
|
+
child_tags.empty?
|
52
|
+
end
|
60
53
|
|
61
|
-
|
54
|
+
def depth
|
55
|
+
ancestors.size
|
56
|
+
end
|
62
57
|
|
63
|
-
|
64
|
-
|
58
|
+
def prevent_circular_reference
|
59
|
+
return unless parent_tag == self || ancestors.include?(self)
|
65
60
|
|
66
|
-
|
67
|
-
end
|
61
|
+
errors.add(:parent_tag, "kan geen circulaire referentie bevatten")
|
68
62
|
end
|
69
63
|
end
|
@@ -1,8 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
belongs_to :haystack_tag
|
6
|
-
belongs_to :taggable, polymorphic: true
|
7
|
-
end
|
1
|
+
class HaystackTagging < ApplicationRecord
|
2
|
+
belongs_to :haystack_tag
|
3
|
+
belongs_to :taggable, polymorphic: true
|
8
4
|
end
|
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
|
+
version: 1.0.1
|
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-
|
11
|
+
date: 2024-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bcrypt_pbkdf
|
@@ -270,14 +270,8 @@ extensions: []
|
|
270
270
|
extra_rdoc_files: []
|
271
271
|
files:
|
272
272
|
- Rakefile
|
273
|
-
- config/initializers/application_controller_renderer.rb
|
274
|
-
- config/initializers/backtrace_silencers.rb
|
275
273
|
- config/initializers/config.rb
|
276
|
-
- config/initializers/cors.rb
|
277
274
|
- config/initializers/filter_parameter_logging.rb
|
278
|
-
- config/initializers/inflections.rb
|
279
|
-
- config/initializers/lograge.rb
|
280
|
-
- config/initializers/mime_types.rb
|
281
275
|
- config/initializers/redis.rb
|
282
276
|
- config/initializers/wrap_parameters.rb
|
283
277
|
- config/locales/en.yml
|
@@ -1,7 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file.
|
2
|
-
|
3
|
-
# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
|
4
|
-
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
|
5
|
-
|
6
|
-
# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
|
7
|
-
# Rails.backtrace_cleaner.remove_silencers!
|
data/config/initializers/cors.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file.
|
2
|
-
|
3
|
-
# Avoid CORS issues when API is called from the frontend app.
|
4
|
-
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.
|
5
|
-
|
6
|
-
# Read more: https://github.com/cyu/rack-cors
|
7
|
-
|
8
|
-
# Rails.application.config.middleware.insert_before 0, Rack::Cors do
|
9
|
-
# allow do
|
10
|
-
# origins 'example.com'
|
11
|
-
#
|
12
|
-
# resource '*',
|
13
|
-
# headers: :any,
|
14
|
-
# methods: [:get, :post, :put, :patch, :delete, :options, :head]
|
15
|
-
# end
|
16
|
-
# end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# Be sure to restart your server when you modify this file.
|
2
|
-
|
3
|
-
# Add new inflection rules using the following format. Inflections
|
4
|
-
# are locale specific, and you may define rules for as many different
|
5
|
-
# locales as you wish. All of these examples are active by default:
|
6
|
-
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
7
|
-
# inflect.plural /^(ox)$/i, '\1en'
|
8
|
-
# inflect.singular /^(ox)en/i, '\1'
|
9
|
-
# inflect.irregular 'person', 'people'
|
10
|
-
# inflect.uncountable %w( fish sheep )
|
11
|
-
# end
|
12
|
-
|
13
|
-
# These inflection rules are supported but not enabled by default:
|
14
|
-
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
15
|
-
# inflect.acronym 'RESTful'
|
16
|
-
# end
|
File without changes
|