Skip to content

Commit

Permalink
Validate configuration better
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegeek committed Aug 7, 2023
1 parent afd16a2 commit 76315e2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
2 changes: 1 addition & 1 deletion encoded_id-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
# Uncomment to register a new dependency of your gem
spec.add_dependency "activesupport", ">= 6.0", "< 8.0"
spec.add_dependency "activerecord", ">= 6.0", "< 8.0"
spec.add_dependency "encoded_id", "~> 1.0.0.rc1"
spec.add_dependency "encoded_id", "~> 1.0.0.rc2"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
39 changes: 30 additions & 9 deletions lib/encoded_id/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ module EncodedId
module Rails
# Configuration class for initializer
class Configuration
attr_accessor :salt,
:character_group_size,
:group_separator,
:alphabet,
:id_length,
:slug_value_method_name,
:slugged_id_separator,
:annotation_method_name, # Set to nil to disable annotated IDs
:annotated_id_separator
attr_accessor :salt, :character_group_size, :alphabet, :id_length
attr_accessor :slug_value_method_name, :annotation_method_name
attr_reader :group_separator, :slugged_id_separator, :annotated_id_separator

def initialize
@character_group_size = 4
Expand All @@ -24,6 +18,33 @@ def initialize
@annotation_method_name = :annotation_for_encoded_id
@annotated_id_separator = "_"
end

# Perform validation vs alphabet on these assignments

def group_separator=(value)
unless valid_separator?(value, alphabet)
raise ArgumentError, "Group separator characters must not be part of the alphabet"
end
@group_separator = value
end

def slugged_id_separator=(value)
if value.blank? || value == group_separator || !valid_separator?(value, alphabet)
raise ArgumentError, "Slugged ID separator characters must not be part of the alphabet or the same as the group separator"
end
@slugged_id_separator = value
end

def annotated_id_separator=(value)
if value.blank? || value == group_separator || !valid_separator?(value, alphabet)
raise ArgumentError, "Annotated ID separator characters must not be part of the alphabet or the same as the group separator"
end
@annotated_id_separator = value
end

def valid_separator?(separator, characters)
separator.chars.none? { |v| characters.include?(v) }
end
end
end
end
26 changes: 26 additions & 0 deletions lib/generators/encoded_id/rails/templates/encoded_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,30 @@
# Default: 8
#
# config.id_length = 8

# The name of the method that returns the value to be used in the slug.
#
# Default: :name_for_encoded_id_slug
#
# config.slug_value_method_name = :name_for_encoded_id_slug

# The separator used between the slug and the encoded ID.
# `nil` disables grouping.
#
# Default: "--"
#
# config.slugged_id_separator = "--"

# The name of the method that returns the annotation to be used in the annotated ID.
#
# Default: :annotation_for_encoded_id
#
# config.annotation_method_name = :annotation_for_encoded_id

# The separator used between the annotation and the encoded ID.
# `nil` disables annotation.
#
# Default: "_"
#
# config.annotated_id_separator = "_"
end
22 changes: 22 additions & 0 deletions test/encoded_id/test_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ def setup
EncodedId::Rails.configuration.slug_value_method_name = :custom_slug_method
end

def test_configuration_prevents_invalid_group_separator
assert_raises ArgumentError do
EncodedId::Rails.configuration.group_separator = "a"
end
end

def test_configuration_prevents_invalid_slugged_id_separator
assert_raises ArgumentError do
EncodedId::Rails.configuration.slugged_id_separator = "a"
end
EncodedId::Rails.configuration.group_separator = "-"
assert_raises ArgumentError do
EncodedId::Rails.configuration.slugged_id_separator = "-"
end
end

def test_configuration_prevents_invalid_annotated_id_separator
assert_raises ArgumentError do
EncodedId::Rails.configuration.annotated_id_separator = "a"
end
end

def test_find_by_encoded_id_gets_model_given_encoded_id
assert_equal model, MyModel.find_by_encoded_id(model.encoded_id)
end
Expand Down

0 comments on commit 76315e2

Please sign in to comment.