activemodel 3.0.pre → 3.0.0.rc
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +44 -1
- data/MIT-LICENSE +1 -1
- data/README.rdoc +184 -0
- data/lib/active_model.rb +29 -19
- data/lib/active_model/attribute_methods.rb +167 -46
- data/lib/active_model/callbacks.rb +134 -0
- data/lib/active_model/conversion.rb +41 -1
- data/lib/active_model/deprecated_error_methods.rb +1 -1
- data/lib/active_model/dirty.rb +56 -12
- data/lib/active_model/errors.rb +205 -46
- data/lib/active_model/lint.rb +53 -17
- data/lib/active_model/locale/en.yml +26 -23
- data/lib/active_model/mass_assignment_security.rb +160 -0
- data/lib/active_model/mass_assignment_security/permission_set.rb +40 -0
- data/lib/active_model/mass_assignment_security/sanitizer.rb +23 -0
- data/lib/active_model/naming.rb +70 -5
- data/lib/active_model/observing.rb +40 -16
- data/lib/active_model/railtie.rb +2 -0
- data/lib/active_model/serialization.rb +59 -0
- data/lib/active_model/serializers/json.rb +17 -11
- data/lib/active_model/serializers/xml.rb +66 -123
- data/lib/active_model/test_case.rb +0 -2
- data/lib/active_model/translation.rb +64 -0
- data/lib/active_model/validations.rb +150 -68
- data/lib/active_model/validations/acceptance.rb +53 -33
- data/lib/active_model/validations/callbacks.rb +57 -0
- data/lib/active_model/validations/confirmation.rb +41 -23
- data/lib/active_model/validations/exclusion.rb +18 -13
- data/lib/active_model/validations/format.rb +28 -24
- data/lib/active_model/validations/inclusion.rb +18 -13
- data/lib/active_model/validations/length.rb +67 -65
- data/lib/active_model/validations/numericality.rb +83 -58
- data/lib/active_model/validations/presence.rb +10 -8
- data/lib/active_model/validations/validates.rb +110 -0
- data/lib/active_model/validations/with.rb +90 -23
- data/lib/active_model/validator.rb +186 -0
- data/lib/active_model/version.rb +3 -2
- metadata +79 -20
- data/README +0 -21
- data/lib/active_model/state_machine.rb +0 -70
- data/lib/active_model/state_machine/event.rb +0 -62
- data/lib/active_model/state_machine/machine.rb +0 -75
- data/lib/active_model/state_machine/state.rb +0 -47
- data/lib/active_model/state_machine/state_transition.rb +0 -40
- data/lib/active_model/validations_repair_helper.rb +0 -35
data/lib/active_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activemodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 7712042
|
5
|
+
prerelease: true
|
6
|
+
segments:
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
- rc
|
11
|
+
version: 3.0.0.rc
|
5
12
|
platform: ruby
|
6
13
|
authors:
|
7
14
|
- David Heinemeier Hansson
|
@@ -9,20 +16,59 @@ autorequire:
|
|
9
16
|
bindir: bin
|
10
17
|
cert_chain: []
|
11
18
|
|
12
|
-
date:
|
19
|
+
date: 2010-07-26 00:00:00 -05:00
|
13
20
|
default_executable:
|
14
21
|
dependencies:
|
15
22
|
- !ruby/object:Gem::Dependency
|
16
23
|
name: activesupport
|
17
|
-
|
18
|
-
|
19
|
-
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
20
27
|
requirements:
|
21
28
|
- - "="
|
22
29
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
25
|
-
|
30
|
+
hash: 7712042
|
31
|
+
segments:
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
- 0
|
35
|
+
- rc
|
36
|
+
version: 3.0.0.rc
|
37
|
+
type: :runtime
|
38
|
+
version_requirements: *id001
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: builder
|
41
|
+
prerelease: false
|
42
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 15
|
48
|
+
segments:
|
49
|
+
- 2
|
50
|
+
- 1
|
51
|
+
- 2
|
52
|
+
version: 2.1.2
|
53
|
+
type: :runtime
|
54
|
+
version_requirements: *id002
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: i18n
|
57
|
+
prerelease: false
|
58
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ~>
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 13
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
- 4
|
67
|
+
- 1
|
68
|
+
version: 0.4.1
|
69
|
+
type: :runtime
|
70
|
+
version_requirements: *id003
|
71
|
+
description: A toolkit for building modeling frameworks like Active Record and Active Resource. Rich support for attributes, callbacks, validations, observers, serialization, internationalization, and testing.
|
26
72
|
email: david@loudthinking.com
|
27
73
|
executables: []
|
28
74
|
|
@@ -33,26 +79,28 @@ extra_rdoc_files: []
|
|
33
79
|
files:
|
34
80
|
- CHANGELOG
|
35
81
|
- MIT-LICENSE
|
36
|
-
- README
|
82
|
+
- README.rdoc
|
37
83
|
- lib/active_model/attribute_methods.rb
|
84
|
+
- lib/active_model/callbacks.rb
|
38
85
|
- lib/active_model/conversion.rb
|
39
86
|
- lib/active_model/deprecated_error_methods.rb
|
40
87
|
- lib/active_model/dirty.rb
|
41
88
|
- lib/active_model/errors.rb
|
42
89
|
- lib/active_model/lint.rb
|
43
90
|
- lib/active_model/locale/en.yml
|
91
|
+
- lib/active_model/mass_assignment_security/permission_set.rb
|
92
|
+
- lib/active_model/mass_assignment_security/sanitizer.rb
|
93
|
+
- lib/active_model/mass_assignment_security.rb
|
44
94
|
- lib/active_model/naming.rb
|
45
95
|
- lib/active_model/observing.rb
|
96
|
+
- lib/active_model/railtie.rb
|
46
97
|
- lib/active_model/serialization.rb
|
47
98
|
- lib/active_model/serializers/json.rb
|
48
99
|
- 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
|
54
100
|
- lib/active_model/test_case.rb
|
101
|
+
- lib/active_model/translation.rb
|
55
102
|
- lib/active_model/validations/acceptance.rb
|
103
|
+
- lib/active_model/validations/callbacks.rb
|
56
104
|
- lib/active_model/validations/confirmation.rb
|
57
105
|
- lib/active_model/validations/exclusion.rb
|
58
106
|
- lib/active_model/validations/format.rb
|
@@ -60,9 +108,10 @@ files:
|
|
60
108
|
- lib/active_model/validations/length.rb
|
61
109
|
- lib/active_model/validations/numericality.rb
|
62
110
|
- lib/active_model/validations/presence.rb
|
111
|
+
- lib/active_model/validations/validates.rb
|
63
112
|
- lib/active_model/validations/with.rb
|
64
113
|
- lib/active_model/validations.rb
|
65
|
-
- lib/active_model/
|
114
|
+
- lib/active_model/validator.rb
|
66
115
|
- lib/active_model/version.rb
|
67
116
|
- lib/active_model.rb
|
68
117
|
has_rdoc: true
|
@@ -75,23 +124,33 @@ rdoc_options: []
|
|
75
124
|
require_paths:
|
76
125
|
- lib
|
77
126
|
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
78
128
|
requirements:
|
79
129
|
- - ">="
|
80
130
|
- !ruby/object:Gem::Version
|
81
|
-
|
82
|
-
|
131
|
+
hash: 57
|
132
|
+
segments:
|
133
|
+
- 1
|
134
|
+
- 8
|
135
|
+
- 7
|
136
|
+
version: 1.8.7
|
83
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
84
139
|
requirements:
|
85
140
|
- - ">"
|
86
141
|
- !ruby/object:Gem::Version
|
142
|
+
hash: 25
|
143
|
+
segments:
|
144
|
+
- 1
|
145
|
+
- 3
|
146
|
+
- 1
|
87
147
|
version: 1.3.1
|
88
|
-
version:
|
89
148
|
requirements: []
|
90
149
|
|
91
150
|
rubyforge_project: activemodel
|
92
|
-
rubygems_version: 1.3.
|
151
|
+
rubygems_version: 1.3.7
|
93
152
|
signing_key:
|
94
153
|
specification_version: 3
|
95
|
-
summary: A toolkit for building
|
154
|
+
summary: A toolkit for building modeling frameworks (part of Rails).
|
96
155
|
test_files: []
|
97
156
|
|
data/README
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
Active Model
|
2
|
-
==============
|
3
|
-
|
4
|
-
Totally experimental library that aims to extract common model mixins from
|
5
|
-
ActiveRecord for use in ActiveResource (and other similar libraries).
|
6
|
-
This is in a very rough state (no autotest or spec rake tasks set up yet),
|
7
|
-
so please excuse the mess.
|
8
|
-
|
9
|
-
Here's what I plan to extract:
|
10
|
-
* ActiveModel::Observing
|
11
|
-
* ActiveModel::Callbacks
|
12
|
-
* ActiveModel::Validations
|
13
|
-
|
14
|
-
# for ActiveResource params and ActiveRecord options
|
15
|
-
* ActiveModel::Scoping
|
16
|
-
|
17
|
-
# to_json, to_xml, etc
|
18
|
-
* ActiveModel::Serialization
|
19
|
-
|
20
|
-
I'm trying to keep ActiveRecord compatibility where possible, but I'm
|
21
|
-
annotating the spots where I'm diverging a bit.
|
@@ -1,70 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
module StateMachine
|
3
|
-
autoload :Event, 'active_model/state_machine/event'
|
4
|
-
autoload :Machine, 'active_model/state_machine/machine'
|
5
|
-
autoload :State, 'active_model/state_machine/state'
|
6
|
-
autoload :StateTransition, 'active_model/state_machine/state_transition'
|
7
|
-
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
|
10
|
-
class InvalidTransition < Exception
|
11
|
-
end
|
12
|
-
|
13
|
-
module ClassMethods
|
14
|
-
def inherited(klass)
|
15
|
-
super
|
16
|
-
klass.state_machines = state_machines
|
17
|
-
end
|
18
|
-
|
19
|
-
def state_machines
|
20
|
-
@state_machines ||= {}
|
21
|
-
end
|
22
|
-
|
23
|
-
def state_machines=(value)
|
24
|
-
@state_machines = value ? value.dup : nil
|
25
|
-
end
|
26
|
-
|
27
|
-
def state_machine(name = nil, options = {}, &block)
|
28
|
-
if name.is_a?(Hash)
|
29
|
-
options = name
|
30
|
-
name = nil
|
31
|
-
end
|
32
|
-
name ||= :default
|
33
|
-
state_machines[name] ||= Machine.new(self, name)
|
34
|
-
block ? state_machines[name].update(options, &block) : state_machines[name]
|
35
|
-
end
|
36
|
-
|
37
|
-
def define_state_query_method(state_name)
|
38
|
-
name = "#{state_name}?"
|
39
|
-
undef_method(name) if method_defined?(name)
|
40
|
-
class_eval "def #{name}; current_state.to_s == %(#{state_name}) end"
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def current_state(name = nil, new_state = nil, persist = false)
|
45
|
-
sm = self.class.state_machine(name)
|
46
|
-
ivar = sm.current_state_variable
|
47
|
-
if name && new_state
|
48
|
-
if persist && respond_to?(:write_state)
|
49
|
-
write_state(sm, new_state)
|
50
|
-
end
|
51
|
-
|
52
|
-
if respond_to?(:write_state_without_persistence)
|
53
|
-
write_state_without_persistence(sm, new_state)
|
54
|
-
end
|
55
|
-
|
56
|
-
instance_variable_set(ivar, new_state)
|
57
|
-
else
|
58
|
-
instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar)
|
59
|
-
value = instance_variable_get(ivar)
|
60
|
-
return value if value
|
61
|
-
|
62
|
-
if respond_to?(:read_state)
|
63
|
-
value = instance_variable_set(ivar, read_state(sm))
|
64
|
-
end
|
65
|
-
|
66
|
-
value || sm.initial_state
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
module StateMachine
|
3
|
-
class Event
|
4
|
-
attr_reader :name, :success
|
5
|
-
|
6
|
-
def initialize(machine, name, options = {}, &block)
|
7
|
-
@machine, @name, @transitions = machine, name, []
|
8
|
-
if machine
|
9
|
-
machine.klass.send(:define_method, "#{name}!") do |*args|
|
10
|
-
machine.fire_event(name, self, true, *args)
|
11
|
-
end
|
12
|
-
|
13
|
-
machine.klass.send(:define_method, name.to_s) do |*args|
|
14
|
-
machine.fire_event(name, self, false, *args)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
update(options, &block)
|
18
|
-
end
|
19
|
-
|
20
|
-
def fire(obj, to_state = nil, *args)
|
21
|
-
transitions = @transitions.select { |t| t.from == obj.current_state(@machine ? @machine.name : nil) }
|
22
|
-
raise InvalidTransition if transitions.size == 0
|
23
|
-
|
24
|
-
next_state = nil
|
25
|
-
transitions.each do |transition|
|
26
|
-
next if to_state && !Array(transition.to).include?(to_state)
|
27
|
-
if transition.perform(obj)
|
28
|
-
next_state = to_state || Array(transition.to).first
|
29
|
-
transition.execute(obj, *args)
|
30
|
-
break
|
31
|
-
end
|
32
|
-
end
|
33
|
-
next_state
|
34
|
-
end
|
35
|
-
|
36
|
-
def transitions_from_state?(state)
|
37
|
-
@transitions.any? { |t| t.from? state }
|
38
|
-
end
|
39
|
-
|
40
|
-
def ==(event)
|
41
|
-
if event.is_a? Symbol
|
42
|
-
name == event
|
43
|
-
else
|
44
|
-
name == event.name
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def update(options = {}, &block)
|
49
|
-
if options.key?(:success) then @success = options[:success] end
|
50
|
-
if block then instance_eval(&block) end
|
51
|
-
self
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
def transitions(trans_opts)
|
56
|
-
Array(trans_opts[:from]).each do |s|
|
57
|
-
@transitions << StateTransition.new(trans_opts.merge({:from => s.to_sym}))
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
module StateMachine
|
3
|
-
class Machine
|
4
|
-
attr_writer :initial_state
|
5
|
-
attr_accessor :states, :events, :state_index
|
6
|
-
attr_reader :klass, :name
|
7
|
-
|
8
|
-
def initialize(klass, name, options = {}, &block)
|
9
|
-
@klass, @name, @states, @state_index, @events = klass, name, [], {}, {}
|
10
|
-
update(options, &block)
|
11
|
-
end
|
12
|
-
|
13
|
-
def initial_state
|
14
|
-
@initial_state ||= (states.first ? states.first.name : nil)
|
15
|
-
end
|
16
|
-
|
17
|
-
def update(options = {}, &block)
|
18
|
-
if options.key?(:initial) then @initial_state = options[:initial] end
|
19
|
-
if block then instance_eval(&block) end
|
20
|
-
self
|
21
|
-
end
|
22
|
-
|
23
|
-
def fire_event(event, record, persist, *args)
|
24
|
-
state_index[record.current_state(@name)].call_action(:exit, record)
|
25
|
-
if new_state = @events[event].fire(record, *args)
|
26
|
-
state_index[new_state].call_action(:enter, record)
|
27
|
-
|
28
|
-
if record.respond_to?(event_fired_callback)
|
29
|
-
record.send(event_fired_callback, record.current_state, new_state)
|
30
|
-
end
|
31
|
-
|
32
|
-
record.current_state(@name, new_state, persist)
|
33
|
-
record.send(@events[event].success) if @events[event].success
|
34
|
-
true
|
35
|
-
else
|
36
|
-
if record.respond_to?(event_failed_callback)
|
37
|
-
record.send(event_failed_callback, event)
|
38
|
-
end
|
39
|
-
|
40
|
-
false
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def states_for_select
|
45
|
-
states.map { |st| [st.display_name, st.name.to_s] }
|
46
|
-
end
|
47
|
-
|
48
|
-
def events_for(state)
|
49
|
-
events = @events.values.select { |event| event.transitions_from_state?(state) }
|
50
|
-
events.map! { |event| event.name }
|
51
|
-
end
|
52
|
-
|
53
|
-
def current_state_variable
|
54
|
-
"@#{@name}_current_state"
|
55
|
-
end
|
56
|
-
|
57
|
-
private
|
58
|
-
def state(name, options = {})
|
59
|
-
@states << (state_index[name] ||= State.new(name, :machine => self)).update(options)
|
60
|
-
end
|
61
|
-
|
62
|
-
def event(name, options = {}, &block)
|
63
|
-
(@events[name] ||= Event.new(self, name)).update(options, &block)
|
64
|
-
end
|
65
|
-
|
66
|
-
def event_fired_callback
|
67
|
-
@event_fired_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_fired'
|
68
|
-
end
|
69
|
-
|
70
|
-
def event_failed_callback
|
71
|
-
@event_failed_callback ||= (@name == :default ? '' : "#{@name}_") + 'event_failed'
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
module StateMachine
|
3
|
-
class State
|
4
|
-
attr_reader :name, :options
|
5
|
-
|
6
|
-
def initialize(name, options = {})
|
7
|
-
@name = name
|
8
|
-
if machine = options.delete(:machine)
|
9
|
-
machine.klass.define_state_query_method(name)
|
10
|
-
end
|
11
|
-
update(options)
|
12
|
-
end
|
13
|
-
|
14
|
-
def ==(state)
|
15
|
-
if state.is_a? Symbol
|
16
|
-
name == state
|
17
|
-
else
|
18
|
-
name == state.name
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def call_action(action, record)
|
23
|
-
action = @options[action]
|
24
|
-
case action
|
25
|
-
when Symbol, String
|
26
|
-
record.send(action)
|
27
|
-
when Proc
|
28
|
-
action.call(record)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def display_name
|
33
|
-
@display_name ||= name.to_s.gsub(/_/, ' ').capitalize
|
34
|
-
end
|
35
|
-
|
36
|
-
def for_select
|
37
|
-
[display_name, name.to_s]
|
38
|
-
end
|
39
|
-
|
40
|
-
def update(options = {})
|
41
|
-
if options.key?(:display) then @display_name = options.delete(:display) end
|
42
|
-
@options = options
|
43
|
-
self
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|