activerecord_json_validator 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21b3da5123efa04feb0b954cb2300a241de8f091
4
- data.tar.gz: ce13d50fd4ef460f12ed57ab732834133425e254
3
+ metadata.gz: 2333a21aa351ea4a53b99e11d40f48b9fb6fa145
4
+ data.tar.gz: d306555e99f6aa8fb57af47c616c75c6f7c4f43a
5
5
  SHA512:
6
- metadata.gz: b47ebab5fb3d2d4575d2c659201d83e300d7627ece382276429bc0eb7e37a0c104976578a645e74c1a2eaf80a017dbc1de78f2f2dd5f9f38e8bc7989fb91756c
7
- data.tar.gz: fd32b8a98506390f1fae3b3acb8dd4f073edf4c7ec60f3aaad8d1bd5703e1547c006f3cc4363ba3080facbf20f1383a16005a02b9764eb73e8fcc767eaea662b
6
+ metadata.gz: 163af0746e0533ebbf10a67d38583afc3a77107bbf248e10ea4dda9b4b463616ba3449a0613b4bca0744d71f304b45cd9a7cd981bc98ea596d944ce21baba507
7
+ data.tar.gz: 4f1635fc39554eda8e821bb81bde46d1df33762938e9b690ddfec2c72cf2541a9cac0eb285c8af6abf34702985779b4e7c095224d586c345468f3058a1be1504
@@ -17,3 +17,6 @@ before_script:
17
17
  - psql -c 'create database activerecord_json_validator_test;' -U postgres
18
18
 
19
19
  script: "echo 'DO IT' && bundle exec rake spec"
20
+
21
+ addons:
22
+ postgresql: 9.3
@@ -5,24 +5,13 @@ class JsonValidator < ActiveModel::EachValidator
5
5
  @attributes = options[:attributes]
6
6
 
7
7
  super
8
+
9
+ inject_setter_method(options[:class], @attributes) if options[:class]
8
10
  end
9
11
 
10
- # Redefine the setter method for the attributes, since we want to
11
- # catch any MultiJson::LoadError errors.
12
+ # Only respond to `#setup` if we’re in Rails 4.0 or less
12
13
  def setup(model)
13
- @attributes.each do |attribute|
14
- model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
15
- define_method "#{attribute}=" do |args|
16
- begin
17
- @_#{attribute}_sane_json = true
18
- super(args)
19
- rescue MultiJson::LoadError
20
- @_#{attribute}_sane_json = false
21
- super({})
22
- end
23
- end
24
- RUBY
25
- end
14
+ inject_setter_method(model, @attributes)
26
15
  end
27
16
 
28
17
  # Validate the JSON value with a JSON schema path or String
@@ -39,4 +28,24 @@ class JsonValidator < ActiveModel::EachValidator
39
28
  record.errors.add(attribute, options.fetch(:message), value: value)
40
29
  end
41
30
  end
31
+
32
+ protected
33
+
34
+ # Redefine the setter method for the attributes, since we want to
35
+ # catch any MultiJson::LoadError errors.
36
+ def inject_setter_method(klass, attributes)
37
+ attributes.each do |attribute|
38
+ klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
39
+ define_method "#{attribute}=" do |args|
40
+ begin
41
+ @_#{attribute}_sane_json = true
42
+ super(args)
43
+ rescue MultiJson::LoadError
44
+ @_#{attribute}_sane_json = false
45
+ super({})
46
+ end
47
+ end
48
+ RUBY
49
+ end
50
+ end
42
51
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module JSONValidator
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
@@ -5,7 +5,7 @@ describe JsonValidator do
5
5
  run_migration do
6
6
  create_table(:users, force: true) do |t|
7
7
  t.string :name
8
- t.text :profile # Change this to `t.json` when Travis supports PostgreSQL 9.2+
8
+ t.text :profile
9
9
  end
10
10
  end
11
11
 
@@ -27,21 +27,21 @@ describe JsonValidator do
27
27
 
28
28
  context 'with blank JSON value' do
29
29
  let(:attributes) { { name: 'Samuel Garneau', profile: {} } }
30
- it { expect(User.new(attributes)).to_not be_valid }
30
+ it { expect(User.create(attributes)).to_not be_valid }
31
31
  end
32
32
 
33
33
  context 'with invalid JSON value' do
34
34
  let(:attributes) { { name: 'Samuel Garneau', profile: { city: 'Quebec City' } } }
35
- it { expect(User.new(attributes)).to_not be_valid }
35
+ it { expect(User.create(attributes)).to_not be_valid }
36
36
  end
37
37
 
38
38
  context 'with valid JSON value' do
39
39
  let(:attributes) { { name: 'Samuel Garneau', profile: { country: 'CA' } } }
40
- it { expect(User.new(attributes)).to be_valid }
40
+ it { expect(User.create(attributes)).to be_valid }
41
41
  end
42
42
 
43
43
  context 'with malformed JSON value' do
44
44
  let(:attributes) { { name: 'Samuel Garneau', profile: 'foo:}bar' } }
45
- it { expect(User.new(attributes)).to_not be_valid }
45
+ it { expect(User.create(attributes)).to_not be_valid }
46
46
  end
47
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_json_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-18 00:00:00.000000000 Z
11
+ date: 2013-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler