no_fly_list 0.7.0 → 0.7.3

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: a83b9c51a6624a462debf10d93389d093ab732e9b7e836ee90bde0f1ae5239e5
4
- data.tar.gz: 88e96def76873eaa415431ce3ddb05076f59234d68583bad4d8c4f298fe9c0c4
3
+ metadata.gz: 5faaa3eaa7a8bc8f8186f8ee0dd10971f0c187963a4b9ef93cf2ef00963bedc0
4
+ data.tar.gz: 575274c475a0a13b4b94194f904aeec4e4d6d7795cd36db6b12ad6eff28a64f3
5
5
  SHA512:
6
- metadata.gz: 28621b0b5ed052c16667b41c8398a0533d16b631cc31944a4d48f0f468da34a798e603b568435579afd1b07dae2a11ee9a7276d6b31f2a37baf9212aab5fa80a
7
- data.tar.gz: 8740278135b296f312a8ef5189ab25d95c3e6428f27c286b7d10d5d359197e52280682c9adeed0305859663f6d90b4d5596465274d3eb54e5c0893326d305110
6
+ metadata.gz: a98397336ebfc91db71338a108df13b48af0fdc1287c2c3e9ce98569394fc05cfe8b4450d1ef2272c15b7a077966b21671c6817c4aa2ae55972f4683545f5032
7
+ data.tar.gz: 1cb1ec3ffcfb380fd616dabf5e5551e5ec5c5f52f24ab966aa68542c6f60f4770b293bdba8ee5841583e93b7a9d8a20a7ef25a02627473382ad2c84baaa9f88c
@@ -39,14 +39,19 @@ module NoFlyList
39
39
 
40
40
  def connection_abstract_class_name
41
41
  # should be abstract class name
42
- ActiveRecord::Base.descendants.find do |klass|
42
+ klass = ActiveRecord::Base.descendants.find do |klass|
43
43
  klass.abstract_class? && klass.connection_db_config.name == connection_name
44
- end.name
44
+ end
45
+ klass&.name || "ApplicationRecord"
45
46
  end
46
47
 
47
48
  def migration_version
48
49
  "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
49
50
  end
51
+
52
+ def migration_class_name
53
+ "CreateApplicationTaggingTable"
54
+ end
50
55
  end
51
56
  end
52
57
  end
@@ -36,18 +36,17 @@ module NoFlyList
36
36
  if model_class < ActiveRecord::Base
37
37
  true
38
38
  else
39
- say "#{class_name} is not an ActiveRecord model. Aborting generator.", :red
40
- false
39
+ raise ArgumentError, "#{class_name} is not an ActiveRecord model. Aborting generator."
41
40
  end
42
41
  rescue NameError
43
- say "#{class_name} is not a valid constant. Aborting generator.", :red
44
- false
42
+ raise ArgumentError, "#{class_name} is not a valid constant. Aborting generator."
45
43
  end
46
44
 
47
45
  def model_abstract_class_name
48
- model_class.ancestors.find do |klass|
49
- klass.is_a?(Class) && klass.abstract_class?
50
- end.name
46
+ klass = model_class.ancestors.find do |ancestor|
47
+ ancestor.is_a?(Class) && ancestor.abstract_class?
48
+ end
49
+ klass&.name || "ApplicationRecord"
51
50
  end
52
51
  end
53
52
  end
@@ -9,6 +9,7 @@ module NoFlyList
9
9
  module Generators
10
10
  class TaggingGenerator < Rails::Generators::NamedBase
11
11
  include ActiveRecord::Generators::Migration
12
+ source_root File.expand_path("templates", __dir__)
12
13
 
13
14
  class_option :database, type: :string, default: "primary",
14
15
  desc: "Use different database for migration"
@@ -5,15 +5,14 @@ require "rails/generators"
5
5
  require "rails/generators/active_record"
6
6
  require "rails/generators/named_base"
7
7
 
8
- unless defined?(ApplicationTagTransformer)
9
- module NoFlyList
10
- module Generators
11
- # bin/rails g no_fly_list:transformer
12
- class TransformerGenerator < Rails::Generators::Base
13
- source_root File.expand_path("templates", __dir__)
14
- def create_tag_transformer_file
15
- template "tag_transformer.rb", File.join("app/transformers", "application_tag_transformer.rb")
16
- end
8
+ module NoFlyList
9
+ module Generators
10
+ # bin/rails g no_fly_list:transformer
11
+ class TransformerGenerator < Rails::Generators::Base
12
+ source_root File.expand_path("templates", __dir__)
13
+
14
+ def create_tag_transformer_file
15
+ template "tag_transformer.rb", File.join("app/transformers", "application_tag_transformer.rb")
17
16
  end
18
17
  end
19
18
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NoFlyList
4
+ module DefaultTransformer
5
+ module_function
6
+
7
+ # @param [String, Array<String>] tags
8
+ def parse_tags(tags)
9
+ tags = recreate_string(tags) if tags.is_a?(Array)
10
+ tags.split(separator).map(&:strip)
11
+ end
12
+
13
+ # Recreate a string from an array of tags
14
+ # @param [Array<String>] tags
15
+ # @return [String]
16
+ def recreate_string(tags)
17
+ tags.join(separator)
18
+ end
19
+
20
+ # @return [String]
21
+ def separator
22
+ ","
23
+ end
24
+ end
25
+ end
@@ -19,7 +19,7 @@ module NoFlyList
19
19
  taggable_class: @taggable_class.to_s,
20
20
  tag_class_name: tag_class_name,
21
21
  tagging_class_name: tagging_class_name,
22
- transformer: options.fetch(:transformer, ApplicationTagTransformer).to_s,
22
+ transformer: options.fetch(:transformer, "ApplicationTagTransformer").to_s,
23
23
  polymorphic: options.fetch(:polymorphic, false),
24
24
  restrict_to_existing: options.fetch(:restrict_to_existing, false),
25
25
  limit: options.fetch(:limit, nil),
@@ -41,7 +41,7 @@ module NoFlyList
41
41
  def initialize(taggable_klass, context, options = {})
42
42
  @taggable_klass = taggable_klass
43
43
  @context = context
44
- @transformer = options.fetch(:transformer, ApplicationTagTransformer)
44
+ @transformer = options.fetch(:transformer, "ApplicationTagTransformer")
45
45
  @polymorphic = options.fetch(:polymorphic, false)
46
46
  @restrict_to_existing = options.fetch(:restrict_to_existing, false)
47
47
  @counter_cache = options.fetch(:counter_cache, false)
@@ -59,7 +59,12 @@ module NoFlyList
59
59
 
60
60
  proxy = instance_variable_get(var)
61
61
  next if proxy.nil?
62
- return false unless proxy.save
62
+ unless proxy.save
63
+ proxy.errors.each do |error|
64
+ errors.add(:base, error.message)
65
+ end
66
+ throw :abort
67
+ end
63
68
  end
64
69
  true
65
70
  ensure
@@ -19,19 +19,32 @@ module NoFlyList
19
19
  # @param restrict_to_existing [Boolean] Only allow existing tags
20
20
  # @param limit [Integer, nil] Maximum number of tags allowed
21
21
  def initialize(model, tag_model, context,
22
- transformer: ApplicationTagTransformer,
22
+ transformer: "ApplicationTagTransformer",
23
23
  restrict_to_existing: false,
24
24
  limit: nil)
25
25
  @model = model
26
26
  @tag_model = tag_model
27
27
  @context = context
28
- @transformer = transformer.is_a?(String) ? transformer.constantize : transformer
28
+ @transformer = resolve_transformer(transformer)
29
29
  @restrict_to_existing = restrict_to_existing
30
30
  @limit = limit
31
31
  @pending_changes = nil # Use nil to indicate no changes yet
32
32
  @clear_operation = false
33
33
  end
34
34
 
35
+ def resolve_transformer(trans)
36
+ const = trans
37
+ const = const.constantize if const.is_a?(String)
38
+ unless const.respond_to?(:parse_tags) && const.respond_to?(:recreate_string)
39
+ warn "NoFlyList: transformer #{trans.inspect} is invalid. Falling back to DefaultTransformer"
40
+ const = NoFlyList::DefaultTransformer
41
+ end
42
+ const
43
+ rescue NameError
44
+ warn "NoFlyList: transformer #{trans.inspect} not found. Falling back to DefaultTransformer"
45
+ NoFlyList::DefaultTransformer
46
+ end
47
+
35
48
  # Determines if tags have changed from database state
36
49
  # @return [Boolean] True if pending changes differ from database
37
50
  # @api private
@@ -319,11 +332,12 @@ module NoFlyList
319
332
 
320
333
  def current_list_from_database
321
334
  if setup[:polymorphic]
322
- tagging_table = setup[:tagging_class_name].tableize
335
+ tagging_klass = setup[:tagging_class_name].constantize
336
+ tagging_table = tagging_klass.arel_table
337
+
323
338
  @model.send(@context.to_s)
324
- .joins("INNER JOIN #{tagging_table} ON #{tagging_table}.tag_id = tags.id")
325
- .where("#{tagging_table}.taggable_type = ? AND #{tagging_table}.taggable_id = ?",
326
- @model.class.name, @model.id)
339
+ .where(tagging_table[:taggable_type].eq(@model.class.name))
340
+ .where(tagging_table[:taggable_id].eq(@model.id))
327
341
  .pluck(:name)
328
342
  else
329
343
  @model.send(@context.to_s).pluck(:name)
@@ -332,7 +346,7 @@ module NoFlyList
332
346
 
333
347
  def set_list(_context, value)
334
348
  @clear_operation = false
335
- @pending_changes = transformer.parse_tags(value)
349
+ @pending_changes = transformer.parse_tags(value).uniq.reject(&:blank?)
336
350
  mark_record_dirty
337
351
  valid? # Just check validity without raising
338
352
  self
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NoFlyList
4
- VERSION = "0.7.0"
4
+ VERSION = "0.7.3"
5
5
  end
data/lib/no_fly_list.rb CHANGED
@@ -28,6 +28,7 @@ module NoFlyList
28
28
 
29
29
  autoload :TaggingRecord
30
30
  autoload :TagRecord
31
+ autoload :DefaultTransformer
31
32
 
32
33
  autoload :TaggingProxy
33
34
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: no_fly_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-03-22 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activerecord
@@ -23,7 +23,8 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: '7.2'
26
- description: Tagging system for ActiveRecord models inspired by the TSA
26
+ description: A flexible, high-performance tagging system for Rails applications with
27
+ support for polymorphic tags, custom transformers, and database-specific optimizations
27
28
  email:
28
29
  - terminale@gmail.com
29
30
  executables: []
@@ -44,6 +45,7 @@ files:
44
45
  - lib/no_fly_list.rb
45
46
  - lib/no_fly_list/application_tag.rb
46
47
  - lib/no_fly_list/application_tagging.rb
48
+ - lib/no_fly_list/default_transformer.rb
47
49
  - lib/no_fly_list/railtie.rb
48
50
  - lib/no_fly_list/railties/tasks.rake
49
51
  - lib/no_fly_list/tag_record.rb
@@ -65,7 +67,12 @@ homepage: https://github.com/contriboss/no_fly_list
65
67
  licenses:
66
68
  - MIT
67
69
  metadata:
70
+ homepage_uri: https://github.com/contriboss/no_fly_list
71
+ source_code_uri: https://github.com/contriboss/no_fly_list
72
+ changelog_uri: https://github.com/contriboss/no_fly_list/blob/master/CHANGELOG.md
73
+ bug_tracker_uri: https://github.com/contriboss/no_fly_list/issues
68
74
  rubygems_mfa_required: 'true'
75
+ github_repo: ssh://github.com/contriboss/no_fly_list
69
76
  rdoc_options: []
70
77
  require_paths:
71
78
  - lib
@@ -80,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
87
  - !ruby/object:Gem::Version
81
88
  version: '0'
82
89
  requirements: []
83
- rubygems_version: 3.6.2
90
+ rubygems_version: 3.6.9
84
91
  specification_version: 4
85
- summary: Tagging system for ActiveRecord models
92
+ summary: Modern tagging system for Rails 7.2+ applications
86
93
  test_files: []