Skip to content

Commit

Permalink
Also pass the record, attribute, and value to the resposne callback
Browse files Browse the repository at this point in the history
  • Loading branch information
bfolkens committed Aug 10, 2012
1 parent f659d71 commit f173ddf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/url_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def validate_each(record, attribute, value)

record.errors.add(attribute, options[:invalid_url_message] || :invalid_url) unless url_format_valid?(uri, options)
record.errors.add(attribute, options[:url_not_accessible_message] || :url_not_accessible) unless response = url_accessible?(uri, options)
record.errors.add(attribute, options[:url_invalid_response_message] || :url_invalid_response) unless url_response_valid?(response, options)
record.errors.add(attribute, options[:url_invalid_response_message] || :url_invalid_response) unless url_response_valid?(response, record, attribute, value, options)
end

private
Expand Down Expand Up @@ -175,9 +175,9 @@ def http_url_accessible?(uri, options)
return false
end

def url_response_valid?(response, options)
def url_response_valid?(response, record, attribute, value, options)
return true unless response.kind_of?(HTTPI::Response) and options[:check_path]
options[:response_callback].call(response) if options[:response_callback].respond_to?(:call)
options[:response_callback].call(response, record, attribute, value) if options[:response_callback].respond_to?(:call)
response_codes = options[:check_path] == true ? [400..499, 500..599] : Array.wrap(options[:check_path]).flatten
return response_codes.none? do |code| # it's good if it's not a bad response
case code # and it's a bad response if...
Expand Down
2 changes: 1 addition & 1 deletion spec/url_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class Record
context "[:response_callback]" do
it "should be yielded the HTTPI response" do
called = false
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => true, :response_callback => lambda { |response| called = true; response.should be_kind_of(HTTPI::Response) })
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => true, :response_callback => lambda { |response, record, attribute, value| called = true; response.should be_kind_of(HTTPI::Response) })
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
called.should be_true
end
Expand Down

0 comments on commit f173ddf

Please sign in to comment.