brycesenz/validates_serialized

The gem does not work with rails 3.*

Closed this issue · 3 comments

Example:

#!/usr/bin/env ruby

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  gem 'activemodel', '3.2.22'
  gem 'validates_serialized', '0.0.8'
end

class ValidatorArrayTestOne
  include ActiveModel::Validations
  validates_array_values :my_attr, presence: true, inclusion: { in: [1, 2, 3, 4] }

  def initialize(h={})
    h.each {|k,v| send("#{k}=",v)}
  end

  def my_attr
    @my_attr ||= []
  end

  def my_attr=(val)
    @my_attr = val
  end
end

record = ValidatorArrayTestOne.new(my_attr: [1, 5])
puts record.valid?

Output:

Resolving dependencies...
Using i18n 0.7.0
Using multi_json 1.11.2
Using activesupport 3.2.22
Using builder 3.0.4
Using activemodel 3.2.22
Using validates_serialized 0.0.8
Using bundler 1.10.6
/home/dxdy/.rbenv/versions/1.9.3-p545/lib64/ruby/gems/1.9.1/gems/activemodel-3.2.22/lib/active_model/validator.rb:160:in `validate_each': Subclasses must implement a validate_each(record, attribute, value) method (NotImplementedError)

Hrm, I built this for use in a Rails 3 app, so it should work. Can you try replacing validates_array_values with validates_array_values_with? Those actually do two different things.

I run tests and they break down on this line.

git clone https://github.com/brycesenz/validates_serialized.git
git checkout -b rails_3

vim validates_serialized.gemspec
# => spec.add_dependency "activemodel", "3.2.22"

bundle install

# =>
# Using rake 10.4.2
# Using i18n 0.7.0
# Using multi_json 1.11.2
# Using activesupport 3.2.22
# Using builder 3.0.4
# Using activemodel 3.2.22
# Using bundler 1.10.6
# Using diff-lcs 1.2.5
# Using rspec-core 2.99.2
# Using rspec-expectations 2.99.2
# Using rspec-mocks 2.99.2
# Using rspec 2.99.0
# Using sqlite3 1.3.10
# Using validates_serialized 0.0.8 from source at .

bundle exec rspec
# => 102 examples, 27 failures 

Output:

.....

  27) ActiveModel::Validations::HashValidator with instance #validates_hash_values_with #validate adds multiple errors for invalid value
     Failure/Error: validates_hash_values_with ::ActiveModel::Validations::PresenceValidator, attributes: [:my_attr]
     NotImplementedError:
       Subclasses must implement a validate_each(record, attribute, value) method
     # ./lib/validates_serialized/validators/serialized_validator.rb:28:in `block in validate_each'
     # ./lib/validates_serialized/validators/serialized_validator.rb:27:in `each'
     # ./lib/validates_serialized/validators/serialized_validator.rb:27:in `validate_each'
     # ./lib/validates_serialized/validators/hash_validator.rb:8:in `block in validate_serialized'
     # ./lib/validates_serialized/validators/hash_validator.rb:6:in `each_pair'
     # ./lib/validates_serialized/validators/hash_validator.rb:6:in `validate_serialized'
     # ./lib/validates_serialized/validators/serialized_validator.rb:22:in `block in validate'
     # ./lib/validates_serialized/validators/serialized_validator.rb:19:in `each'
     # ./lib/validates_serialized/validators/serialized_validator.rb:19:in `validate'
     # ./lib/validates_serialized/validators/hash_validator.rb:70:in `block in validates_hash_values_with'
     # ./lib/validates_serialized/validators/hash_validator.rb:68:in `each'
     # ./lib/validates_serialized/validators/hash_validator.rb:68:in `validates_hash_values_with'
     # ./spec/validators/hash_validator_spec.rb:154:in `instance_validations'
     # ./spec/validators/hash_validator_spec.rb:173:in `block (4 levels) in <top (required)>'

The class PresenceValidator and other Validators contain method validate_each only with rails 4.0.0!

Closing, as Rails 3 is basically end of life at this point.