warnr 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *~
2
+ pkg/
3
+
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
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 ADDED
@@ -0,0 +1,27 @@
1
+ Warnr
2
+ ======
3
+
4
+ Warnr builds on the power of Rails 3 validations.
5
+ It lets you use validations to identify situations which are warnings rather than errors.
6
+ It also lets you define a callback on the model which is executed after save if there are any warnings.
7
+
8
+ Usage Example
9
+
10
+ class Client < ActiveRecord::Base
11
+ # Notify the client manager that the ABN has not been set
12
+ belongs_to :manager, :class_name => "User"
13
+
14
+ validates_presence_of :abn, :manager_id
15
+
16
+ treat_validation_errors_as_warnings_on :abn
17
+ on_save_with_warnings :handle_warnings
18
+
19
+ def handle_warnings
20
+ MissingDataNotifier.missing_data_notification(client.manager, client).deliver
21
+ # Will save the record in a week, which will trigger this process again.
22
+ DelayedJob.create(1.week, Client, client.id, :save)
23
+ end
24
+ end
25
+
26
+
27
+ Copyright (c) 2010 Daniel Heath, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ require 'rake'
2
+ require 'rubygems'
3
+
4
+ require 'spec/rake/spectask'
5
+ require 'rake/rdoctask'
6
+
7
+ desc 'Default: run unit tests.'
8
+ task :default => :test
9
+
10
+ desc 'Generate documentation for the warner plugin.'
11
+ Rake::RDocTask.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'Warnr'
14
+ rdoc.options << '--line-numbers' << '--inline-source'
15
+ rdoc.rdoc_files.include('README')
16
+ rdoc.rdoc_files.include('lib/*.rb')
17
+ end
18
+
19
+ desc 'Test the warner plugin.'
20
+ Spec::Rake::SpecTask.new('test') do |t|
21
+ t.spec_files = FileList['spec/*.rb']
22
+ end
23
+
24
+ begin
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |gemspec|
27
+ gemspec.name = "warnr"
28
+ gemspec.summary = "Non-fatal rails 3 validations (ie, warnings rather than errors)"
29
+ gemspec.description = <<EOF
30
+ Ernr builds on the power of Rails 3 validations.
31
+ It lets you use validations to identify situations which are warnings rather than errors.
32
+ It also lets you define a callback on the model which is executed after save if there are any warnings.
33
+ EOF
34
+ gemspec.email = "daniel.heath@gmail.com"
35
+ gemspec.homepage = "http://github.com/DanielHeath/warnr"
36
+ gemspec.authors = ["Daniel Heath"]
37
+ end
38
+ Jeweler::GemcutterTasks.new
39
+ rescue LoadError
40
+ puts "Jeweler not available. Install it with: gem install jeweler"
41
+ end
42
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ # Include hook code here
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
data/lib/warnr.rb ADDED
@@ -0,0 +1,81 @@
1
+ require 'active_record'
2
+
3
+ module Warnr
4
+
5
+ # Patches ActiveRecord to:
6
+ # * Store a list of 'warning' fields
7
+ # * Store a list of callbacks for 'after save, if warnings exist'
8
+ # * Store a list of warning messages
9
+ # * Move messages out of the errors object if they belong in the warning object
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+ base.class_eval do
13
+ class_inheritable_accessor :warnr_warning_fields
14
+ self.warnr_warning_fields = []
15
+ define_callbacks :on_save_with_warnings
16
+ attr_reader :warnings
17
+ after_initialize :setup_warnr_warnings
18
+ alias_method_chain 'valid?', 'warnr'
19
+ end
20
+ end
21
+
22
+ module ClassMethods
23
+
24
+ # Specify a list of fields to work on.
25
+ # Validation errors on these fields will be ignored.
26
+ def treat_validation_errors_as_warnings_on(*fields)
27
+ self.warnr_warning_fields = self.warnr_warning_fields | fields
28
+ end
29
+
30
+ # Pass a method name; sets up a callback which runs after create_or_update.
31
+ def on_save_with_warnings(method)
32
+ set_callback(:on_save_with_warnings, :after, method)
33
+ end
34
+
35
+ end
36
+
37
+ private
38
+
39
+ def create_or_update # :nodoc:
40
+ super
41
+ run_callbacks :on_save_with_warnings if errors.empty? and not warnings.empty?
42
+ end
43
+
44
+ def setup_warnr_warnings # :nodoc:
45
+ @warnings = ActiveModel::Errors.new(self)
46
+ end
47
+
48
+ # Ugly hack; rather than preventing errors getting added, we move them afterwards.
49
+ # Alternative may be to modify the ActiveModel validation base class
50
+ # so that it passes a record proxy to the validations that can then decide
51
+ # whether to return the errors or warnings collection when record.errors is called.
52
+ def move_errors_to_warnings
53
+ self.class.warnr_warning_fields.each do |field|
54
+ if errors[field]
55
+ errors[field].each { |error| warnings.add(field, error) }
56
+ errors.delete(field)
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ module ActiveRecord::Validations::InstanceMethods
63
+ # Using alias_method_chain (ugly!) to wrap the valid? method.
64
+ def valid_with_warnr?( *args )
65
+ warnings.clear
66
+
67
+ valid_without_warnr?
68
+
69
+ # WARNR: Moves any warning-level validation errors to the warnings collection
70
+ move_errors_to_warnings
71
+ # WARNR: Runs warning block if defined
72
+ run_callbacks :on_save_with_warnings if errors.empty? and not warnings.empty?
73
+
74
+ errors.empty?
75
+ end
76
+ end
77
+
78
+ ActiveRecord::Base.class_eval do
79
+ include(Warnr)
80
+ end
81
+
@@ -0,0 +1,152 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: ActiveRecord::Validations::InstanceMethods</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">ActiveRecord::Validations::InstanceMethods</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../../../files/lib/warnr_rb.html">
59
+ lib/warnr.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ </table>
66
+ </div>
67
+ <!-- banner header -->
68
+
69
+ <div id="bodyContent">
70
+
71
+
72
+
73
+ <div id="contextContent">
74
+
75
+
76
+
77
+ </div>
78
+
79
+ <div id="method-list">
80
+ <h3 class="section-bar">Methods</h3>
81
+
82
+ <div class="name-list">
83
+ <a href="#M000001">valid_with_warnr?</a>&nbsp;&nbsp;
84
+ </div>
85
+ </div>
86
+
87
+ </div>
88
+
89
+
90
+ <!-- if includes -->
91
+
92
+ <div id="section">
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+ <!-- if method_list -->
102
+ <div id="methods">
103
+ <h3 class="section-bar">Public Instance methods</h3>
104
+
105
+ <div id="method-M000001" class="method-detail">
106
+ <a name="M000001"></a>
107
+
108
+ <div class="method-heading">
109
+ <a href="#M000001" class="method-signature">
110
+ <span class="method-name">valid_with_warnr?</span><span class="method-args">( *args )</span>
111
+ </a>
112
+ </div>
113
+
114
+ <div class="method-description">
115
+ <p>
116
+ Using alias_method_chain (ugly!) to wrap the valid? method.
117
+ </p>
118
+ <p><a class="source-toggle" href="#"
119
+ onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
120
+ <div class="method-source-code" id="M000001-source">
121
+ <pre>
122
+ <span class="ruby-comment cmt"># File lib/warnr.rb, line 64</span>
123
+ 64: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">valid_with_warnr?</span>( <span class="ruby-operator">*</span><span class="ruby-identifier">args</span> )
124
+ 65: <span class="ruby-identifier">warnings</span>.<span class="ruby-identifier">clear</span>
125
+ 66:
126
+ 67: <span class="ruby-identifier">valid_without_warnr?</span>
127
+ 68:
128
+ 69: <span class="ruby-comment cmt"># WARNR: Moves any warning-level validation errors to the warnings collection</span>
129
+ 70: <span class="ruby-identifier">move_errors_to_warnings</span>
130
+ 71: <span class="ruby-comment cmt"># WARNR: Runs warning block if defined</span>
131
+ 72: <span class="ruby-identifier">run_callbacks</span> <span class="ruby-identifier">:on_save_with_warnings</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-keyword kw">and</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">warnings</span>.<span class="ruby-identifier">empty?</span>
132
+ 73:
133
+ 74: <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">empty?</span>
134
+ 75: <span class="ruby-keyword kw">end</span>
135
+ </pre>
136
+ </div>
137
+ </div>
138
+ </div>
139
+
140
+
141
+ </div>
142
+
143
+
144
+ </div>
145
+
146
+
147
+ <div id="validator-badges">
148
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
149
+ </div>
150
+
151
+ </body>
152
+ </html>
@@ -0,0 +1,107 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: ActiveRecord::Validations</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">ActiveRecord::Validations</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ </td>
59
+ </tr>
60
+
61
+ </table>
62
+ </div>
63
+ <!-- banner header -->
64
+
65
+ <div id="bodyContent">
66
+
67
+
68
+
69
+ <div id="contextContent">
70
+
71
+
72
+
73
+ </div>
74
+
75
+
76
+ </div>
77
+
78
+
79
+ <!-- if includes -->
80
+
81
+ <div id="section">
82
+
83
+ <div id="class-list">
84
+ <h3 class="section-bar">Classes and Modules</h3>
85
+
86
+ Module <a href="Validations/InstanceMethods.html" class="link">ActiveRecord::Validations::InstanceMethods</a><br />
87
+
88
+ </div>
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+ <!-- if method_list -->
97
+
98
+
99
+ </div>
100
+
101
+
102
+ <div id="validator-badges">
103
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
104
+ </div>
105
+
106
+ </body>
107
+ </html>
@@ -0,0 +1,107 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: ActiveRecord</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">ActiveRecord</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ </td>
59
+ </tr>
60
+
61
+ </table>
62
+ </div>
63
+ <!-- banner header -->
64
+
65
+ <div id="bodyContent">
66
+
67
+
68
+
69
+ <div id="contextContent">
70
+
71
+
72
+
73
+ </div>
74
+
75
+
76
+ </div>
77
+
78
+
79
+ <!-- if includes -->
80
+
81
+ <div id="section">
82
+
83
+ <div id="class-list">
84
+ <h3 class="section-bar">Classes and Modules</h3>
85
+
86
+ Module <a href="ActiveRecord/Validations.html" class="link">ActiveRecord::Validations</a><br />
87
+
88
+ </div>
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+ <!-- if method_list -->
97
+
98
+
99
+ </div>
100
+
101
+
102
+ <div id="validator-badges">
103
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
104
+ </div>
105
+
106
+ </body>
107
+ </html>