-
Notifications
You must be signed in to change notification settings - Fork 3
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
[WIP] Bang Validations #10
base: master
Are you sure you want to change the base?
Changes from all commits
23e4048
ba72c3c
b28da4c
1e2018a
00578ba
dbd4f6d
dcf12de
0a90d51
995ba45
4172fd4
a78b5d2
8b75a60
3625f2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.0.2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,28 +5,36 @@ | |
module ActionLogic | ||
module ActionValidation | ||
module ClassMethods | ||
def validates_before!(args) | ||
@validates_before = args.merge(raise_action_logic_exception: true) | ||
end | ||
|
||
def validates_before(args) | ||
@validates_before = args | ||
@validates_before = args.merge(raise_action_logic_exception: false) | ||
end | ||
|
||
def validates_after(args) | ||
@validates_after = args | ||
@validates_after = args.merge(raise_action_logic_exception: true) | ||
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 don't know how I feel about using an extra attribute to signal the validation classes. This is just a rough draft. If this is the approach to take, it needs to be a very specific name to avoid potential collisions, hence "raise_action_logic_exception". |
||
end | ||
|
||
def validates_around!(args) | ||
@validates_around = args.merge(raise_action_logic_exception: true) | ||
end | ||
|
||
def validates_around(args) | ||
@validates_around = args | ||
@validates_around = args.merge(raise_action_logic_exception: false) | ||
end | ||
|
||
def get_validates_before | ||
@validates_before ||= {} | ||
@validates_before ||= { raise_action_logic_exception: true } | ||
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. TODO: fix dupe. |
||
end | ||
|
||
def get_validates_after | ||
@validates_after ||= {} | ||
@validates_after ||= { raise_action_logic_exception: true } | ||
end | ||
|
||
def get_validates_around | ||
@validates_around ||= {} | ||
@validates_around ||= { raise_action_logic_exception: true } | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,17 @@ module ActionValidation | |
class PresenceValidation < BaseValidation | ||
|
||
def self.validate!(validation_rules, context) | ||
return unless validation_rules.values.find { |expected_validation| expected_validation[:presence] } | ||
errors = presence_errors(validation_rules, context) | ||
raise ActionLogic::PresenceError.new(errors) if errors.any? | ||
tmp_rules = validation_rules.clone | ||
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. we have to |
||
raise_exception = tmp_rules.delete(:raise_action_logic_exception) | ||
return unless tmp_rules.values.find { |expected_validation| expected_validation[:presence] } | ||
errors = presence_errors(tmp_rules, context) | ||
if raise_exception | ||
raise ActionLogic::PresenceError.new(errors) if errors.any? | ||
end | ||
end | ||
|
||
def self.presence_errors(validation_rules, context) | ||
validation_rules.reduce([]) do |error_collection, (expected_attribute, expected_validation)| | ||
def self.presence_errors(tmp_rules, context) | ||
tmp_rules.reduce([]) do |error_collection, (expected_attribute, expected_validation)| | ||
next unless expected_validation[:presence] | ||
error_collection << error_message(expected_attribute, expected_validation, context) | ||
error_collection | ||
|
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.
Will eventually eliminate this dupe, but for now I just wanted to see what it looked like to use the
validations
hash to signal to the validation classes whether to raise an exception or not.