active_record_defaults 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +19 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +38 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/active_record_defaults.gemspec +74 -0
- data/lib/active_record/defaults.rb +132 -0
- data/lib/active_record_defaults.rb +10 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/abstract_unit.rb +29 -0
- data/test/database.yml +6 -0
- data/test/fixtures/address.rb +1 -0
- data/test/fixtures/group.rb +2 -0
- data/test/fixtures/people.yml +7 -0
- data/test/fixtures/person.rb +20 -0
- data/test/fixtures/person_with_default_school.rb +5 -0
- data/test/fixtures/person_with_default_school_id.rb +3 -0
- data/test/fixtures/school.rb +3 -0
- data/test/fixtures/schools.yml +3 -0
- data/test/schema.rb +18 -0
- data/test/test_active_record_defaults.rb +68 -0
- data/test/test_helper.rb +3 -0
- metadata +97 -0
data/History.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
[13 Nov 09]
|
2
|
+
|
3
|
+
* Ported to Gem
|
4
|
+
* Moved to Jeweler
|
5
|
+
* Pushed to Gemcutter
|
6
|
+
|
7
|
+
[20 Sep 07]
|
8
|
+
|
9
|
+
* Remove ability to set defaults with defaults instance method. Too easy to override values from parameters.
|
10
|
+
|
11
|
+
[25 Jul 07]
|
12
|
+
|
13
|
+
* Using a symbol as a default value will cause the return value of the method with that name to be the default.
|
14
|
+
|
15
|
+
* Documentation updates.
|
16
|
+
|
17
|
+
[24 Nov 06]
|
18
|
+
|
19
|
+
* Make plugin work with composed_of [Problem reported by Dan Kubb]
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006 Jonathan Viney
|
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.rdoc
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
= Changes for this Fork
|
2
|
+
|
3
|
+
* Ported to a Gem.
|
4
|
+
|
5
|
+
|
6
|
+
= Active Record Defaults
|
7
|
+
|
8
|
+
If you find this plugin useful, please consider a donation to show your support!
|
9
|
+
|
10
|
+
http://www.paypal.com/cgi-bin/webscr?cmd=_send-money
|
11
|
+
|
12
|
+
Email address: jonathan.viney@gmail.com
|
13
|
+
|
14
|
+
== Instructions
|
15
|
+
|
16
|
+
Allow you to easily specify default values for attributes on new model objects. Eg:
|
17
|
+
|
18
|
+
class Person < ActiveRecord::Base
|
19
|
+
defaults :country => 'New Zealand', :type => 'Unknown', :address => proc { Address.new }
|
20
|
+
|
21
|
+
default :last_name do |person|
|
22
|
+
person.first_name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
The default value is only used if the attribute is not present in the attributes hash.
|
27
|
+
|
28
|
+
See active_record_defaults.rb for full documentation.
|
29
|
+
|
30
|
+
== Installation
|
31
|
+
|
32
|
+
script/plugin install http://svn.viney.net.nz/things/rails/plugins/active_record_defaults
|
33
|
+
|
34
|
+
== Help
|
35
|
+
|
36
|
+
Feel free to email with any problems, suggestions, bugs etc...
|
37
|
+
|
38
|
+
jonathan dot viney @ gmail . com
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "active_record_defaults"
|
8
|
+
gem.summary = %Q{Easily specify default values for attributes on new model objects.}
|
9
|
+
gem.description = %Q{Allows you to easily specify default values for attributes on new model objects.}
|
10
|
+
gem.email = "jonathan.viney@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/midas/active_record_defaults"
|
12
|
+
gem.authors = ["Jonathan Viney (aussiegeek)", "C. Jason Harrelson (midas)"]
|
13
|
+
gem.add_dependency "activerecord", ">= 2"
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
if File.exist?('VERSION')
|
47
|
+
version = File.read('VERSION')
|
48
|
+
else
|
49
|
+
version = ""
|
50
|
+
end
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "tester #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.1
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{active_record_defaults}
|
8
|
+
s.version = "1.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jonathan Viney (aussiegeek)", "C. Jason Harrelson (midas)"]
|
12
|
+
s.date = %q{2009-11-13}
|
13
|
+
s.description = %q{Allows you to easily specify default values for attributes on new model objects.}
|
14
|
+
s.email = %q{jonathan.viney@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"History.txt",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README.rdoc",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"active_record_defaults.gemspec",
|
25
|
+
"lib/active_record/defaults.rb",
|
26
|
+
"lib/active_record_defaults.rb",
|
27
|
+
"script/console",
|
28
|
+
"script/destroy",
|
29
|
+
"script/generate",
|
30
|
+
"test/abstract_unit.rb",
|
31
|
+
"test/database.yml",
|
32
|
+
"test/fixtures/address.rb",
|
33
|
+
"test/fixtures/group.rb",
|
34
|
+
"test/fixtures/people.yml",
|
35
|
+
"test/fixtures/person.rb",
|
36
|
+
"test/fixtures/person_with_default_school.rb",
|
37
|
+
"test/fixtures/person_with_default_school_id.rb",
|
38
|
+
"test/fixtures/school.rb",
|
39
|
+
"test/fixtures/schools.yml",
|
40
|
+
"test/schema.rb",
|
41
|
+
"test/test_active_record_defaults.rb",
|
42
|
+
"test/test_helper.rb"
|
43
|
+
]
|
44
|
+
s.homepage = %q{http://github.com/midas/active_record_defaults}
|
45
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
46
|
+
s.require_paths = ["lib"]
|
47
|
+
s.rubygems_version = %q{1.3.5}
|
48
|
+
s.summary = %q{Easily specify default values for attributes on new model objects.}
|
49
|
+
s.test_files = [
|
50
|
+
"test/abstract_unit.rb",
|
51
|
+
"test/fixtures/address.rb",
|
52
|
+
"test/fixtures/group.rb",
|
53
|
+
"test/fixtures/person.rb",
|
54
|
+
"test/fixtures/person_with_default_school.rb",
|
55
|
+
"test/fixtures/person_with_default_school_id.rb",
|
56
|
+
"test/fixtures/school.rb",
|
57
|
+
"test/schema.rb",
|
58
|
+
"test/test_active_record_defaults.rb",
|
59
|
+
"test/test_helper.rb"
|
60
|
+
]
|
61
|
+
|
62
|
+
if s.respond_to? :specification_version then
|
63
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
64
|
+
s.specification_version = 3
|
65
|
+
|
66
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
67
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2"])
|
68
|
+
else
|
69
|
+
s.add_dependency(%q<activerecord>, [">= 2"])
|
70
|
+
end
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<activerecord>, [">= 2"])
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Defaults
|
3
|
+
def self.included(base)
|
4
|
+
return if base.included_modules.include?(ActiveRecord::Defaults::InstanceMethods)
|
5
|
+
|
6
|
+
base.extend ClassMethods
|
7
|
+
base.send(:include, InstanceMethods)
|
8
|
+
|
9
|
+
base.send :alias_method, :initialize_without_defaults, :initialize
|
10
|
+
base.send :alias_method, :initialize, :initialize_with_defaults
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
# Define default values for attributes on new records. Requires a hash of <tt>attribute => value</tt> pairs, or a single attribute with an associated block.
|
15
|
+
# If the value is a block, it will be called to retrieve the default value.
|
16
|
+
# If the value is a symbol, a method by that name will be called on the object to retrieve the default value.
|
17
|
+
#
|
18
|
+
# The following code demonstrates the different ways default values can be specified. Defaults are applied in the order they are defined.
|
19
|
+
#
|
20
|
+
# class Person < ActiveRecord::Base
|
21
|
+
# defaults :name => 'My name', :city => lambda { 'My city' }
|
22
|
+
#
|
23
|
+
# default :birthdate do |person|
|
24
|
+
# Date.today if person.wants_birthday_today?
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# default :favourite_colour => :default_favourite_colour
|
28
|
+
#
|
29
|
+
# def default_favourite_colour
|
30
|
+
# "Blue"
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# The <tt>defaults</tt> and the <tt>default</tt> methods behave the same way. Use whichever is appropriate.
|
35
|
+
#
|
36
|
+
# The default values are only used if the key is not present in the given attributes.
|
37
|
+
#
|
38
|
+
# p = Person.new
|
39
|
+
# p.name # "My name"
|
40
|
+
# p.city # "My city"
|
41
|
+
#
|
42
|
+
# p = Person.new(:name => nil)
|
43
|
+
# p.name # nil
|
44
|
+
# p.city # "My city"
|
45
|
+
#
|
46
|
+
# == Default values for belongs_to associations
|
47
|
+
#
|
48
|
+
# Default values can also be specified for an association. For instance:
|
49
|
+
#
|
50
|
+
# class Student < ActiveRecord::Base
|
51
|
+
# belongs_to :school
|
52
|
+
# default :school => lambda { School.new }
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# In this scenario, if a school_id was provided in the attributes hash, the default value for the association will be ignored:
|
56
|
+
#
|
57
|
+
# s = Student.new
|
58
|
+
# s.school # => #<School: ...>
|
59
|
+
#
|
60
|
+
# s = Student.new(:school_id => nil)
|
61
|
+
# s.school # => nil
|
62
|
+
#
|
63
|
+
# Similarly, if a default value is specified for the foreign key and an object for the association is provided, the default foreign key is ignored.
|
64
|
+
def defaults(defaults, &block)
|
65
|
+
default_objects = case
|
66
|
+
when defaults.is_a?(Hash)
|
67
|
+
defaults.map { |attribute, value| Default.new(attribute, value) }
|
68
|
+
|
69
|
+
when defaults.is_a?(Symbol) && block
|
70
|
+
Default.new(defaults, block)
|
71
|
+
|
72
|
+
else
|
73
|
+
raise "pass either a hash of attribute/value pairs, or a single attribute with a block"
|
74
|
+
end
|
75
|
+
|
76
|
+
write_inheritable_array :attribute_defaults, [*default_objects]
|
77
|
+
end
|
78
|
+
|
79
|
+
alias_method :default, :defaults
|
80
|
+
end
|
81
|
+
|
82
|
+
module InstanceMethods
|
83
|
+
def initialize_with_defaults(attributes = nil)
|
84
|
+
initialize_without_defaults(attributes)
|
85
|
+
apply_default_attribute_values(attributes)
|
86
|
+
yield self if block_given?
|
87
|
+
end
|
88
|
+
|
89
|
+
def apply_default_attribute_values(attributes)
|
90
|
+
attribute_keys = (attributes || {}).keys.map!(&:to_s)
|
91
|
+
|
92
|
+
if attribute_defaults = self.class.read_inheritable_attribute(:attribute_defaults)
|
93
|
+
attribute_defaults.each do |default|
|
94
|
+
next if attribute_keys.include?(default.attribute)
|
95
|
+
|
96
|
+
# Ignore a default value for association_id if association has been specified
|
97
|
+
reflection = self.class.reflections[default.attribute.to_sym]
|
98
|
+
if reflection and reflection.macro == :belongs_to and attribute_keys.include?(reflection.primary_key_name)
|
99
|
+
next
|
100
|
+
end
|
101
|
+
|
102
|
+
# Ignore a default value for association if association_id has been specified
|
103
|
+
reflection = self.class.reflections.values.find { |r| r.respond_to?(:primary_key_name) && r.primary_key_name == default.attribute }
|
104
|
+
if reflection and reflection.macro == :belongs_to and attribute_keys.include?(reflection.name.to_s)
|
105
|
+
next
|
106
|
+
end
|
107
|
+
|
108
|
+
send("#{default.attribute}=", default.value(self))
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Default
|
115
|
+
attr_reader :attribute
|
116
|
+
|
117
|
+
def initialize(attribute, value)
|
118
|
+
@attribute, @value = attribute.to_s, value
|
119
|
+
end
|
120
|
+
|
121
|
+
def value(record)
|
122
|
+
if @value.is_a?(Symbol)
|
123
|
+
record.send(@value)
|
124
|
+
elsif @value.respond_to?(:call)
|
125
|
+
@value.call(record)
|
126
|
+
else
|
127
|
+
@value
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'active_record/defaults'
|
5
|
+
|
6
|
+
module ActiveRecordDefaults
|
7
|
+
VERSION = '1.0.0'
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveRecord::Base.send( :include, ActiveRecord::Defaults ) if defined?( ActiveRecord::Base )
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/active_record_defaults.rb'}"
|
9
|
+
puts "Loading active_record_defaults gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/environment'
|
5
|
+
rescue LoadError
|
6
|
+
require 'rubygems'
|
7
|
+
require_gem 'activerecord'
|
8
|
+
end
|
9
|
+
|
10
|
+
# Search for fixtures first
|
11
|
+
fixture_path = File.dirname(__FILE__) + '/fixtures/'
|
12
|
+
Dependencies.load_paths.insert(0, fixture_path)
|
13
|
+
|
14
|
+
require 'active_record/fixtures'
|
15
|
+
|
16
|
+
require File.dirname(__FILE__) + '/../lib/active_record_defaults'
|
17
|
+
|
18
|
+
ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
19
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
|
20
|
+
ActiveRecord::Base.establish_connection(ENV['DB'] || 'mysql')
|
21
|
+
|
22
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
23
|
+
|
24
|
+
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + '/fixtures/'
|
25
|
+
|
26
|
+
class Test::Unit::TestCase #:nodoc:
|
27
|
+
self.use_transactional_fixtures = true
|
28
|
+
self.use_instantiated_fixtures = false
|
29
|
+
end
|
data/test/database.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Address = Struct.new(:suburb, :city)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Person < ActiveRecord::Base
|
2
|
+
belongs_to :school
|
3
|
+
|
4
|
+
# Include an aggregate reflection to check compatibility
|
5
|
+
composed_of :address, :mapping => [%w(address_suburb suburb), %{address_city city}]
|
6
|
+
|
7
|
+
defaults :city => 'Christchurch', :country => Proc.new { 'New Zealand' }
|
8
|
+
|
9
|
+
default :first_name => 'Sean'
|
10
|
+
|
11
|
+
default :last_name do
|
12
|
+
'Fitzpatrick'
|
13
|
+
end
|
14
|
+
|
15
|
+
defaults :lucky_number => proc { 2 }, :favourite_colour => :default_favourite_colour
|
16
|
+
|
17
|
+
def default_favourite_colour
|
18
|
+
last_name == 'Fitzpatrick' ? "Blue" : "Red"
|
19
|
+
end
|
20
|
+
end
|
data/test/schema.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
ActiveRecord::Schema.define :version => 0 do
|
2
|
+
create_table :people, :force => true do |t|
|
3
|
+
t.column :first_name, :string
|
4
|
+
t.column :middle_name, :string
|
5
|
+
t.column :last_name, :string
|
6
|
+
t.column :city, :string
|
7
|
+
t.column :country, :string
|
8
|
+
t.column :birthdate, :date
|
9
|
+
t.column :lucky_number, :integer
|
10
|
+
t.column :favourite_colour, :string
|
11
|
+
t.column :type, :string
|
12
|
+
t.column :school_id, :integer
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table :schools, :force => true do |t|
|
16
|
+
t.column :name, :string
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require File.dirname(__FILE__) + '/abstract_unit.rb'
|
3
|
+
|
4
|
+
class ActiveRecordDefaultsTest < Test::Unit::TestCase
|
5
|
+
fixtures :people, :schools
|
6
|
+
|
7
|
+
def test_defaults_for_new_record
|
8
|
+
p = Person.new
|
9
|
+
|
10
|
+
assert_equal 'Christchurch', p.city
|
11
|
+
assert_equal 'New Zealand', p.country
|
12
|
+
assert_equal 'Sean', p.first_name
|
13
|
+
assert_equal 'Fitzpatrick', p.last_name
|
14
|
+
assert_equal 2, p.lucky_number
|
15
|
+
assert_equal 'Blue', p.favourite_colour
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_default_ignored_if_key_present
|
19
|
+
p = Person.new(:city => '', 'lucky_number' => nil)
|
20
|
+
|
21
|
+
assert_equal '', p.city
|
22
|
+
assert_equal nil, p.lucky_number
|
23
|
+
|
24
|
+
assert_equal 'New Zealand', p.country
|
25
|
+
assert_equal 'Sean', p.first_name
|
26
|
+
assert_equal 'Fitzpatrick', p.last_name
|
27
|
+
assert_equal 'Blue', p.favourite_colour
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_existing_records_unchanged
|
31
|
+
assert_nil Person.find(1).last_name
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_default_relying_on_previous_default
|
35
|
+
assert_equal "Red", Person.new(:last_name => 'Carter').favourite_colour
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_defaults_on_create
|
39
|
+
p = Person.create!
|
40
|
+
|
41
|
+
assert_equal 'Christchurch', p.city
|
42
|
+
assert_equal 'New Zealand', p.country
|
43
|
+
assert_equal 'Sean', p.first_name
|
44
|
+
assert_equal 'Fitzpatrick', p.last_name
|
45
|
+
assert_equal 2, p.lucky_number
|
46
|
+
assert_equal 'Blue', p.favourite_colour
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_default_belongs_to_association
|
50
|
+
assert_equal School.find(1), PersonWithDefaultSchool.new.school
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_specific_belongs_to_foreign_key_value_overrides_association_default
|
54
|
+
assert_nil PersonWithDefaultSchool.new(:school_id => nil).school
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_default_belongs_to_association_by_id
|
58
|
+
assert_equal School.find(1), PersonWithDefaultSchoolId.new.school
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_specific_belong_to_association_overrides_default_foreign_key_value
|
62
|
+
assert_nil PersonWithDefaultSchoolId.new(:school => nil).school
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_initialize_model_without_any_defaults
|
66
|
+
assert_nothing_raised { Group.new }
|
67
|
+
end
|
68
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_defaults
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Viney (aussiegeek)
|
8
|
+
- C. Jason Harrelson (midas)
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-11-13 00:00:00 -06:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "2"
|
25
|
+
version:
|
26
|
+
description: Allows you to easily specify default values for attributes on new model objects.
|
27
|
+
email: jonathan.viney@gmail.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- History.txt
|
36
|
+
- MIT-LICENSE
|
37
|
+
- README.rdoc
|
38
|
+
- Rakefile
|
39
|
+
- VERSION
|
40
|
+
- active_record_defaults.gemspec
|
41
|
+
- lib/active_record/defaults.rb
|
42
|
+
- lib/active_record_defaults.rb
|
43
|
+
- script/console
|
44
|
+
- script/destroy
|
45
|
+
- script/generate
|
46
|
+
- test/abstract_unit.rb
|
47
|
+
- test/database.yml
|
48
|
+
- test/fixtures/address.rb
|
49
|
+
- test/fixtures/group.rb
|
50
|
+
- test/fixtures/people.yml
|
51
|
+
- test/fixtures/person.rb
|
52
|
+
- test/fixtures/person_with_default_school.rb
|
53
|
+
- test/fixtures/person_with_default_school_id.rb
|
54
|
+
- test/fixtures/school.rb
|
55
|
+
- test/fixtures/schools.yml
|
56
|
+
- test/schema.rb
|
57
|
+
- test/test_active_record_defaults.rb
|
58
|
+
- test/test_helper.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/midas/active_record_defaults
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --charset=UTF-8
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.5
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Easily specify default values for attributes on new model objects.
|
87
|
+
test_files:
|
88
|
+
- test/abstract_unit.rb
|
89
|
+
- test/fixtures/address.rb
|
90
|
+
- test/fixtures/group.rb
|
91
|
+
- test/fixtures/person.rb
|
92
|
+
- test/fixtures/person_with_default_school.rb
|
93
|
+
- test/fixtures/person_with_default_school_id.rb
|
94
|
+
- test/fixtures/school.rb
|
95
|
+
- test/schema.rb
|
96
|
+
- test/test_active_record_defaults.rb
|
97
|
+
- test/test_helper.rb
|