-
Notifications
You must be signed in to change notification settings - Fork 465
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
[POC] rails attribute for ancestor_ids #481
Open
kbrock
wants to merge
5
commits into
stefankroes:master
Choose a base branch
from
kbrock:attribute
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+132
−132
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7c59bb5
child_ancestry_str
kbrock 57bf430
use serialize for ancestry
kbrock 79a79bd
fix changed method names to better support 5.0
kbrock ac13415
rename ancestry to ancestor_ids in more places
kbrock ecdf320
slow removing ancestor(y) references
kbrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,75 @@ def self.default_update_strategy | |
def self.default_update_strategy=(value) | ||
@@default_update_strategy = value | ||
end | ||
|
||
# used for materialized path | ||
class MaterializedPathString < ActiveRecord::Type::Value | ||
def initialize(casting: :to_i, delimiter: '/') | ||
@casting = casting&.to_proc | ||
@delimiter = delimiter | ||
end | ||
|
||
def type | ||
:materialized_path_string | ||
end | ||
|
||
# convert to database type | ||
def serialize(value) | ||
if value.kind_of?(Array) | ||
value.map(&:to_s).join(@delimiter).presence | ||
elsif value.kind_of?(Integer) | ||
value.to_s | ||
elsif value.nil? || value.kind_of?(String) | ||
value | ||
else | ||
byebug | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we want to handle this case a little differently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol |
||
puts "curious type: #{value.class}" | ||
end | ||
end | ||
|
||
def cast(value) | ||
cast_value(value) #unless value.nil? (want to get rid of this - fix default value) | ||
end | ||
|
||
# called by cast (form or setter) or deserialize (database) | ||
def cast_value(value) | ||
if value.kind_of?(Array) | ||
super | ||
elsif value.nil? | ||
# would prefer to use default here | ||
# but with default, it kept thinking the field had changed when it hadn't | ||
super([]) | ||
else | ||
#TODO: test ancestry=1 | ||
super(value.to_s.split(@delimiter).map(&@casting)) | ||
end | ||
end | ||
end | ||
end | ||
ActiveRecord::Type.register(:materialized_path_string, Ancestry::MaterializedPathString) | ||
|
||
class ArrayPatternValidator < ActiveModel::EachValidator | ||
def initialize(options) | ||
raise ArgumentError, "Pattern unspecified, Specify using :pattern" unless options[:pattern] | ||
|
||
options[:pattern] = /\A#{options[:pattern].to_s}\Z/ unless options[:pattern].to_s.include?('\A') | ||
options[:id] = true unless options.key?(:id) | ||
options[:integer] = true unless options.key?(:integer) | ||
|
||
super | ||
end | ||
|
||
def validate_each(record, attribute, value) | ||
if options[:id] && value.include?(record.id) | ||
record.errors.add(attribute, I18n.t("ancestry.exclude_self", {:class_name => self.class.name.humanize})) | ||
end | ||
|
||
if value.any? { |v| v.to_s !~ options[:pattern] } | ||
record.errors.add(attribute, "illegal characters") | ||
end | ||
|
||
if options[:integer] && value.any? { |v| v < 1 } | ||
record.errors.add(attribute, "non positive ancestor id") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we want an empty ancestry to have
[]
ornil
technically null means that the value is undetermined. But in our case, we want to say "none" which is "" and not nil.