Created
January 20, 2015 13:40
-
-
Save killthekitten/c7c12debe4389b91787a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class BulkValidator | |
attr_accessor :items, :bulk_validations | |
def initialize(items, validations) | |
@items = items | |
@bulk_validations = build_bulk_validations(validations) | |
end | |
def candidates | |
bulk_validations.each do |validation| | |
@items = validation.apply items | |
end | |
items | |
end | |
def invalidate_candidates | |
end | |
private | |
def build_bulk_validations(validations) | |
validations.map do |validation| | |
case validation.validation_type | |
when "presence" then PresenceValidation.new(validation.options) | |
... | |
else | |
fail("Unknown validation type #{validation.validation_type}") | |
end | |
end | |
end |
This file contains hidden or 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
class CatalogField < ActiveRecord::Base | |
... | |
# Бывшая dirty_validated_items | |
def invalidated_items | |
bulk_validator = BulkValidator.new(items, validations) | |
bulk_validator.candidates | |
end | |
... | |
end |
This file contains hidden or 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
def confirm_changes | |
@catalog_field.assign_attributes(catalog_field_params) | |
@items = @catalog_field.invalidated_items | |
# Вообще проверь почему здесь следующая строчка присутствует :) | |
render :edit unless @catalog_field.valid? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment