save_whats_valid 0.0.1
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.
- data/MIT-LICENSE +20 -0
- data/README.md +34 -0
- data/Rakefile +27 -0
- data/lib/save_whats_valid.rb +50 -0
- data/lib/save_whats_valid/version.rb +3 -0
- data/lib/tasks/save_whats_valid_tasks.rake +4 -0
- metadata +120 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
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,34 @@
|
|
1
|
+
# Save What's Valid
|
2
|
+
|
3
|
+
Rails' default `update_attributes` behavior is that when any validation fails,
|
4
|
+
none of the data gets saved. However, in some instances this is not the most
|
5
|
+
ideal behavior. If you import partial data which users then have the task of
|
6
|
+
completing, not saving partially completed forms can create a really negative
|
7
|
+
user experience and decrease overall completion (or conversion) rates.
|
8
|
+
|
9
|
+
For example, some users complained when their incomplete changes didn't stick
|
10
|
+
bceause they had intended to send the remainder of the form to a colleague to
|
11
|
+
complete the rest.
|
12
|
+
|
13
|
+
This gem encapsulates the logic I've used to ensure that partially completed
|
14
|
+
forms still persist.
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
class Person < ActiveRecord::Base
|
19
|
+
|
20
|
+
# Include the module.
|
21
|
+
include SaveWhatsValid
|
22
|
+
|
23
|
+
# These are the attributes available on the model.
|
24
|
+
attr_accessible :age, :description, :first_name, :last_name, :middle_initial
|
25
|
+
|
26
|
+
# These two attributes are required.
|
27
|
+
validates_presence_of :first_name
|
28
|
+
validates_presence_of :last_name
|
29
|
+
|
30
|
+
# These are the attributes we want to save any valid value for.
|
31
|
+
save_whats_valid :age, :description, :first_name, :last_name
|
32
|
+
|
33
|
+
end
|
34
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
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 = 'SaveWhatsValid'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'rspec/core/rake_task'
|
24
|
+
RSpec::Core::RakeTask.new('spec')
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module SaveWhatsValid
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
attr_accessor :save_whats_valid_attributes
|
6
|
+
def save_whats_valid(*attributes)
|
7
|
+
self.save_whats_valid_attributes = attributes
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def update_attributes(attributes)
|
12
|
+
saved_successfully = super(attributes)
|
13
|
+
|
14
|
+
if self.class.save_whats_valid_attributes.present?
|
15
|
+
|
16
|
+
# load a copy of the listing.
|
17
|
+
copy = self.class.find(id)
|
18
|
+
|
19
|
+
unless saved_successfully
|
20
|
+
|
21
|
+
fields = self.class.save_whats_valid_attributes.map(&:to_s)
|
22
|
+
|
23
|
+
self.changed.each do |changed_field|
|
24
|
+
|
25
|
+
# if this is one of the fields we allow to be updated even when the object itself is invalid...
|
26
|
+
if fields.include? changed_field
|
27
|
+
|
28
|
+
# unless this field is one of the fields with an error...
|
29
|
+
unless self.errors.messages.keys.map(&:to_s).include? changed_field
|
30
|
+
|
31
|
+
# otherwise, just copy over the field we want to save.
|
32
|
+
copy.send("#{changed_field}=", self.send(changed_field))
|
33
|
+
|
34
|
+
# save it regardless of the state of the object.
|
35
|
+
copy.save(validate: false)
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
return saved_successfully
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: save_whats_valid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Culver
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.13
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.13
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec-rails
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Rails' default update_attributes behavior is that when any validation
|
79
|
+
fails, none of the data gets saved. However, in some instances this is not the most
|
80
|
+
ideal behavior. If you import partial data which users then have the task of completing,
|
81
|
+
not saving partially completed forms can create a really negative user experience
|
82
|
+
and decrease overall completion (or conversion) rates. This gem encapsulates the
|
83
|
+
logic I've used to ensure that partially completed forms still persist.
|
84
|
+
email:
|
85
|
+
- andrew.culver@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- lib/save_whats_valid/version.rb
|
91
|
+
- lib/save_whats_valid.rb
|
92
|
+
- lib/tasks/save_whats_valid_tasks.rake
|
93
|
+
- MIT-LICENSE
|
94
|
+
- Rakefile
|
95
|
+
- README.md
|
96
|
+
homepage: http://github.com/andrewculver/save_whats_valid
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.24
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Save some fields even when the form isn't valid.
|
120
|
+
test_files: []
|