soft_validate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 DeveloperTown
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ Soft Validate
2
+ =============
3
+
4
+ Easily recommend fields be filled out in order for something to be considered complete (AKA LinkedIn profile progress)
5
+
6
+ Install
7
+ -------
8
+ gem install 'soft_validate'
9
+
10
+ Usage
11
+ -----
12
+ Add this to your model:
13
+
14
+ soft_validates_presence_of :name
15
+
16
+ And you will get methods like this:
17
+
18
+ user.soft_valid?
19
+ user.soft_errors
20
+ user.progress_percent
21
+ user.progress_count
22
+ user.progress_complete_count
23
+
24
+ License
25
+ -------
26
+
27
+ MIT License
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'SoftValidate'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,70 @@
1
+ require 'active_record'
2
+ require 'pry'
3
+
4
+ module SoftValidate
5
+
6
+ def self.included(base)
7
+ base.send :extend, ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ # any method placed here will apply to classes, like Hickwall
12
+
13
+ def soft_validates_presence_of(attr)
14
+ cattr_accessor :soft_attributes
15
+ self.soft_attributes ||= Array.new
16
+ self.soft_attributes << attr
17
+ send :include, InstanceMethods
18
+ end
19
+
20
+
21
+
22
+ end
23
+
24
+ module InstanceMethods
25
+ # any method placed here will apply to instaces, like @hickwall
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
53
+
54
+ self.soft_attributes.each do |a|
55
+ if !self.attributes[a.to_s].nil?
56
+ count += 1
57
+ end
58
+ end
59
+
60
+ count
61
+ end
62
+
63
+ def progress_percent
64
+ self.progress_count.to_f / self.progress_complete_count.to_f
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ ActiveRecord::Base.send :include, SoftValidate
@@ -0,0 +1,3 @@
1
+ module SoftValidate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :soft_validate do
3
+ # # Task goes here
4
+ # end
data/test/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ plugin_test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
data/test/debug.log ADDED
@@ -0,0 +1,25 @@
1
+ # Logfile created on 2012-04-10 18:37:57 -0500 by logger.rb/31641
2
+  (1.5ms) select sqlite_version(*)
3
+  (0.3ms) CREATE TABLE "dumb_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "first_name" varchar(255), "last_name" varchar(255))
4
+  (0.1ms) CREATE TABLE "non_validated_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255)) 
5
+  (0.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
6
+  (0.0ms) PRAGMA index_list("schema_migrations")
7
+  (0.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
8
+  (0.1ms) SELECT version FROM "schema_migrations"
9
+  (0.1ms) INSERT INTO "schema_migrations" (version) VALUES ('0')
10
+  (1.1ms) select sqlite_version(*)
11
+  (0.3ms) CREATE TABLE "dumb_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "first_name" varchar(255), "last_name" varchar(255))
12
+  (0.1ms) CREATE TABLE "non_validated_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255)) 
13
+  (0.1ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
14
+  (0.0ms) PRAGMA index_list("schema_migrations")
15
+  (0.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
16
+  (0.0ms) SELECT version FROM "schema_migrations"
17
+  (0.1ms) INSERT INTO "schema_migrations" (version) VALUES ('0')
18
+  (1.5ms) select sqlite_version(*)
19
+  (0.3ms) CREATE TABLE "dumb_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "first_name" varchar(255), "last_name" varchar(255))
20
+  (0.1ms) CREATE TABLE "non_validated_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255)) 
21
+  (0.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
22
+  (0.0ms) PRAGMA index_list("schema_migrations")
23
+  (0.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
24
+  (0.1ms) SELECT version FROM "schema_migrations"
25
+  (0.1ms) INSERT INTO "schema_migrations" (version) VALUES ('0')
File without changes
@@ -0,0 +1,6 @@
1
+  (0.3ms) begin transaction
2
+  (0.0ms) rollback transaction
3
+  (0.3ms) begin transaction
4
+  (0.0ms) rollback transaction
5
+  (0.0ms) begin transaction
6
+  (0.0ms) rollback transaction
@@ -0,0 +1,6 @@
1
+
2
+
3
+ class DumbUser < ActiveRecord::Base
4
+ soft_validates_presence_of :first_name
5
+ soft_validates_presence_of :last_name
6
+ end
@@ -0,0 +1,10 @@
1
+ invalid:
2
+ id: 1
3
+ email: test@example.com
4
+
5
+
6
+ valid:
7
+ id: 2
8
+ email: test@example.com
9
+ first_name: dumb
10
+ last_name: user
@@ -0,0 +1,2 @@
1
+ class NonValidatedUser < ActiveRecord::Base
2
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,11 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :dumb_users, :force => true do |t|
3
+ t.column "email", :string
4
+ t.column "first_name", :string
5
+ t.column "last_name", :string
6
+ end
7
+
8
+ create_table :non_validated_users, :force => true do |t|
9
+ t.column "email", :string
10
+ end
11
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ require 'fixtures/non_validated_user'
4
+
5
+ class SoftValidateTest < TEST_CASE
6
+ fixtures :dumb_users
7
+
8
+ test "truth" do
9
+ assert_kind_of Module, SoftValidate
10
+ end
11
+
12
+ test "a soft validated record will have the #soft_validates_presence_of method" do
13
+ assert DumbUser.instance_methods.include?(:soft_valid?)
14
+ end
15
+
16
+ test 'a dumb user will not be soft valid' do
17
+ assert !DumbUser.new.soft_valid?.nil?
18
+ assert !DumbUser.new.soft_valid?
19
+ end
20
+
21
+ test 'a fully valid user should return true to #soft_valid?' do
22
+ user = DumbUser.new(:email => 'me@you.com', :first_name => 'joe', :last_name => 'schmoe')
23
+ assert user.soft_valid?
24
+ end
25
+
26
+ test 'an invalid user should return error messages' do
27
+ user = DumbUser.new
28
+
29
+ response = user.soft_errors
30
+ assert !response.nil?
31
+ assert !response.empty?
32
+
33
+ assert response[:first_name].eql?("shouldn't be blank")
34
+ assert response[:last_name].eql?("shouldn't be blank")
35
+ end
36
+
37
+ test 'a valid user should return an empty error message response' do
38
+ user = DumbUser.new(:email => 'me@you.com', :first_name => 'joe', :last_name => 'schmoe')
39
+ assert user.soft_errors.empty?
40
+ end
41
+
42
+ test 'The progress count should reflect the number of soft validated attributes that are populated' do
43
+ user = DumbUser.new(:email => 'me@you.com', :first_name => 'joe')
44
+ assert user.progress_count == 1
45
+
46
+ user = DumbUser.new(:email => 'me@you.com', :first_name => 'joe', :last_name => 'schmoe')
47
+ assert user.progress_count == 2
48
+ end
49
+
50
+ test 'The progress complete count should be the number of soft validated attributes' do
51
+ assert DumbUser.new.progress_complete_count == 2
52
+ end
53
+
54
+ test 'Calling #progress_percent on a partially populated instance should return the right value' do
55
+ user = DumbUser.new(:email => 'me@you.com', :first_name => 'joe')
56
+ assert user.progress_percent == 0.50
57
+ end
58
+
59
+ test 'we can instantiate a DumbUser' do
60
+ ::DumbUser.new({})
61
+ assert true
62
+ end
63
+
64
+ end
@@ -0,0 +1,41 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'soft_validate'
4
+
5
+ require 'rubygems'
6
+ require 'test/unit'
7
+ require 'active_record'
8
+ require 'active_record/fixtures'
9
+ # require "#{File.dirname(__FILE__)}/../init"
10
+
11
+ # require 'fixtures/dumb_user'
12
+
13
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
14
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
15
+ ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'plugin_test'])
16
+
17
+ load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
18
+
19
+ if ActiveSupport.const_defined?(:TestCase)
20
+ ActiveSupport::TestCase.send(:include, ActiveRecord::TestFixtures)
21
+ TEST_CASE = ActiveSupport::TestCase
22
+ else
23
+ TEST_CASE = Test::Unit::TestCase
24
+ end
25
+
26
+ TEST_CASE.fixture_path = File.dirname(__FILE__) + "/fixtures/"
27
+ $:.unshift(TEST_CASE.fixture_path)
28
+
29
+ class TEST_CASE #:nodoc:
30
+ def create_fixtures(*table_names)
31
+ if block_given?
32
+ Fixtures.create_fixtures(TEST_CASE.fixture_path, table_names) { yield }
33
+ else
34
+ Fixtures.create_fixtures(TEST_CASE.fixture_path, table_names)
35
+ end
36
+ end
37
+
38
+ self.use_transactional_fixtures = false
39
+
40
+ self.use_instantiated_fixtures = false
41
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soft_validate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Christiansen
9
+ - Jason Vasquez
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-04-11 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: &70095288178760 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.2.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70095288178760
26
+ - !ruby/object:Gem::Dependency
27
+ name: sqlite3
28
+ requirement: &70095288177280 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70095288177280
37
+ description: Add softs_validate_presence_of :attr to your model and it will give you
38
+ methods to figure out percent complete, missing fields, etc without preventing the
39
+ record from being saved.
40
+ email:
41
+ - dave@developertown.com
42
+ - jason.vasquez@developertown.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/soft_validate/version.rb
48
+ - lib/soft_validate.rb
49
+ - lib/tasks/soft_validate_tasks.rake
50
+ - MIT-LICENSE
51
+ - Rakefile
52
+ - README.md
53
+ - test/database.yml
54
+ - test/debug.log
55
+ - test/dummy/db/test.sqlite3
56
+ - test/dummy/log/test.log
57
+ - test/fixtures/dumb_user.rb
58
+ - test/fixtures/dumb_users.yml
59
+ - test/fixtures/non_validated_user.rb
60
+ - test/schema.rb
61
+ - test/soft_validate_test.rb
62
+ - test/test_helper.rb
63
+ homepage: https://github.com/developertown/soft_validate
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.15
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Makes it easy to suggest that certain fields be provided, LinkedIn profile
87
+ style
88
+ test_files:
89
+ - test/database.yml
90
+ - test/debug.log
91
+ - test/dummy/db/test.sqlite3
92
+ - test/dummy/log/test.log
93
+ - test/fixtures/dumb_user.rb
94
+ - test/fixtures/dumb_users.yml
95
+ - test/fixtures/non_validated_user.rb
96
+ - test/schema.rb
97
+ - test/soft_validate_test.rb
98
+ - test/test_helper.rb