activemodel 3.0.0.beta4 → 3.0.pre

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.
Files changed (39) hide show
  1. data/CHANGELOG +1 -39
  2. data/MIT-LICENSE +1 -1
  3. data/README +16 -200
  4. data/lib/active_model.rb +19 -28
  5. data/lib/active_model/attribute_methods.rb +27 -142
  6. data/lib/active_model/conversion.rb +1 -37
  7. data/lib/active_model/dirty.rb +12 -51
  8. data/lib/active_model/errors.rb +22 -146
  9. data/lib/active_model/lint.rb +14 -48
  10. data/lib/active_model/locale/en.yml +23 -26
  11. data/lib/active_model/naming.rb +5 -41
  12. data/lib/active_model/observing.rb +16 -35
  13. data/lib/active_model/serialization.rb +0 -57
  14. data/lib/active_model/serializers/json.rb +8 -13
  15. data/lib/active_model/serializers/xml.rb +123 -63
  16. data/lib/active_model/state_machine.rb +70 -0
  17. data/lib/active_model/state_machine/event.rb +62 -0
  18. data/lib/active_model/state_machine/machine.rb +75 -0
  19. data/lib/active_model/state_machine/state.rb +47 -0
  20. data/lib/active_model/state_machine/state_transition.rb +40 -0
  21. data/lib/active_model/test_case.rb +2 -0
  22. data/lib/active_model/validations.rb +62 -125
  23. data/lib/active_model/validations/acceptance.rb +18 -23
  24. data/lib/active_model/validations/confirmation.rb +10 -14
  25. data/lib/active_model/validations/exclusion.rb +13 -15
  26. data/lib/active_model/validations/format.rb +24 -26
  27. data/lib/active_model/validations/inclusion.rb +13 -15
  28. data/lib/active_model/validations/length.rb +65 -61
  29. data/lib/active_model/validations/numericality.rb +58 -76
  30. data/lib/active_model/validations/presence.rb +8 -8
  31. data/lib/active_model/validations/with.rb +22 -90
  32. data/lib/active_model/validations_repair_helper.rb +35 -0
  33. data/lib/active_model/version.rb +2 -3
  34. metadata +19 -63
  35. data/lib/active_model/callbacks.rb +0 -134
  36. data/lib/active_model/railtie.rb +0 -2
  37. data/lib/active_model/translation.rb +0 -60
  38. data/lib/active_model/validations/validates.rb +0 -108
  39. data/lib/active_model/validator.rb +0 -183
@@ -2,13 +2,7 @@ require 'active_support/core_ext/object/blank'
2
2
 
3
3
  module ActiveModel
4
4
  module Validations
5
- class PresenceValidator < EachValidator
6
- def validate(record)
7
- record.errors.add_on_blank(attributes, options[:message])
8
- end
9
- end
10
-
11
- module HelperMethods
5
+ module ClassMethods
12
6
  # Validates that the specified attributes are not blank (as defined by Object#blank?). Happens by default on save. Example:
13
7
  #
14
8
  # class Person < ActiveRecord::Base
@@ -34,7 +28,13 @@ module ActiveModel
34
28
  # The method, proc or string should return or evaluate to a true or false value.
35
29
  #
36
30
  def validates_presence_of(*attr_names)
37
- validates_with PresenceValidator, _merge_attributes(attr_names)
31
+ configuration = attr_names.extract_options!
32
+
33
+ # can't use validates_each here, because it cannot cope with nonexistent attributes,
34
+ # while errors.add_on_empty can
35
+ validate configuration do |record|
36
+ record.errors.add_on_blank(attr_names, configuration[:message])
37
+ end
38
38
  end
39
39
  end
40
40
  end
@@ -1,25 +1,15 @@
1
1
  module ActiveModel
2
2
  module Validations
3
- module HelperMethods
4
- private
5
- def _merge_attributes(attr_names)
6
- options = attr_names.extract_options!
7
- options.merge(:attributes => attr_names.flatten)
8
- end
9
- end
10
-
11
3
  module ClassMethods
12
4
 
13
- # Passes the record off to the class or classes specified and allows them
14
- # to add errors based on more complex conditions.
5
+ # Passes the record off to the class or classes specified and allows them to add errors based on more complex conditions.
15
6
  #
16
- # class Person
17
- # include ActiveModel::Validations
7
+ # class Person < ActiveRecord::Base
18
8
  # validates_with MyValidator
19
9
  # end
20
10
  #
21
- # class MyValidator < ActiveModel::Validator
22
- # def validate(record)
11
+ # class MyValidator < ActiveRecord::Validator
12
+ # def validate
23
13
  # if some_complex_logic
24
14
  # record.errors[:base] << "This record is invalid"
25
15
  # end
@@ -33,100 +23,42 @@ module ActiveModel
33
23
  #
34
24
  # You may also pass it multiple classes, like so:
35
25
  #
36
- # class Person
37
- # include ActiveModel::Validations
26
+ # class Person < ActiveRecord::Base
38
27
  # validates_with MyValidator, MyOtherValidator, :on => :create
39
28
  # end
40
29
  #
41
30
  # Configuration options:
42
- # * <tt>on</tt> - Specifies when this validation is active
43
- # (<tt>:create</tt> or <tt>:update</tt>
44
- # * <tt>if</tt> - Specifies a method, proc or string to call to determine
45
- # if the validation should occur (e.g. <tt>:if => :allow_validation</tt>,
46
- # or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>).
31
+ # * <tt>on</tt> - Specifies when this validation is active (<tt>:create</tt> or <tt>:update</tt>
32
+ # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
33
+ # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>).
47
34
  # The method, proc or string should return or evaluate to a true or false value.
48
- # * <tt>unless</tt> - Specifies a method, proc or string to call to
49
- # determine if the validation should not occur
50
- # (e.g. <tt>:unless => :skip_validation</tt>, or
51
- # <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>).
35
+ # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
36
+ # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>).
52
37
  # The method, proc or string should return or evaluate to a true or false value.
53
38
  #
54
- # If you pass any additional configuration options, they will be passed
55
- # to the class and available as <tt>options</tt>:
39
+ # If you pass any additional configuration options, they will be passed to the class and available as <tt>options</tt>:
56
40
  #
57
- # class Person
58
- # include ActiveModel::Validations
41
+ # class Person < ActiveRecord::Base
59
42
  # validates_with MyValidator, :my_custom_key => "my custom value"
60
43
  # end
61
44
  #
62
- # class MyValidator < ActiveModel::Validator
63
- # def validate(record)
45
+ # class MyValidator < ActiveRecord::Validator
46
+ # def validate
64
47
  # options[:my_custom_key] # => "my custom value"
65
48
  # end
66
49
  # end
67
50
  #
68
- def validates_with(*args, &block)
69
- options = args.extract_options!
70
- args.each do |klass|
71
- validator = klass.new(options, &block)
72
- validator.setup(self) if validator.respond_to?(:setup)
51
+ def validates_with(*args)
52
+ configuration = args.extract_options!
73
53
 
74
- if validator.respond_to?(:attributes) && !validator.attributes.empty?
75
- validator.attributes.each do |attribute|
76
- _validators[attribute.to_sym] << validator
77
- end
78
- else
79
- _validators[nil] << validator
54
+ validate configuration do |record|
55
+ args.each do |klass|
56
+ klass.new(record, configuration.except(:on, :if, :unless)).validate
80
57
  end
81
-
82
- validate(validator, options)
83
58
  end
84
59
  end
85
60
  end
86
-
87
- # Passes the record off to the class or classes specified and allows them
88
- # to add errors based on more complex conditions.
89
- #
90
- # class Person
91
- # include ActiveModel::Validations
92
- #
93
- # validates :instance_validations
94
- #
95
- # def instance_validations
96
- # validates_with MyValidator
97
- # end
98
- # end
99
- #
100
- # Please consult the class method documentation for more information on
101
- # creating your own validator.
102
- #
103
- # You may also pass it multiple classes, like so:
104
- #
105
- # class Person
106
- # include ActiveModel::Validations
107
- #
108
- # validates :instance_validations, :on => :create
109
- #
110
- # def instance_validations
111
- # validates_with MyValidator, MyOtherValidator
112
- # end
113
- # end
114
- #
115
- # Standard configuration options (:on, :if and :unless), which are
116
- # available on the class version of validates_with, should instead be
117
- # placed on the <tt>validates</tt> method as these are applied and tested
118
- # in the callback
119
- #
120
- # If you pass any additional configuration options, they will be passed
121
- # to the class and available as <tt>options</tt>, please refer to the
122
- # class version of this method for more information
123
- #
124
- def validates_with(*args, &block)
125
- options = args.extract_options!
126
- args.each do |klass|
127
- validator = klass.new(options, &block)
128
- validator.validate(self)
129
- end
130
- end
131
61
  end
132
- end
62
+ end
63
+
64
+
@@ -0,0 +1,35 @@
1
+ module ActiveModel
2
+ module ValidationsRepairHelper
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def repair_validations(*model_classes)
7
+ setup do
8
+ @_stored_callbacks = {}
9
+ model_classes.each do |k|
10
+ @_stored_callbacks[k] = k._validate_callbacks.dup
11
+ end
12
+ end
13
+ teardown do
14
+ model_classes.each do |k|
15
+ k._validate_callbacks = @_stored_callbacks[k]
16
+ k.__update_callbacks(:validate)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ def repair_validations(*model_classes, &block)
23
+ @__stored_callbacks = {}
24
+ model_classes.each do |k|
25
+ @__stored_callbacks[k] = k._validate_callbacks.dup
26
+ end
27
+ return block.call
28
+ ensure
29
+ model_classes.each do |k|
30
+ k._validate_callbacks = @__stored_callbacks[k]
31
+ k.__update_callbacks(:validate)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -2,9 +2,8 @@ module ActiveModel
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
4
  MINOR = 0
5
- TINY = 0
6
- BUILD = "beta4"
5
+ TINY = "pre"
7
6
 
8
- STRING = [MAJOR, MINOR, TINY, BUILD].join('.')
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
9
8
  end
10
9
  end
metadata CHANGED
@@ -1,13 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemodel
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: true
5
- segments:
6
- - 3
7
- - 0
8
- - 0
9
- - beta4
10
- version: 3.0.0.beta4
4
+ version: 3.0.pre
11
5
  platform: ruby
12
6
  authors:
13
7
  - David Heinemeier Hansson
@@ -15,53 +9,20 @@ autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
11
 
18
- date: 2010-06-08 00:00:00 -04:00
12
+ date: 2009-10-16 00:00:00 -05:00
19
13
  default_executable:
20
14
  dependencies:
21
15
  - !ruby/object:Gem::Dependency
22
16
  name: activesupport
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- requirements:
26
- - - "="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 3
30
- - 0
31
- - 0
32
- - beta4
33
- version: 3.0.0.beta4
34
- type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: builder
38
- prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
- requirements:
41
- - - ~>
42
- - !ruby/object:Gem::Version
43
- segments:
44
- - 2
45
- - 1
46
- - 2
47
- version: 2.1.2
48
17
  type: :runtime
49
- version_requirements: *id002
50
- - !ruby/object:Gem::Dependency
51
- name: i18n
52
- prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
54
20
  requirements:
55
- - - ~>
21
+ - - "="
56
22
  - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
- - 4
60
- - 1
61
- version: 0.4.1
62
- type: :runtime
63
- version_requirements: *id003
64
- description: A toolkit for building modeling frameworks like Active Record and Active Resource. Rich support for attributes, callbacks, validations, observers, serialization, internationalization, and testing.
23
+ version: 3.0.pre
24
+ version:
25
+ description: Extracts common modeling concerns from ActiveRecord to share between similar frameworks like ActiveResource.
65
26
  email: david@loudthinking.com
66
27
  executables: []
67
28
 
@@ -74,7 +35,6 @@ files:
74
35
  - MIT-LICENSE
75
36
  - README
76
37
  - lib/active_model/attribute_methods.rb
77
- - lib/active_model/callbacks.rb
78
38
  - lib/active_model/conversion.rb
79
39
  - lib/active_model/deprecated_error_methods.rb
80
40
  - lib/active_model/dirty.rb
@@ -83,12 +43,15 @@ files:
83
43
  - lib/active_model/locale/en.yml
84
44
  - lib/active_model/naming.rb
85
45
  - lib/active_model/observing.rb
86
- - lib/active_model/railtie.rb
87
46
  - lib/active_model/serialization.rb
88
47
  - lib/active_model/serializers/json.rb
89
48
  - lib/active_model/serializers/xml.rb
49
+ - lib/active_model/state_machine/event.rb
50
+ - lib/active_model/state_machine/machine.rb
51
+ - lib/active_model/state_machine/state.rb
52
+ - lib/active_model/state_machine/state_transition.rb
53
+ - lib/active_model/state_machine.rb
90
54
  - lib/active_model/test_case.rb
91
- - lib/active_model/translation.rb
92
55
  - lib/active_model/validations/acceptance.rb
93
56
  - lib/active_model/validations/confirmation.rb
94
57
  - lib/active_model/validations/exclusion.rb
@@ -97,10 +60,9 @@ files:
97
60
  - lib/active_model/validations/length.rb
98
61
  - lib/active_model/validations/numericality.rb
99
62
  - lib/active_model/validations/presence.rb
100
- - lib/active_model/validations/validates.rb
101
63
  - lib/active_model/validations/with.rb
102
64
  - lib/active_model/validations.rb
103
- - lib/active_model/validator.rb
65
+ - lib/active_model/validations_repair_helper.rb
104
66
  - lib/active_model/version.rb
105
67
  - lib/active_model.rb
106
68
  has_rdoc: true
@@ -116,26 +78,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
78
  requirements:
117
79
  - - ">="
118
80
  - !ruby/object:Gem::Version
119
- segments:
120
- - 1
121
- - 8
122
- - 7
123
- version: 1.8.7
81
+ version: "0"
82
+ version:
124
83
  required_rubygems_version: !ruby/object:Gem::Requirement
125
84
  requirements:
126
85
  - - ">"
127
86
  - !ruby/object:Gem::Version
128
- segments:
129
- - 1
130
- - 3
131
- - 1
132
87
  version: 1.3.1
88
+ version:
133
89
  requirements: []
134
90
 
135
91
  rubyforge_project: activemodel
136
- rubygems_version: 1.3.6
92
+ rubygems_version: 1.3.5
137
93
  signing_key:
138
94
  specification_version: 3
139
- summary: A toolkit for building modeling frameworks (part of Rails).
95
+ summary: A toolkit for building other modeling frameworks like ActiveRecord
140
96
  test_files: []
141
97
 
@@ -1,134 +0,0 @@
1
- require 'active_support/core_ext/array/wrap'
2
- require 'active_support/callbacks'
3
-
4
- module ActiveModel
5
- # == Active Model Callbacks
6
- #
7
- # Provides an interface for any class to have Active Record like callbacks.
8
- #
9
- # Like the Active Record methods, the call back chain is aborted as soon as
10
- # one of the methods in the chain returns false.
11
- #
12
- # First, extend ActiveModel::Callbacks from the class you are creating:
13
- #
14
- # class MyModel
15
- # extend ActiveModel::Callbacks
16
- # end
17
- #
18
- # Then define a list of methods that you want call backs attached to:
19
- #
20
- # define_model_callbacks :create, :update
21
- #
22
- # This will provide all three standard callbacks (before, around and after) around
23
- # both the :create and :update methods. To implement, you need to wrap the methods
24
- # you want call backs on in a block so that the call backs get a chance to fire:
25
- #
26
- # def create
27
- # _run_create_callbacks do
28
- # # Your create action methods here
29
- # end
30
- # end
31
- #
32
- # The _run_<method_name>_callbacks methods are dynamically created when you extend
33
- # the <tt>ActiveModel::Callbacks</tt> module.
34
- #
35
- # Then in your class, you can use the +before_create+, +after_create+ and +around_create+
36
- # methods, just as you would in an Active Record module.
37
- #
38
- # before_create :action_before_create
39
- #
40
- # def action_before_create
41
- # # Your code here
42
- # end
43
- #
44
- # You can choose not to have all three callbacks by passing an hash to the
45
- # define_model_callbacks method.
46
- #
47
- # define_model_callbacks :create, :only => :after, :before
48
- #
49
- # Would only create the after_create and before_create callback methods in your
50
- # class.
51
- module Callbacks
52
- def self.extended(base)
53
- base.class_eval do
54
- include ActiveSupport::Callbacks
55
- end
56
- end
57
-
58
- # define_model_callbacks accepts all options define_callbacks does, in case you
59
- # want to overwrite a default. Besides that, it also accepts an :only option,
60
- # where you can choose if you want all types (before, around or after) or just some.
61
- #
62
- # define_model_callbacks :initializer, :only => :after
63
- #
64
- # Note, the <tt>:only => <type></tt> hash will apply to all callbacks defined on
65
- # that method call. To get around this you can call the define_model_callbacks
66
- # method as many times as you need.
67
- #
68
- # define_model_callbacks :create, :only => :after
69
- # define_model_callbacks :update, :only => :before
70
- # define_model_callbacks :destroy, :only => :around
71
- #
72
- # Would create +after_create+, +before_update+ and +around_destroy+ methods only.
73
- #
74
- # You can pass in a class to before_<type>, after_<type> and around_<type>, in which
75
- # case the call back will call that class's <action>_<type> method passing the object
76
- # that the callback is being called on.
77
- #
78
- # class MyModel
79
- # extend ActiveModel::Callbacks
80
- # define_model_callbacks :create
81
- #
82
- # before_create AnotherClass
83
- # end
84
- #
85
- # class AnotherClass
86
- # def self.before_create( obj )
87
- # # obj is the MyModel instance that the callback is being called on
88
- # end
89
- # end
90
- #
91
- def define_model_callbacks(*callbacks)
92
- options = callbacks.extract_options!
93
- options = { :terminator => "result == false", :scope => [:kind, :name] }.merge(options)
94
-
95
- types = Array.wrap(options.delete(:only))
96
- types = [:before, :around, :after] if types.empty?
97
-
98
- callbacks.each do |callback|
99
- define_callbacks(callback, options)
100
-
101
- types.each do |type|
102
- send(:"_define_#{type}_model_callback", self, callback)
103
- end
104
- end
105
- end
106
-
107
- def _define_before_model_callback(klass, callback) #:nodoc:
108
- klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
109
- def self.before_#{callback}(*args, &block)
110
- set_callback(:#{callback}, :before, *args, &block)
111
- end
112
- CALLBACK
113
- end
114
-
115
- def _define_around_model_callback(klass, callback) #:nodoc:
116
- klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
117
- def self.around_#{callback}(*args, &block)
118
- set_callback(:#{callback}, :around, *args, &block)
119
- end
120
- CALLBACK
121
- end
122
-
123
- def _define_after_model_callback(klass, callback) #:nodoc:
124
- klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
125
- def self.after_#{callback}(*args, &block)
126
- options = args.extract_options!
127
- options[:prepend] = true
128
- options[:if] = Array.wrap(options[:if]) << "!halted && value != false"
129
- set_callback(:#{callback}, :after, *(args << options), &block)
130
- end
131
- CALLBACK
132
- end
133
- end
134
- end