default_value_for 1.0.1 → 1.0.3
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/README.rdoc +19 -3
- data/default_value_for.gemspec +2 -3
- data/init.rb +1 -2
- data/lib/default_value_for.rb +107 -99
- data/lib/default_value_for/railtie.rb +6 -9
- data/test.rb +4 -1
- metadata +6 -7
- data/lib/rails.rb +0 -23
data/README.rdoc
CHANGED
@@ -20,10 +20,22 @@ section. Please read on.
|
|
20
20
|
|
21
21
|
== Installation
|
22
22
|
|
23
|
+
=== Rails 2
|
24
|
+
|
23
25
|
Install with:
|
24
26
|
|
25
27
|
./script/plugin install git://github.com/FooBarWidget/default_value_for.git
|
26
28
|
|
29
|
+
=== Rails 3
|
30
|
+
|
31
|
+
You can either install it as a plugin:
|
32
|
+
|
33
|
+
rails plugin install git://github.com/FooBarWidget/default_value_for.git
|
34
|
+
|
35
|
+
or install it by adding it to your Gemfile:
|
36
|
+
|
37
|
+
gem "default_value_for"
|
38
|
+
|
27
39
|
|
28
40
|
== The default_value_for method
|
29
41
|
|
@@ -84,9 +96,13 @@ The difference is purely aesthetic. If you have lots of default values which ar
|
|
84
96
|
|
85
97
|
As a side note, due to specifics of Ruby's parser, you cannot say,
|
86
98
|
|
87
|
-
|
99
|
+
default_value_for :uuid { UuidGenerator.new.generate_uuid }
|
100
|
+
|
101
|
+
because it will not parse. One needs to write
|
102
|
+
|
103
|
+
default_value_for(:uuid) { UuidGenerator.new.generate_uuid }
|
88
104
|
|
89
|
-
|
105
|
+
instead. This is in part the inspiration for the +default_values+ syntax.
|
90
106
|
|
91
107
|
== Rules
|
92
108
|
|
@@ -290,7 +306,7 @@ And if schema defaults don't provide the flexibility that you need, then
|
|
290
306
|
you could specify a per-environment default:
|
291
307
|
|
292
308
|
class User < ActiveRecord::Base
|
293
|
-
if
|
309
|
+
if Rails.env == "development"
|
294
310
|
default_value_for :is_admin, true
|
295
311
|
end
|
296
312
|
end
|
data/default_value_for.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{default_value_for}
|
3
|
-
s.version = "1.0.
|
3
|
+
s.version = "1.0.3"
|
4
4
|
s.summary = %q{Provides a way to specify default values for ActiveRecord models}
|
5
5
|
s.description = %q{The default_value_for plugin allows one to define default values for ActiveRecord models in a declarative manner}
|
6
6
|
s.email = %q{info@phusion.nl}
|
@@ -10,7 +10,6 @@ Gem::Specification.new do |s|
|
|
10
10
|
'LICENSE.TXT', 'Rakefile', 'README.rdoc', 'test.rb',
|
11
11
|
'init.rb',
|
12
12
|
'lib/default_value_for.rb',
|
13
|
-
'lib/default_value_for/railtie.rb'
|
14
|
-
'lib/rails.rb'
|
13
|
+
'lib/default_value_for/railtie.rb'
|
15
14
|
]
|
16
15
|
end
|
data/init.rb
CHANGED
data/lib/default_value_for.rb
CHANGED
@@ -1,99 +1,107 @@
|
|
1
|
-
# Copyright (c) 2008, 2009, 2010 Phusion
|
2
|
-
#
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
8
|
-
# furnished to do so, subject to the following conditions:
|
9
|
-
#
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
11
|
-
# all copies or substantial portions of the Software.
|
12
|
-
#
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
# THE SOFTWARE.
|
20
|
-
|
21
|
-
require
|
22
|
-
|
23
|
-
module
|
24
|
-
class NormalValueContainer
|
25
|
-
def initialize(value)
|
26
|
-
@value = value
|
27
|
-
end
|
28
|
-
|
29
|
-
def evaluate(instance)
|
30
|
-
if @value.duplicable?
|
31
|
-
return @value.dup
|
32
|
-
else
|
33
|
-
return @value
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class BlockValueContainer
|
39
|
-
def initialize(block)
|
40
|
-
@block = block
|
41
|
-
end
|
42
|
-
|
43
|
-
def evaluate(instance)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
1
|
+
# Copyright (c) 2008, 2009, 2010, 2011 Phusion
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'default_value_for/railtie' if defined?(Rails::Railtie)
|
22
|
+
|
23
|
+
module DefaultValueFor
|
24
|
+
class NormalValueContainer
|
25
|
+
def initialize(value)
|
26
|
+
@value = value
|
27
|
+
end
|
28
|
+
|
29
|
+
def evaluate(instance)
|
30
|
+
if @value.duplicable?
|
31
|
+
return @value.dup
|
32
|
+
else
|
33
|
+
return @value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class BlockValueContainer
|
39
|
+
def initialize(block)
|
40
|
+
@block = block
|
41
|
+
end
|
42
|
+
|
43
|
+
def evaluate(instance)
|
44
|
+
if @block.arity == 0
|
45
|
+
return @block.call
|
46
|
+
else
|
47
|
+
return @block.call(instance)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module ClassMethods
|
53
|
+
def default_value_for(attribute, value = nil, &block)
|
54
|
+
if !method_defined?(:initialize_with_defaults)
|
55
|
+
include(InstanceMethods)
|
56
|
+
alias_method_chain :initialize, :defaults
|
57
|
+
if respond_to?(:class_attribute)
|
58
|
+
class_attribute :_default_attribute_values
|
59
|
+
else
|
60
|
+
class_inheritable_accessor :_default_attribute_values
|
61
|
+
end
|
62
|
+
self._default_attribute_values = ActiveSupport::OrderedHash.new
|
63
|
+
end
|
64
|
+
if block_given?
|
65
|
+
container = BlockValueContainer.new(block)
|
66
|
+
else
|
67
|
+
container = NormalValueContainer.new(value)
|
68
|
+
end
|
69
|
+
_default_attribute_values[attribute.to_s] = container
|
70
|
+
end
|
71
|
+
|
72
|
+
def default_values(values)
|
73
|
+
values.each_pair do |key, value|
|
74
|
+
if value.kind_of? Proc
|
75
|
+
default_value_for(key, &value)
|
76
|
+
else
|
77
|
+
default_value_for(key, value)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
module InstanceMethods
|
84
|
+
def initialize_with_defaults(attrs = nil, *args)
|
85
|
+
initialize_without_defaults(attrs, *args) do
|
86
|
+
if attrs
|
87
|
+
stringified_attrs = attrs.stringify_keys
|
88
|
+
safe_attrs = if respond_to? :sanitize_for_mass_assignment
|
89
|
+
sanitize_for_mass_assignment(stringified_attrs)
|
90
|
+
else
|
91
|
+
remove_attributes_protected_from_mass_assignment(stringified_attrs)
|
92
|
+
end
|
93
|
+
safe_attribute_names = safe_attrs.keys.map do |x|
|
94
|
+
x.to_s
|
95
|
+
end
|
96
|
+
end
|
97
|
+
self.class._default_attribute_values.each do |attribute, container|
|
98
|
+
if safe_attribute_names.nil? || !safe_attribute_names.any? { |attr_name| attr_name =~ /^#{attribute}($|\()/ }
|
99
|
+
__send__("#{attribute}=", container.evaluate(self))
|
100
|
+
changed_attributes.delete(attribute)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
yield(self) if block_given?
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2008, 2009, 2010 Phusion
|
1
|
+
# Copyright (c) 2008, 2009, 2010, 2011 Phusion
|
2
2
|
#
|
3
3
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
4
|
# of this software and associated documentation files (the "Software"), to deal
|
@@ -19,14 +19,11 @@
|
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
21
|
# Rails 3 initialization
|
22
|
-
module
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
ActiveSupport.on_load :active_record do
|
28
|
-
ActiveRecord::Base.extend(DefaultValueForPlugin::ClassMethods)
|
29
|
-
end
|
22
|
+
module DefaultValueFor
|
23
|
+
class Railtie < Rails::Railtie
|
24
|
+
initializer 'default_value_for.insert_into_active_record' do
|
25
|
+
ActiveSupport.on_load :active_record do
|
26
|
+
ActiveRecord::Base.extend(DefaultValueFor::ClassMethods)
|
30
27
|
end
|
31
28
|
end
|
32
29
|
end
|
data/test.rb
CHANGED
@@ -19,10 +19,12 @@
|
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
21
|
require 'rubygems'
|
22
|
+
gem 'activerecord', '~> 3.1.0.rc1'
|
22
23
|
require 'active_record'
|
23
24
|
require 'test/unit'
|
24
25
|
require 'active_support/core_ext/logger'
|
25
26
|
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
27
|
+
require File.dirname(__FILE__) + '/lib/default_value_for'
|
26
28
|
require File.dirname(__FILE__) + '/init'
|
27
29
|
Dir.chdir(File.dirname(__FILE__))
|
28
30
|
|
@@ -222,7 +224,8 @@ class DefaultValuePluginTest < Test::Unit::TestCase
|
|
222
224
|
def test_default_values
|
223
225
|
define_model_class do
|
224
226
|
default_values :type => "normal",
|
225
|
-
|
227
|
+
:number => lambda { 10 + 5 },
|
228
|
+
:timestamp => lambda {|_| Time.now }
|
226
229
|
end
|
227
230
|
|
228
231
|
object = TestClass.new
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: default_value_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 17
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 3
|
10
|
+
version: 1.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Hongli Lai
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-07-18 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -36,7 +36,6 @@ files:
|
|
36
36
|
- init.rb
|
37
37
|
- lib/default_value_for.rb
|
38
38
|
- lib/default_value_for/railtie.rb
|
39
|
-
- lib/rails.rb
|
40
39
|
has_rdoc: true
|
41
40
|
homepage: http://github.com/FooBarWidget/default_value_for
|
42
41
|
licenses: []
|
@@ -67,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
66
|
requirements: []
|
68
67
|
|
69
68
|
rubyforge_project:
|
70
|
-
rubygems_version: 1.
|
69
|
+
rubygems_version: 1.5.2
|
71
70
|
signing_key:
|
72
71
|
specification_version: 3
|
73
72
|
summary: Provides a way to specify default values for ActiveRecord models
|
data/lib/rails.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# Copyright (c) 2008, 2009, 2010 Phusion
|
2
|
-
#
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
8
|
-
# furnished to do so, subject to the following conditions:
|
9
|
-
#
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
11
|
-
# all copies or substantial portions of the Software.
|
12
|
-
#
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
# THE SOFTWARE.
|
20
|
-
|
21
|
-
# Rails 2 initialization
|
22
|
-
require "default_value_for"
|
23
|
-
ActiveRecord::Base.extend(DefaultValueForPlugin::ClassMethods)
|