-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate non-nil associations when validation on association or fk
- Loading branch information
1 parent
da6b0bb
commit d5ba230
Showing
13 changed files
with
25,066 additions
and
40,854 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module Boba | ||
module ActiveRecord | ||
module AttributeService | ||
class << self | ||
extend T::Sig | ||
|
||
sig { params(constant: T.class_of(::ActiveRecord::Base), attribute: String).returns(T::Boolean) } | ||
def has_unconditional_presence_validator?(constant, attribute) | ||
return false unless constant.respond_to?(:validators_on) | ||
|
||
constant.validators_on(attribute).any? do |validator| | ||
next false unless validator.is_a?(::ActiveRecord::Validations::PresenceValidator) | ||
|
||
!validator.options.key?(:if) && !validator.options.key?(:unless) && !validator.options.key?(:on) | ||
end | ||
end | ||
|
||
sig { params(constant: T.class_of(::ActiveRecord::Base), column_name: String).returns(T::Boolean) } | ||
def has_non_null_database_constraint?(constant, column_name) | ||
column = constant.columns_hash[column_name] | ||
return false if column.nil? | ||
|
||
!column.null | ||
rescue StandardError | ||
false | ||
end | ||
|
||
sig { params(constant: T.class_of(::ActiveRecord::Base), column_name: String).returns(T::Boolean) } | ||
def virtual_attribute?(constant, column_name) | ||
constant.columns_hash[column_name].nil? | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require_relative("attribute_service") | ||
|
||
module Boba | ||
module ActiveRecord | ||
module ReflectionService | ||
class << self | ||
extend T::Sig | ||
|
||
ReflectionType = T.type_alias do | ||
T.any(::ActiveRecord::Reflection::ThroughReflection, ::ActiveRecord::Reflection::AssociationReflection) | ||
end | ||
|
||
sig { params(reflection: ReflectionType).returns(T::Boolean) } | ||
def has_one_and_required_reflection?(reflection) | ||
return false unless reflection.has_one? | ||
return true if !!reflection.options[:required] | ||
return true if reflection_required_by_database_constraint?(reflection) | ||
|
||
reflection_required_by_validation?(reflection) | ||
end | ||
|
||
sig { params(reflection: ReflectionType).returns(T::Boolean) } | ||
def belongs_to_and_non_optional_reflection?(reflection) | ||
return false unless reflection.belongs_to? | ||
|
||
optional = if reflection.options.key?(:required) | ||
!reflection.options[:required] | ||
else | ||
reflection.options[:optional] | ||
end | ||
return !optional unless optional.nil? | ||
return true if reflection_required_by_database_constraint?(reflection) | ||
return true if reflection_required_by_validation?(reflection) | ||
|
||
# nothing defined, so fall back to the default active record config | ||
!!reflection.active_record.belongs_to_required_by_default | ||
end | ||
|
||
private | ||
|
||
# check for non-nullable database constraint on the foreign key | ||
sig { params(reflection: ReflectionType).returns(T::Boolean) } | ||
def reflection_required_by_database_constraint?(reflection) | ||
Boba::ActiveRecord::AttributeService.has_non_null_database_constraint?( | ||
reflection.active_record, | ||
reflection.foreign_key, | ||
) | ||
end | ||
|
||
# check for presence validator on the foreign key or on the association | ||
sig { params(reflection: ReflectionType).returns(T::Boolean) } | ||
def reflection_required_by_validation?(reflection) | ||
return true if Boba::ActiveRecord::AttributeService.has_unconditional_presence_validator?( | ||
reflection.active_record, | ||
reflection.foreign_key, | ||
) | ||
|
||
Boba::ActiveRecord::AttributeService.has_unconditional_presence_validator?( | ||
reflection.active_record, | ||
reflection.name, | ||
) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
# frozen_string_literal: true | ||
|
||
module Boba | ||
VERSION = "0.0.7" | ||
VERSION = "0.0.8" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/*.rbi linguist-generated=true |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.