slim_validation 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/slim_validation.rb +5 -2
- data/lib/slim_validation/version.rb +1 -1
- data/spec/dummy/app/models/entry.rb +3 -0
- data/spec/models/entry_spec.rb +15 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb337db1e25a1983778c7d611c726f78ee2e3452
|
4
|
+
data.tar.gz: 1d6ca65d94e1f77f5877bbe346dbb03691f867f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fada04684b322af96d1aa64e00adce11a4ad54d746e6b8746e948a58c689daa4c2d6f64cf3e0a04b3b71fc6ef808dc0e9b8382c237b734567107f805533e5a44
|
7
|
+
data.tar.gz: a24705ae343091d344a585889418e31861549019108daf93c28e5ac74ec206192516ad83a42fa22adaca2e74bbea062071e1b4e30b1cdc7e9414190644e4f827
|
data/Gemfile.lock
CHANGED
data/lib/slim_validation.rb
CHANGED
@@ -6,11 +6,14 @@ require 'slim'
|
|
6
6
|
class SlimValidator < ActiveModel::EachValidator
|
7
7
|
def validate_each(record, attr_name, value)
|
8
8
|
result = Slim::Template.new(options[:options]) { value }.render(options[:scope] == :record ? record : options[:scope])
|
9
|
-
rescue
|
9
|
+
rescue => e
|
10
|
+
if (error_storer = options[:error]).present?
|
11
|
+
record.send("#{error_storer}=", e)
|
12
|
+
end
|
10
13
|
record.errors.add(attr_name, :invalid_slim, options)
|
11
14
|
ensure
|
12
15
|
if (to = options[:to]).present?
|
13
|
-
to.is_a?(Proc) ? to.(record, result) : record
|
16
|
+
to.is_a?(Proc) ? to.(record, result) : record.send("#{to}=", result)
|
14
17
|
end
|
15
18
|
end
|
16
19
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
class Entry < ActiveRecord::Base
|
2
|
+
attr_accessor :slim_error
|
3
|
+
|
2
4
|
validates :slim,
|
3
5
|
slim: {
|
4
6
|
allow_blank: true
|
@@ -31,6 +33,7 @@ class Entry < ActiveRecord::Base
|
|
31
33
|
options: {pretty: false},
|
32
34
|
to: :html,
|
33
35
|
scope: :record,
|
36
|
+
error: :slim_error,
|
34
37
|
allow_blank: true
|
35
38
|
}
|
36
39
|
|
data/spec/models/entry_spec.rb
CHANGED
@@ -7,6 +7,16 @@ RSpec.describe Entry, type: :model do
|
|
7
7
|
it { expect { create(:entry, slim_proc: invalid_slim) }.to raise_exception(ActiveRecord::RecordInvalid) }
|
8
8
|
it { expect { create(:entry, slim_attribute: invalid_slim) }.to raise_exception(ActiveRecord::RecordInvalid) }
|
9
9
|
it { expect { create(:entry, slim_scope: invalid_slim) }.to raise_exception(ActiveRecord::RecordInvalid) }
|
10
|
+
|
11
|
+
context 'with error storer' do
|
12
|
+
let(:entry) do
|
13
|
+
entry = build(:entry, slim_scope: invalid_slim)
|
14
|
+
entry.valid?
|
15
|
+
entry
|
16
|
+
end
|
17
|
+
it { expect(entry.slim_error).to be_truthy }
|
18
|
+
end
|
19
|
+
|
10
20
|
end
|
11
21
|
|
12
22
|
context 'when added valid slim' do
|
@@ -39,6 +49,11 @@ RSpec.describe Entry, type: :model do
|
|
39
49
|
it { expect(entry).to be_a(Entry) }
|
40
50
|
it { expect(entry.html).to eq('<header><h1>local value</h1></header><section><p>content</p></section><footer><p>2010/05/09</p></footer>') }
|
41
51
|
end
|
52
|
+
|
53
|
+
context 'with error storer' do
|
54
|
+
let(:entry) { create(:entry, slim_scope: valid_slim) }
|
55
|
+
it { expect(entry.slim_error).to be_nil }
|
56
|
+
end
|
42
57
|
end
|
43
58
|
end
|
44
59
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slim_validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mmmpa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|