soft_validate 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/soft_validate.rb +15 -49
- data/lib/soft_validate/version.rb +1 -1
- data/test/debug.log +16 -0
- metadata +5 -5
data/lib/soft_validate.rb
CHANGED
@@ -1,70 +1,36 @@
|
|
1
1
|
require 'active_record'
|
2
|
-
require 'pry'
|
3
2
|
|
4
3
|
module SoftValidate
|
5
|
-
|
6
|
-
def self.included(base)
|
7
|
-
base.send :extend, ClassMethods
|
8
|
-
end
|
4
|
+
extend ActiveSupport::Concern
|
9
5
|
|
10
6
|
module ClassMethods
|
11
|
-
# any method placed here will apply to classes, like Hickwall
|
12
|
-
|
13
7
|
def soft_validates_presence_of(attr)
|
14
8
|
cattr_accessor :soft_attributes
|
15
9
|
self.soft_attributes ||= Array.new
|
16
10
|
self.soft_attributes << attr
|
17
|
-
send :include, InstanceMethods
|
18
11
|
end
|
19
12
|
|
20
|
-
|
21
|
-
|
22
13
|
end
|
23
14
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
def progress_complete_count
|
28
|
-
self.soft_attributes.length
|
29
|
-
end
|
30
|
-
|
31
|
-
def soft_valid?
|
32
|
-
|
33
|
-
self.soft_attributes.each do |a|
|
34
|
-
return false if self.attributes[a.to_s].nil?
|
35
|
-
end
|
36
|
-
|
37
|
-
true
|
38
|
-
end
|
39
|
-
|
40
|
-
def soft_errors
|
41
|
-
errors = {}
|
42
|
-
self.soft_attributes.each do |a|
|
43
|
-
if self.attributes[a.to_s].nil?
|
44
|
-
errors[a] = "shouldn't be blank"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
errors
|
49
|
-
end
|
50
|
-
|
51
|
-
def progress_count
|
52
|
-
count = 0
|
15
|
+
def progress_complete_count
|
16
|
+
self.soft_attributes.length
|
17
|
+
end
|
53
18
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
58
|
-
end
|
19
|
+
def soft_valid?
|
20
|
+
self.soft_attributes.all? { |a| !self.attributes[a.to_s].nil? }
|
21
|
+
end
|
59
22
|
|
60
|
-
|
61
|
-
|
23
|
+
def soft_errors
|
24
|
+
Hash[ self.soft_attributes.map { |a| [a, "shouldn't be blank"] if self.attributes[a.to_s].nil? } ]
|
25
|
+
end
|
62
26
|
|
63
|
-
|
64
|
-
|
65
|
-
end
|
27
|
+
def progress_count
|
28
|
+
self.soft_attributes.count { |a| !self.attributes[a.to_s].nil? }
|
66
29
|
end
|
67
30
|
|
31
|
+
def progress_percent
|
32
|
+
self.progress_count.to_f / self.progress_complete_count.to_f
|
33
|
+
end
|
68
34
|
end
|
69
35
|
|
70
36
|
ActiveRecord::Base.send :include, SoftValidate
|
data/test/debug.log
CHANGED
@@ -23,3 +23,19 @@
|
|
23
23
|
[1m[35m (0.1ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
24
24
|
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
25
25
|
[1m[35m (0.1ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('0')
|
26
|
+
[1m[36m (1.2ms)[0m [1mselect sqlite_version(*)[0m
|
27
|
+
[1m[35m (0.4ms)[0m CREATE TABLE "dumb_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "first_name" varchar(255), "last_name" varchar(255))
|
28
|
+
[1m[36m (0.2ms)[0m [1mCREATE TABLE "non_validated_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255)) [0m
|
29
|
+
[1m[35m (0.2ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
30
|
+
[1m[36m (0.0ms)[0m [1mPRAGMA index_list("schema_migrations")[0m
|
31
|
+
[1m[35m (0.2ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
32
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
33
|
+
[1m[35m (0.1ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('0')
|
34
|
+
[1m[36m (2.2ms)[0m [1mselect sqlite_version(*)[0m
|
35
|
+
[1m[35m (0.4ms)[0m CREATE TABLE "dumb_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "first_name" varchar(255), "last_name" varchar(255))
|
36
|
+
[1m[36m (0.2ms)[0m [1mCREATE TABLE "non_validated_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255)) [0m
|
37
|
+
[1m[35m (0.1ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
38
|
+
[1m[36m (0.0ms)[0m [1mPRAGMA index_list("schema_migrations")[0m
|
39
|
+
[1m[35m (0.1ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
40
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
41
|
+
[1m[35m (0.1ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('0')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soft_validate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ date: 2012-04-11 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
17
|
-
requirement: &
|
17
|
+
requirement: &70180042847580 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 3.2.3
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70180042847580
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: sqlite3
|
28
|
-
requirement: &
|
28
|
+
requirement: &70180042845940 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70180042845940
|
37
37
|
description: Add softs_validate_presence_of :attr to your model and it will give you
|
38
38
|
methods to figure out percent complete, missing fields, etc without preventing the
|
39
39
|
record from being saved.
|