lev 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/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +52 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lev.gemspec +27 -0
- data/lib/lev/.DS_Store +0 -0
- data/lib/lev/algorithm.rb +26 -0
- data/lib/lev/better_active_model_errors.rb +408 -0
- data/lib/lev/delegate_to_algorithm.rb +25 -0
- data/lib/lev/exceptions.rb +1 -0
- data/lib/lev/form_builder.rb +63 -0
- data/lib/lev/handle_with.rb +48 -0
- data/lib/lev/handler/.DS_Store +0 -0
- data/lib/lev/handler/error.rb +30 -0
- data/lib/lev/handler/error_transferer.rb +28 -0
- data/lib/lev/handler/error_translator.rb +20 -0
- data/lib/lev/handler/errors.rb +19 -0
- data/lib/lev/handler.rb +194 -0
- data/lib/lev/handler_helper.rb +3 -0
- data/lib/lev/routine_nesting.rb +127 -0
- data/lib/lev/transaction_isolation.rb +59 -0
- data/lib/lev/version.rb +3 -0
- data/lib/lev.rb +57 -0
- metadata +158 -0
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
lev
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-1.9.3-p392
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
lev (0.0.1)
|
5
|
+
active_attr
|
6
|
+
transaction_isolation
|
7
|
+
transaction_retry
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
active_attr (0.8.2)
|
13
|
+
activemodel (>= 3.0.2, < 4.1)
|
14
|
+
activesupport (>= 3.0.2, < 4.1)
|
15
|
+
activemodel (4.0.0)
|
16
|
+
activesupport (= 4.0.0)
|
17
|
+
builder (~> 3.1.0)
|
18
|
+
activerecord (4.0.0)
|
19
|
+
activemodel (= 4.0.0)
|
20
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
21
|
+
activesupport (= 4.0.0)
|
22
|
+
arel (~> 4.0.0)
|
23
|
+
activerecord-deprecated_finders (1.0.3)
|
24
|
+
activesupport (4.0.0)
|
25
|
+
i18n (~> 0.6, >= 0.6.4)
|
26
|
+
minitest (~> 4.2)
|
27
|
+
multi_json (~> 1.3)
|
28
|
+
thread_safe (~> 0.1)
|
29
|
+
tzinfo (~> 0.3.37)
|
30
|
+
arel (4.0.0)
|
31
|
+
atomic (1.1.14)
|
32
|
+
builder (3.1.4)
|
33
|
+
i18n (0.6.5)
|
34
|
+
minitest (4.7.5)
|
35
|
+
multi_json (1.8.0)
|
36
|
+
rake (10.1.0)
|
37
|
+
thread_safe (0.1.3)
|
38
|
+
atomic
|
39
|
+
transaction_isolation (1.0.3)
|
40
|
+
activerecord (>= 3.0.11)
|
41
|
+
transaction_retry (1.0.2)
|
42
|
+
activerecord (>= 3.0.11)
|
43
|
+
transaction_isolation (>= 1.0.2)
|
44
|
+
tzinfo (0.3.37)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
bundler (~> 1.3)
|
51
|
+
lev!
|
52
|
+
rake
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 JP Slavinsky
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Lev
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'lev'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install lev
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lev.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lev/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lev"
|
8
|
+
spec.version = Lev::VERSION
|
9
|
+
spec.authors = ["JP Slavinsky"]
|
10
|
+
spec.email = ["jps@kindlinglabs.com"]
|
11
|
+
spec.description = %q{Ride the rails but don't touch them.}
|
12
|
+
spec.summary = %q{Ride the rails but don't touch them.}
|
13
|
+
spec.homepage = "http://github.com/lml/lev"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "transaction_isolation"
|
22
|
+
spec.add_dependency "transaction_retry"
|
23
|
+
spec.add_dependency "active_attr"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
end
|
data/lib/lev/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Lev
|
2
|
+
# http://ducktypo.blogspot.com/2010/08/why-inheritance-sucks.html
|
3
|
+
# http://stackoverflow.com/a/1328093/1664216
|
4
|
+
module Algorithm
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
base.class_eval do
|
9
|
+
include Lev::RoutineNesting
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(*args, &block)
|
14
|
+
in_transaction do
|
15
|
+
exec(*args, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
def call(*args, &block)
|
21
|
+
new.call(*args, &block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,408 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'active_support/core_ext/array/wrap'
|
4
|
+
require 'active_support/core_ext/array/conversions'
|
5
|
+
require 'active_support/core_ext/string/inflections'
|
6
|
+
require 'active_support/core_ext/object/blank'
|
7
|
+
require 'active_support/core_ext/hash/reverse_merge'
|
8
|
+
require 'active_support/ordered_hash'
|
9
|
+
|
10
|
+
module Lev
|
11
|
+
# == Better Errors
|
12
|
+
#
|
13
|
+
# Same as ActiveModel::Errors but also retains error types
|
14
|
+
#
|
15
|
+
# Provides a modified +OrderedHash+ that you can include in your object
|
16
|
+
# for handling error messages and interacting with Action Pack helpers.
|
17
|
+
#
|
18
|
+
# A minimal implementation could be:
|
19
|
+
#
|
20
|
+
# class Person
|
21
|
+
#
|
22
|
+
# # Required dependency for ActiveModel::Errors
|
23
|
+
# extend ActiveModel::Naming
|
24
|
+
#
|
25
|
+
# def initialize
|
26
|
+
# @errors = ActiveModel::Errors.new(self)
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# attr_accessor :name
|
30
|
+
# attr_reader :errors
|
31
|
+
#
|
32
|
+
# def validate!
|
33
|
+
# errors.add(:name, "can not be nil") if name == nil
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# # The following methods are needed to be minimally implemented
|
37
|
+
#
|
38
|
+
# def read_attribute_for_validation(attr)
|
39
|
+
# send(attr)
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# def Person.human_attribute_name(attr, options = {})
|
43
|
+
# attr
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# def Person.lookup_ancestors
|
47
|
+
# [self]
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# The last three methods are required in your object for Errors to be
|
53
|
+
# able to generate error messages correctly and also handle multiple
|
54
|
+
# languages. Of course, if you extend your object with ActiveModel::Translation
|
55
|
+
# you will not need to implement the last two. Likewise, using
|
56
|
+
# ActiveModel::Validations will handle the validation related methods
|
57
|
+
# for you.
|
58
|
+
#
|
59
|
+
# The above allows you to do:
|
60
|
+
#
|
61
|
+
# p = Person.new
|
62
|
+
# p.validate! # => ["can not be nil"]
|
63
|
+
# p.errors.full_messages # => ["name can not be nil"]
|
64
|
+
# # etc..
|
65
|
+
class BetterActiveModelErrors
|
66
|
+
include Enumerable
|
67
|
+
|
68
|
+
CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict]
|
69
|
+
|
70
|
+
attr_reader :messages
|
71
|
+
attr_reader :types
|
72
|
+
|
73
|
+
# Pass in the instance of the object that is using the errors object.
|
74
|
+
#
|
75
|
+
# class Person
|
76
|
+
# def initialize
|
77
|
+
# @errors = ActiveModel::Errors.new(self)
|
78
|
+
# end
|
79
|
+
# end
|
80
|
+
def initialize(base)
|
81
|
+
@base = base
|
82
|
+
@types = ActiveSupport::OrderedHash.new
|
83
|
+
@messages = ActiveSupport::OrderedHash.new
|
84
|
+
end
|
85
|
+
|
86
|
+
def initialize_dup(other)
|
87
|
+
@types = other.types.dup
|
88
|
+
@messages = other.messages.dup
|
89
|
+
end
|
90
|
+
|
91
|
+
# Backport dup from 1.9 so that #initialize_dup gets called
|
92
|
+
unless Object.respond_to?(:initialize_dup, true)
|
93
|
+
def dup # :nodoc:
|
94
|
+
copy = super
|
95
|
+
copy.initialize_dup(self)
|
96
|
+
copy
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Clear the messages
|
101
|
+
def clear
|
102
|
+
types.clear
|
103
|
+
messages.clear
|
104
|
+
end
|
105
|
+
|
106
|
+
# Do the error messages include an error with key +error+?
|
107
|
+
def include?(error)
|
108
|
+
(v = messages[error]) && v.any?
|
109
|
+
end
|
110
|
+
alias :has_key? :include?
|
111
|
+
|
112
|
+
# Get messages for +key+
|
113
|
+
def get(key)
|
114
|
+
messages[key]
|
115
|
+
end
|
116
|
+
|
117
|
+
def get_type(key)
|
118
|
+
types[key]
|
119
|
+
end
|
120
|
+
|
121
|
+
# Set messages for +key+ to +value+
|
122
|
+
def set(key, value)
|
123
|
+
types[key] = (value == [] ? [] : (value.is_a?(Symbol) ? value : nil))
|
124
|
+
messages[key] = value
|
125
|
+
end
|
126
|
+
|
127
|
+
# Delete messages for +key+
|
128
|
+
def delete(key)
|
129
|
+
types.delete(key)
|
130
|
+
messages.delete(key)
|
131
|
+
end
|
132
|
+
|
133
|
+
# When passed a symbol or a name of a method, returns an array of errors
|
134
|
+
# for the method.
|
135
|
+
#
|
136
|
+
# p.errors[:name] # => ["can not be nil"]
|
137
|
+
# p.errors['name'] # => ["can not be nil"]
|
138
|
+
def [](attribute)
|
139
|
+
get(attribute.to_sym) || set(attribute.to_sym, [])
|
140
|
+
end
|
141
|
+
|
142
|
+
# Adds to the supplied attribute the supplied error message.
|
143
|
+
#
|
144
|
+
# p.errors[:name] = "must be set"
|
145
|
+
# p.errors[:name] # => ['must be set']
|
146
|
+
def []=(attribute, error)
|
147
|
+
self[attribute] << error
|
148
|
+
end
|
149
|
+
|
150
|
+
# Iterates through each error key, value pair in the error messages hash.
|
151
|
+
# Yields the attribute and the error for that attribute. If the attribute
|
152
|
+
# has more than one error message, yields once for each error message.
|
153
|
+
#
|
154
|
+
# p.errors.add(:name, "can't be blank")
|
155
|
+
# p.errors.each do |attribute, errors_array|
|
156
|
+
# # Will yield :name and "can't be blank"
|
157
|
+
# end
|
158
|
+
#
|
159
|
+
# p.errors.add(:name, "must be specified")
|
160
|
+
# p.errors.each do |attribute, errors_array|
|
161
|
+
# # Will yield :name and "can't be blank"
|
162
|
+
# # then yield :name and "must be specified"
|
163
|
+
# end
|
164
|
+
def each
|
165
|
+
messages.each_key do |attribute|
|
166
|
+
self[attribute].each { |error| yield attribute, error }
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def each_with_type_and_message
|
171
|
+
types.each_key do |attribute|
|
172
|
+
for ii in 0..self.types[attribute].size-1
|
173
|
+
yield attribute, self.types[attribute][ii], self.messages[attribute][ii]
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# Returns the number of error messages.
|
179
|
+
#
|
180
|
+
# p.errors.add(:name, "can't be blank")
|
181
|
+
# p.errors.size # => 1
|
182
|
+
# p.errors.add(:name, "must be specified")
|
183
|
+
# p.errors.size # => 2
|
184
|
+
def size
|
185
|
+
values.flatten.size
|
186
|
+
end
|
187
|
+
|
188
|
+
# Returns all message values
|
189
|
+
def values
|
190
|
+
messages.values
|
191
|
+
end
|
192
|
+
|
193
|
+
# Returns all message keys
|
194
|
+
def keys
|
195
|
+
messages.keys
|
196
|
+
end
|
197
|
+
|
198
|
+
# Returns an array of error messages, with the attribute name included
|
199
|
+
#
|
200
|
+
# p.errors.add(:name, "can't be blank")
|
201
|
+
# p.errors.add(:name, "must be specified")
|
202
|
+
# p.errors.to_a # => ["name can't be blank", "name must be specified"]
|
203
|
+
def to_a
|
204
|
+
full_messages
|
205
|
+
end
|
206
|
+
|
207
|
+
# Returns the number of error messages.
|
208
|
+
# p.errors.add(:name, "can't be blank")
|
209
|
+
# p.errors.count # => 1
|
210
|
+
# p.errors.add(:name, "must be specified")
|
211
|
+
# p.errors.count # => 2
|
212
|
+
def count
|
213
|
+
to_a.size
|
214
|
+
end
|
215
|
+
|
216
|
+
# Returns true if no errors are found, false otherwise.
|
217
|
+
# If the error message is a string it can be empty.
|
218
|
+
def empty?
|
219
|
+
all? { |k, v| v && v.empty? && !v.is_a?(String) }
|
220
|
+
end
|
221
|
+
alias_method :blank?, :empty?
|
222
|
+
|
223
|
+
# Returns an xml formatted representation of the Errors hash.
|
224
|
+
#
|
225
|
+
# p.errors.add(:name, "can't be blank")
|
226
|
+
# p.errors.add(:name, "must be specified")
|
227
|
+
# p.errors.to_xml
|
228
|
+
# # =>
|
229
|
+
# # <?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
230
|
+
# # <errors>
|
231
|
+
# # <error>name can't be blank</error>
|
232
|
+
# # <error>name must be specified</error>
|
233
|
+
# # </errors>
|
234
|
+
def to_xml(options={})
|
235
|
+
to_a.to_xml options.reverse_merge(:root => "errors", :skip_types => true)
|
236
|
+
end
|
237
|
+
|
238
|
+
# Returns an ActiveSupport::OrderedHash that can be used as the JSON representation for this object.
|
239
|
+
def as_json(options=nil)
|
240
|
+
to_hash
|
241
|
+
end
|
242
|
+
|
243
|
+
def to_hash
|
244
|
+
messages.dup
|
245
|
+
end
|
246
|
+
|
247
|
+
# Adds +message+ to the error messages on +attribute+. More than one error can be added to the same
|
248
|
+
# +attribute+.
|
249
|
+
# If no +message+ is supplied, <tt>:invalid</tt> is assumed.
|
250
|
+
#
|
251
|
+
# If +message+ is a symbol, it will be translated using the appropriate scope (see +translate_error+).
|
252
|
+
# If +message+ is a proc, it will be called, allowing for things like <tt>Time.now</tt> to be used within an error.
|
253
|
+
def add(attribute, message = nil, options = {})
|
254
|
+
normalized_message = normalize_message(attribute, message, options)
|
255
|
+
if options[:strict]
|
256
|
+
raise ActiveModel::StrictValidationFailed, full_message(attribute, normalized_message)
|
257
|
+
end
|
258
|
+
|
259
|
+
self[attribute] << normalized_message
|
260
|
+
self.types[attribute] << message if message.is_a?(Symbol)
|
261
|
+
end
|
262
|
+
|
263
|
+
# Will add an error message to each of the attributes in +attributes+ that is empty.
|
264
|
+
def add_on_empty(attributes, options = {})
|
265
|
+
[attributes].flatten.each do |attribute|
|
266
|
+
value = @base.send(:read_attribute_for_validation, attribute)
|
267
|
+
is_empty = value.respond_to?(:empty?) ? value.empty? : false
|
268
|
+
add(attribute, :empty, options) if value.nil? || is_empty
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
# Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?).
|
273
|
+
def add_on_blank(attributes, options = {})
|
274
|
+
[attributes].flatten.each do |attribute|
|
275
|
+
value = @base.send(:read_attribute_for_validation, attribute)
|
276
|
+
add(attribute, :blank, options) if value.blank?
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
# Returns true if an error on the attribute with the given message is present, false otherwise.
|
281
|
+
# +message+ is treated the same as for +add+.
|
282
|
+
# p.errors.add :name, :blank
|
283
|
+
# p.errors.added? :name, :blank # => true
|
284
|
+
def added?(attribute, message = nil, options = {})
|
285
|
+
message = normalize_message(attribute, message, options)
|
286
|
+
self[attribute].include? message
|
287
|
+
end
|
288
|
+
|
289
|
+
# Returns all the full error messages in an array.
|
290
|
+
#
|
291
|
+
# class Company
|
292
|
+
# validates_presence_of :name, :address, :email
|
293
|
+
# validates_length_of :name, :in => 5..30
|
294
|
+
# end
|
295
|
+
#
|
296
|
+
# company = Company.create(:address => '123 First St.')
|
297
|
+
# company.errors.full_messages # =>
|
298
|
+
# ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"]
|
299
|
+
def full_messages
|
300
|
+
map { |attribute, message| full_message(attribute, message) }
|
301
|
+
end
|
302
|
+
|
303
|
+
# Returns a full message for a given attribute.
|
304
|
+
#
|
305
|
+
# company.errors.full_message(:name, "is invalid") # =>
|
306
|
+
# "Name is invalid"
|
307
|
+
def full_message(attribute, message)
|
308
|
+
self.class.full_message(@base, attribute, message)
|
309
|
+
end
|
310
|
+
|
311
|
+
def self.full_message(model, attribute, message)
|
312
|
+
return message if attribute == :base
|
313
|
+
attr_name = attribute.to_s.gsub('.', '_').humanize
|
314
|
+
attr_name = model.class.human_attribute_name(attribute, :default => attr_name)
|
315
|
+
I18n.t(:"errors.format", {
|
316
|
+
:default => "%{attribute} %{message}",
|
317
|
+
:attribute => attr_name,
|
318
|
+
:message => message
|
319
|
+
})
|
320
|
+
end
|
321
|
+
|
322
|
+
# Translates an error message in its default scope
|
323
|
+
# (<tt>activemodel.errors.messages</tt>).
|
324
|
+
#
|
325
|
+
# Error messages are first looked up in <tt>models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>,
|
326
|
+
# if it's not there, it's looked up in <tt>models.MODEL.MESSAGE</tt> and if that is not
|
327
|
+
# there also, it returns the translation of the default message
|
328
|
+
# (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model name,
|
329
|
+
# translated attribute name and the value are available for interpolation.
|
330
|
+
#
|
331
|
+
# When using inheritance in your models, it will check all the inherited
|
332
|
+
# models too, but only if the model itself hasn't been found. Say you have
|
333
|
+
# <tt>class Admin < User; end</tt> and you wanted the translation for
|
334
|
+
# the <tt>:blank</tt> error message for the <tt>title</tt> attribute,
|
335
|
+
# it looks for these translations:
|
336
|
+
#
|
337
|
+
# * <tt>activemodel.errors.models.admin.attributes.title.blank</tt>
|
338
|
+
# * <tt>activemodel.errors.models.admin.blank</tt>
|
339
|
+
# * <tt>activemodel.errors.models.user.attributes.title.blank</tt>
|
340
|
+
# * <tt>activemodel.errors.models.user.blank</tt>
|
341
|
+
# * any default you provided through the +options+ hash (in the <tt>activemodel.errors</tt> scope)
|
342
|
+
# * <tt>activemodel.errors.messages.blank</tt>
|
343
|
+
# * <tt>errors.attributes.title.blank</tt>
|
344
|
+
# * <tt>errors.messages.blank</tt>
|
345
|
+
#
|
346
|
+
def generate_message(attribute, type = :invalid, options = {})
|
347
|
+
self.class.generate_message(@base, attribute, type, options)
|
348
|
+
end
|
349
|
+
|
350
|
+
# TODO maybe don't need this method split out any more?
|
351
|
+
def self.generate_message(model, attribute, type = :invalid, options = {})
|
352
|
+
type = options.delete(:message) if options[:message].is_a?(Symbol)
|
353
|
+
|
354
|
+
if model.class.respond_to?(:i18n_scope)
|
355
|
+
defaults = model.class.lookup_ancestors.map do |klass|
|
356
|
+
[ :"#{model.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
|
357
|
+
:"#{model.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
|
358
|
+
end
|
359
|
+
else
|
360
|
+
defaults = []
|
361
|
+
end
|
362
|
+
|
363
|
+
defaults << options.delete(:message)
|
364
|
+
defaults << :"#{model.class.i18n_scope}.errors.messages.#{type}" if model.class.respond_to?(:i18n_scope)
|
365
|
+
defaults << :"errors.attributes.#{attribute}.#{type}"
|
366
|
+
defaults << :"errors.messages.#{type}"
|
367
|
+
|
368
|
+
defaults.compact!
|
369
|
+
defaults.flatten!
|
370
|
+
|
371
|
+
key = defaults.shift
|
372
|
+
value = (attribute != :base ? model.send(:read_attribute_for_validation, attribute) : nil)
|
373
|
+
|
374
|
+
options = {
|
375
|
+
:default => defaults,
|
376
|
+
:model => model.class.model_name.human,
|
377
|
+
:attribute => model.class.human_attribute_name(attribute),
|
378
|
+
:value => value
|
379
|
+
}.merge(options)
|
380
|
+
|
381
|
+
I18n.translate(key, options)
|
382
|
+
end
|
383
|
+
|
384
|
+
private
|
385
|
+
def normalize_message(attribute, message, options)
|
386
|
+
message ||= :invalid
|
387
|
+
|
388
|
+
if message.is_a?(Symbol)
|
389
|
+
generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS))
|
390
|
+
elsif message.is_a?(Proc)
|
391
|
+
message.call
|
392
|
+
else
|
393
|
+
message
|
394
|
+
end
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
class StrictValidationFailed < StandardError
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
module ActiveModel
|
403
|
+
module Validations
|
404
|
+
def errors
|
405
|
+
@errors ||= Lev::BetterActiveModelErrors.new(self)
|
406
|
+
end
|
407
|
+
end
|
408
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# ActiveRecord::Base.delegate_to_algorithm
|
2
|
+
#
|
3
|
+
# Let active records delegate certain (likely non-trivial) actions to algoritms
|
4
|
+
#
|
5
|
+
# Arguments:
|
6
|
+
# method: a symbol for the instance method to delegate, e.g. :destroy
|
7
|
+
# options: a hash of options including...
|
8
|
+
# :algorithm_klass => The class of the algorithm to delegate to; if not
|
9
|
+
# given,
|
10
|
+
ActiveRecord::Base.define_singleton_method(:delegate_to_algorithm) do |method, options={}|
|
11
|
+
algorithm_klass = options[:algorithm_klass]
|
12
|
+
|
13
|
+
if algorithm_klass.nil?
|
14
|
+
algorithm_klass_name = "#{method.to_s.capitalize}#{self.name}"
|
15
|
+
algorithm_klass = Kernel.const_get(algorithm_klass_name)
|
16
|
+
end
|
17
|
+
|
18
|
+
self.instance_eval do
|
19
|
+
alias_method "#{method}_original".to_sym, method
|
20
|
+
define_method method do
|
21
|
+
algorithm_klass.call(self)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
class IsolationMismatch < StandardError; end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Lev
|
2
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
3
|
+
|
4
|
+
(field_helpers - %w(label check_box radio_button fields_for file_field)).each do |selector|
|
5
|
+
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
6
|
+
def #{selector}(method, options = {}) # def text_field(method, options = {})
|
7
|
+
set_value_if_available(method, options) # ... verbatim ...
|
8
|
+
mark_error_if_present(method, options) # ... verbatim ...
|
9
|
+
super(method, options) # ... verbatim ...
|
10
|
+
end # end
|
11
|
+
RUBY_EVAL
|
12
|
+
end
|
13
|
+
|
14
|
+
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
|
15
|
+
options[:checked] = true if get_form_params_entry(method).to_i > 0
|
16
|
+
mark_error_if_present(method, options)
|
17
|
+
super(method, options, checked_value, unchecked_value)
|
18
|
+
end
|
19
|
+
|
20
|
+
def radio_button(method, tag_value, options = {})
|
21
|
+
options[:checked] = true if get_form_params_entry(method) == tag_value
|
22
|
+
mark_error_if_present(method, options)
|
23
|
+
super(method, tag_value, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def fields_for(record_name, record_object = nil, fields_options = {}, &block)
|
27
|
+
raise "Didn't put fields_for into LevitateFormBuilder yet"
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def get_form_params_entry(name)
|
33
|
+
@options[:params].present? ?
|
34
|
+
(@options[:params][@object_name].present? ?
|
35
|
+
@options[:params][@object_name][name] :
|
36
|
+
nil) :
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def has_error?(name)
|
41
|
+
@options[:errors].present? ? @options[:errors].has_offending_param?([@object_name, name]) : false
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_value_if_available(method, options)
|
45
|
+
value = get_form_params_entry(method)
|
46
|
+
options[:value] ||= value if !value.nil?
|
47
|
+
end
|
48
|
+
|
49
|
+
def mark_error_if_present(method, options)
|
50
|
+
if has_error?(method)
|
51
|
+
options[:class] = [options[:class], Lev.configuration.form_error_class].compact.join(' ')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def lev_form_for(record_or_name_or_array, *args, &proc)
|
59
|
+
options = args.extract_options!
|
60
|
+
options[:params] = params
|
61
|
+
options[:errors] = @errors || []
|
62
|
+
form_for(record_or_name_or_array, *(args << options.merge(:builder => Lev::FormBuilder)), &proc)
|
63
|
+
end
|