Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default ancestry format and primary key format #612

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 37 additions & 1 deletion lib/ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

module Ancestry
@@default_update_strategy = :ruby
@@default_ancestry_format = :materialized_path2
@@default_primary_key_format = '[0-9]+'

# @!default_update_strategy
# @return [Symbol] the default strategy for updating ancestry
Expand All @@ -27,12 +29,46 @@ module Ancestry
#
# Child records are updated in sql and callbacks will not get called.
# Associated records in memory will have the wrong ancestry value

def self.default_update_strategy
@@default_update_strategy
end

def self.default_update_strategy=(value)
@@default_update_strategy = value
end

# @!default_ancestry_format
# @return [Symbol] the default strategy for updating ancestry
#
# The value changes the default way that ancestry is stored in the database
#
# :materialized_path (default and legacy)
#
# Ancestry is of the form null (for no ancestors) and 1/2/ for children
#
# :materialized_path2 (preferred)
#
# Ancestry is of the form '/' (for no ancestors) and '/1/2/' for children
def self.default_ancestry_format
@@default_ancestry_format
end

def self.default_ancestry_format=(value)
@@default_ancestry_format = value
end

# @!default_primary_key_format
# @return [Symbol] the regular expression representing the primary key
#
# The value represents the way the id looks for validation
#
# '[0-9]+' (default) for integer ids
# '[-A-Fa-f0-9]{36}' for uuids (though you can find other regular expressions)
def self.default_primary_key_format
@@default_primary_key_format
end

def self.default_primary_key_format=(value)
@@default_primary_key_format = value
end
end
6 changes: 3 additions & 3 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def has_ancestry options = {}
self.ancestry_column = options[:ancestry_column] || :ancestry

cattr_accessor :ancestry_primary_key_format
self.ancestry_primary_key_format = options[:primary_key_format].presence || '[0-9]+'
self.ancestry_primary_key_format = options[:primary_key_format].presence || Ancestry.default_primary_key_format

cattr_accessor :ancestry_delimiter
self.ancestry_delimiter = '/'
Expand All @@ -38,9 +38,9 @@ def has_ancestry options = {}
extend Ancestry::ClassMethods

cattr_accessor :ancestry_format
self.ancestry_format = options[:ancestry_format] || :materialized_path
self.ancestry_format = options[:ancestry_format] || Ancestry.default_update_strategy

if options[:ancestry_format] == :materialized_path2
if ancestry_format == :materialized_path2
extend Ancestry::MaterializedPath2
else
extend Ancestry::MaterializedPath
Expand Down
2 changes: 1 addition & 1 deletion test/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def self.setup
# This only affects postgres
# the :ruby code path will get tested in mysql and sqlite3
Ancestry.default_update_strategy = :sql if postgres?
Ancestry.default_ancestry_format = ENV["FORMAT"].to_sym if ENV["FORMAT"].present?

rescue => err
if ENV["CI"]
Expand Down Expand Up @@ -86,7 +87,6 @@ def self.with_model options = {}
width = options.delete(:width) || 0
extra_columns = options.delete(:extra_columns)
default_scope_params = options.delete(:default_scope_params)
options[:ancestry_format] = ENV["FORMAT"].to_sym if ENV["FORMAT"].present?

table_options={}
table_options[:id] = options.delete(:id) if options.key?(:id)
Expand Down