has_defaults 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/.gitignore +2 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +74 -0
  4. data/Rakefile +17 -0
  5. data/has_defaults.gemspec +19 -0
  6. data/lib/has_defaults/active_record_ext.rb +54 -0
  7. data/lib/has_defaults/version.rb +5 -0
  8. data/lib/has_defaults.rb +4 -0
  9. data/pkg/has_defaults-0.3.0.gem +0 -0
  10. data/spec/app_root/log/in_memory.log +147 -0
  11. data/spec/rails2/Gemfile +8 -0
  12. data/spec/rails2/Gemfile.lock +51 -0
  13. data/spec/rails2/Rakefile +11 -0
  14. data/spec/rails2/app_root/app/controllers/application_controller.rb +2 -0
  15. data/spec/rails2/app_root/app/models/donut.rb +21 -0
  16. data/spec/rails2/app_root/app/models/model_without_defaults.rb +2 -0
  17. data/spec/rails2/app_root/app/models/pastry.rb +6 -0
  18. data/spec/rails2/app_root/config/boot.rb +114 -0
  19. data/spec/rails2/app_root/config/database.yml +21 -0
  20. data/spec/rails2/app_root/config/environment.rb +14 -0
  21. data/spec/rails2/app_root/config/environments/in_memory.rb +0 -0
  22. data/spec/rails2/app_root/config/environments/mysql.rb +0 -0
  23. data/spec/rails2/app_root/config/environments/postgresql.rb +0 -0
  24. data/spec/rails2/app_root/config/environments/sqlite.rb +0 -0
  25. data/spec/rails2/app_root/config/environments/sqlite3.rb +0 -0
  26. data/spec/rails2/app_root/config/routes.rb +4 -0
  27. data/spec/rails2/app_root/db/migrate/001_create_pastries.rb +18 -0
  28. data/spec/rails2/app_root/db/migrate/002_create_model_without_defaults.rb +12 -0
  29. data/spec/rails2/app_root/lib/console_with_fixtures.rb +4 -0
  30. data/spec/rails2/app_root/log/.gitignore +1 -0
  31. data/spec/rails2/app_root/script/console +7 -0
  32. data/spec/rails2/has_defaults_spec.rb +79 -0
  33. data/spec/rails2/rcov.opts +2 -0
  34. data/spec/rails2/spec.opts +4 -0
  35. data/spec/rails2/spec_helper.rb +20 -0
  36. data/spec/rails3/.rspec +2 -0
  37. data/spec/rails3/Gemfile +9 -0
  38. data/spec/rails3/Gemfile.lock +101 -0
  39. data/spec/rails3/Rakefile +10 -0
  40. data/spec/rails3/app_root/.gitignore +4 -0
  41. data/spec/rails3/app_root/app/controllers/application_controller.rb +2 -0
  42. data/spec/rails3/app_root/app/models/donut.rb +21 -0
  43. data/spec/rails3/app_root/app/models/model_without_defaults.rb +2 -0
  44. data/spec/rails3/app_root/app/models/pastry.rb +6 -0
  45. data/spec/rails3/app_root/config/application.rb +32 -0
  46. data/spec/rails3/app_root/config/boot.rb +13 -0
  47. data/spec/rails3/app_root/config/database.yml +21 -0
  48. data/spec/rails3/app_root/config/environment.rb +5 -0
  49. data/spec/rails3/app_root/config/environments/in_memory.rb +0 -0
  50. data/spec/rails3/app_root/config/environments/mysql.rb +0 -0
  51. data/spec/rails3/app_root/config/environments/postgresql.rb +0 -0
  52. data/spec/rails3/app_root/config/environments/sqlite.rb +0 -0
  53. data/spec/rails3/app_root/config/environments/sqlite3.rb +0 -0
  54. data/spec/rails3/app_root/config/environments/test.rb +35 -0
  55. data/spec/rails3/app_root/config/initializers/backtrace_silencers.rb +7 -0
  56. data/spec/rails3/app_root/config/initializers/inflections.rb +10 -0
  57. data/spec/rails3/app_root/config/initializers/mime_types.rb +5 -0
  58. data/spec/rails3/app_root/config/initializers/secret_token.rb +7 -0
  59. data/spec/rails3/app_root/config/initializers/session_store.rb +8 -0
  60. data/spec/rails3/app_root/config/locales/en.yml +5 -0
  61. data/spec/rails3/app_root/config/routes.rb +58 -0
  62. data/spec/rails3/app_root/db/migrate/001_create_pastries.rb +18 -0
  63. data/spec/rails3/app_root/db/migrate/002_create_model_without_defaults.rb +12 -0
  64. data/spec/rails3/app_root/lib/tasks/.gitkeep +0 -0
  65. data/spec/rails3/app_root/log/.gitkeep +0 -0
  66. data/spec/rails3/app_root/script/rails +6 -0
  67. data/spec/rails3/has_defaults_spec.rb +74 -0
  68. data/spec/rails3/log/in_memory.log +33 -0
  69. data/spec/rails3/rcov.opts +2 -0
  70. data/spec/rails3/spec_helper.rb +21 -0
  71. metadata +149 -0
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .idea/*
2
+
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Nando Vieira
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 ADDED
@@ -0,0 +1,74 @@
1
+ has_defaults
2
+ ============
3
+
4
+ Default values for ActiveRecord models.
5
+
6
+ INSTALLATION
7
+ ------------
8
+
9
+ In your `Gemfile`
10
+
11
+ gem 'has_defaults'
12
+
13
+ Now run
14
+
15
+ bundle install
16
+
17
+
18
+ USAGE
19
+ -----
20
+
21
+ Add the method call `has_defaults` to your model.
22
+
23
+ class Page < ActiveRecord::Base
24
+ has_defaults :title => "New page", :body => "Put your text here"
25
+ end
26
+
27
+ Attributes will be set only if it's a new record and the attribute is blank.
28
+
29
+ Retrieve the default attribute with the `default_for` instance method:
30
+
31
+ @page.default_for(:title)
32
+
33
+ You can pass Proc as attribute:
34
+
35
+ has_defaults :expires_at => proc { Time.now }
36
+
37
+ You can override the default attributes as follow:
38
+
39
+ Page.has_defaults_options = {:title => "Here's your new page", :body => "Write your page text"}
40
+
41
+ NOTE
42
+ ----
43
+
44
+ Is **Ruby 1.9** ready!
45
+
46
+ MAINTAINER
47
+ ----------
48
+
49
+ * Original version by Nando Vieira (<http://simplesideias.com.br>)
50
+ * Patches in this fork by Henning Koch (<http://www.makandra.de/>)
51
+
52
+ LICENSE:
53
+ --------
54
+
55
+ (The MIT License)
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining
58
+ a copy of this software and associated documentation files (the
59
+ 'Software'), to deal in the Software without restriction, including
60
+ without limitation the rights to use, copy, modify, merge, publish,
61
+ distribute, sublicense, and/or sell copies of the Software, and to
62
+ permit persons to whom the Software is furnished to do so, subject to
63
+ the following conditions:
64
+
65
+ The above copyright notice and this permission notice shall be
66
+ included in all copies or substantial portions of the Software.
67
+
68
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
69
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
70
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
71
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
72
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
73
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
74
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+ require 'bundler/gem_tasks'
4
+
5
+ desc 'Default: Run all specs.'
6
+ task :default => :all_specs
7
+
8
+ desc "Run all specs"
9
+ task :all_specs do
10
+ Dir['spec/**/Rakefile'].each do |rakefile|
11
+ directory_name = File.dirname(rakefile)
12
+ sh <<-CMD
13
+ cd #{directory_name}
14
+ bundle exec rake
15
+ CMD
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "has_defaults/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'has_defaults'
6
+ s.version = HasDefaults::VERSION
7
+ s.authors = ["Henning Koch"]
8
+ s.email = 'henning.koch@makandra.de'
9
+ s.homepage = 'https://github.com/makandra/has_defaults'
10
+ s.summary = 'Default values for ActiveRecord models.'
11
+ s.description = s.summary
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_dependency('rspec')
19
+ end
@@ -0,0 +1,54 @@
1
+ module HasDefaults
2
+ module ActiveRecordExt
3
+
4
+ module ClassMethods
5
+
6
+ def has_defaults(attrs)
7
+ raise "Hash expected; #{attrs.class} given." unless attrs.is_a?(Hash)
8
+
9
+ include InstanceMethods
10
+
11
+ unless respond_to?(:has_defaults_options)
12
+ class_inheritable_hash :has_defaults_options
13
+ end
14
+
15
+ self.has_defaults_options ||= {}
16
+ self.has_defaults_options.merge!(attrs)
17
+
18
+ if Rails.version < '3'
19
+ # ActiveRecord only calls after_initialize callbacks only if is
20
+ # explicit defined in a class. We should however only define that
21
+ # callback if a default has been set, as it really slow downs Rails.
22
+ unless method_defined?(:after_initialize)
23
+ define_method(:after_initialize) {}
24
+ end
25
+ end
26
+
27
+ after_initialize :set_default_attributes
28
+ end
29
+
30
+ end
31
+
32
+ module InstanceMethods
33
+
34
+ def default_for(name)
35
+ self.class.has_defaults_options[name.to_sym]
36
+ end
37
+
38
+ private
39
+
40
+ def set_default_attributes
41
+ if new_record?
42
+ self.class.has_defaults_options.each do |name, value|
43
+ if send(name).nil?
44
+ value = instance_eval(&value) if value.respond_to?(:call)
45
+ send("#{name}=", value)
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module HasDefaults
2
+
3
+ VERSION = '0.3.0'
4
+
5
+ end
@@ -0,0 +1,4 @@
1
+ require 'has_defaults/active_record_ext'
2
+
3
+ ActiveRecord::Base.__send__(:extend, HasDefaults::ActiveRecordExt::ClassMethods)
4
+
Binary file
@@ -0,0 +1,147 @@
1
+ # Logfile created on Mon Mar 22 10:14:11 +0100 2010 SQL (0.5ms)  SELECT name
2
+ FROM sqlite_master
3
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
4
+ 
5
+ SQL (0.2ms) select sqlite_version(*)
6
+ SQL (0.3ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
7
+ SQL (0.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
8
+ SQL (0.2ms)  SELECT name
9
+ FROM sqlite_master
10
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
11
+ 
12
+ SQL (0.1ms) SELECT version FROM schema_migrations
13
+ Migrating to CreatePastries (1)
14
+ SQL (0.2ms) CREATE TABLE "pastries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "type" varchar(255), "flavor" varchar(255), "name" varchar(255), "maker" varchar(255), "main_ingredient" varchar(255)) 
15
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('1')
16
+ Migrating to CreateModelWithoutDefaults (2)
17
+ SQL (0.2ms) CREATE TABLE "model_without_defaults" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) 
18
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('2')
19
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'vanilla')
20
+ Donut Load (0.3ms) SELECT * FROM "pastries" WHERE ( ("pastries"."type" = 'Donut' ) ) LIMIT 1
21
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'cream')
22
+ SQL (0.3ms)  SELECT name
23
+ FROM sqlite_master
24
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
25
+ 
26
+ SQL (0.2ms) select sqlite_version(*)
27
+ SQL (0.3ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
28
+ SQL (0.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
29
+ SQL (0.2ms)  SELECT name
30
+ FROM sqlite_master
31
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
32
+ 
33
+ SQL (0.1ms) SELECT version FROM schema_migrations
34
+ Migrating to CreatePastries (1)
35
+ SQL (0.2ms) CREATE TABLE "pastries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "type" varchar(255), "flavor" varchar(255), "name" varchar(255), "maker" varchar(255), "main_ingredient" varchar(255)) 
36
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('1')
37
+ Migrating to CreateModelWithoutDefaults (2)
38
+ SQL (0.1ms) CREATE TABLE "model_without_defaults" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) 
39
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('2')
40
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'vanilla')
41
+ Donut Load (0.3ms) SELECT * FROM "pastries" WHERE ( ("pastries"."type" = 'Donut' ) ) LIMIT 1
42
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'cream')
43
+ SQL (0.3ms)  SELECT name
44
+ FROM sqlite_master
45
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
46
+ 
47
+ SQL (0.2ms) select sqlite_version(*)
48
+ SQL (0.3ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
49
+ SQL (0.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
50
+ SQL (0.2ms)  SELECT name
51
+ FROM sqlite_master
52
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
53
+ 
54
+ SQL (0.1ms) SELECT version FROM schema_migrations
55
+ Migrating to CreatePastries (1)
56
+ SQL (0.2ms) CREATE TABLE "pastries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "type" varchar(255), "flavor" varchar(255), "name" varchar(255), "maker" varchar(255), "main_ingredient" varchar(255)) 
57
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('1')
58
+ Migrating to CreateModelWithoutDefaults (2)
59
+ SQL (0.2ms) CREATE TABLE "model_without_defaults" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) 
60
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('2')
61
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'vanilla')
62
+ Donut Load (0.3ms) SELECT * FROM "pastries" WHERE ( ("pastries"."type" = 'Donut' ) ) LIMIT 1
63
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'cream')
64
+ SQL (0.3ms)  SELECT name
65
+ FROM sqlite_master
66
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
67
+ 
68
+ SQL (0.2ms) select sqlite_version(*)
69
+ SQL (0.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
70
+ SQL (0.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
71
+ SQL (0.2ms)  SELECT name
72
+ FROM sqlite_master
73
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
74
+ 
75
+ SQL (0.1ms) SELECT version FROM schema_migrations
76
+ Migrating to CreatePastries (1)
77
+ SQL (0.2ms) CREATE TABLE "pastries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "type" varchar(255), "flavor" varchar(255), "name" varchar(255), "maker" varchar(255), "main_ingredient" varchar(255)) 
78
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('1')
79
+ Migrating to CreateModelWithoutDefaults (2)
80
+ SQL (0.1ms) CREATE TABLE "model_without_defaults" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) 
81
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('2')
82
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'vanilla')
83
+ Donut Load (0.3ms) SELECT * FROM "pastries" WHERE ( ("pastries"."type" = 'Donut' ) ) LIMIT 1
84
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'cream')
85
+ SQL (0.3ms)  SELECT name
86
+ FROM sqlite_master
87
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
88
+ 
89
+ SQL (0.7ms) select sqlite_version(*)
90
+ SQL (0.8ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
91
+ SQL (0.5ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
92
+ SQL (0.6ms)  SELECT name
93
+ FROM sqlite_master
94
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
95
+ 
96
+ SQL (0.3ms) SELECT version FROM schema_migrations
97
+ Migrating to CreatePastries (1)
98
+ SQL (0.9ms) CREATE TABLE "pastries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "type" varchar(255), "flavor" varchar(255), "name" varchar(255), "maker" varchar(255), "main_ingredient" varchar(255)) 
99
+ SQL (0.3ms) INSERT INTO schema_migrations (version) VALUES ('1')
100
+ Migrating to CreateModelWithoutDefaults (2)
101
+ SQL (0.6ms) CREATE TABLE "model_without_defaults" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) 
102
+ SQL (0.3ms) INSERT INTO schema_migrations (version) VALUES ('2')
103
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'vanilla')
104
+ Donut Load (0.3ms) SELECT * FROM "pastries" WHERE ( ("pastries"."type" = 'Donut' ) ) LIMIT 1
105
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'cream')
106
+ SQL (0.3ms)  SELECT name
107
+ FROM sqlite_master
108
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
109
+ 
110
+ SQL (0.2ms) select sqlite_version(*)
111
+ SQL (0.3ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
112
+ SQL (0.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
113
+ SQL (0.2ms)  SELECT name
114
+ FROM sqlite_master
115
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
116
+ 
117
+ SQL (0.1ms) SELECT version FROM schema_migrations
118
+ Migrating to CreatePastries (1)
119
+ SQL (0.3ms) CREATE TABLE "pastries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "type" varchar(255), "flavor" varchar(255), "name" varchar(255), "maker" varchar(255), "main_ingredient" varchar(255)) 
120
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('1')
121
+ Migrating to CreateModelWithoutDefaults (2)
122
+ SQL (0.2ms) CREATE TABLE "model_without_defaults" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) 
123
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('2')
124
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'vanilla')
125
+ Donut Load (0.3ms) SELECT * FROM "pastries" WHERE ( ("pastries"."type" = 'Donut' ) ) LIMIT 1
126
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'cream')
127
+ SQL (0.3ms)  SELECT name
128
+ FROM sqlite_master
129
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
130
+ 
131
+ SQL (0.2ms) select sqlite_version(*)
132
+ SQL (0.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
133
+ SQL (0.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
134
+ SQL (0.2ms)  SELECT name
135
+ FROM sqlite_master
136
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
137
+ 
138
+ SQL (0.1ms) SELECT version FROM schema_migrations
139
+ Migrating to CreatePastries (1)
140
+ SQL (0.3ms) CREATE TABLE "pastries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "type" varchar(255), "flavor" varchar(255), "name" varchar(255), "maker" varchar(255), "main_ingredient" varchar(255)) 
141
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('1')
142
+ Migrating to CreateModelWithoutDefaults (2)
143
+ SQL (0.2ms) CREATE TABLE "model_without_defaults" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL) 
144
+ SQL (0.1ms) INSERT INTO schema_migrations (version) VALUES ('2')
145
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'vanilla')
146
+ Donut Load (0.3ms) SELECT * FROM "pastries" WHERE ( ("pastries"."type" = 'Donut' ) ) LIMIT 1
147
+ Donut Create (0.1ms) INSERT INTO "pastries" ("name", "main_ingredient", "type", "maker", "flavor") VALUES('Cream', 'flour', 'Donut', 'Dunkin Donuts', 'cream')
@@ -0,0 +1,8 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'sqlite3'
4
+ gem 'rails', '=2.3.10'
5
+ gem 'rspec', '<2'
6
+ gem 'rspec-rails', '<2'
7
+ gem 'ruby-debug'
8
+ gem 'has_defaults', :path => '../..'
@@ -0,0 +1,51 @@
1
+ PATH
2
+ remote: ../..
3
+ specs:
4
+ has_defaults (0.3.0)
5
+ rspec
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ actionmailer (2.3.10)
11
+ actionpack (= 2.3.10)
12
+ actionpack (2.3.10)
13
+ activesupport (= 2.3.10)
14
+ rack (~> 1.1.0)
15
+ activerecord (2.3.10)
16
+ activesupport (= 2.3.10)
17
+ activeresource (2.3.10)
18
+ activesupport (= 2.3.10)
19
+ activesupport (2.3.10)
20
+ columnize (0.3.2)
21
+ linecache (0.43)
22
+ rack (1.1.0)
23
+ rails (2.3.10)
24
+ actionmailer (= 2.3.10)
25
+ actionpack (= 2.3.10)
26
+ activerecord (= 2.3.10)
27
+ activeresource (= 2.3.10)
28
+ activesupport (= 2.3.10)
29
+ rake (>= 0.8.3)
30
+ rake (0.8.7)
31
+ rspec (1.3.1)
32
+ rspec-rails (1.3.3)
33
+ rack (>= 1.0.0)
34
+ rspec (= 1.3.1)
35
+ ruby-debug (0.10.4)
36
+ columnize (>= 0.1)
37
+ ruby-debug-base (~> 0.10.4.0)
38
+ ruby-debug-base (0.10.4)
39
+ linecache (>= 0.3)
40
+ sqlite3 (1.3.6)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ has_defaults!
47
+ rails (= 2.3.10)
48
+ rspec (< 2)
49
+ rspec-rails (< 2)
50
+ ruby-debug
51
+ sqlite3
@@ -0,0 +1,11 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ desc 'Default: Run all specs for a specific rails version.'
5
+ task :default => :spec
6
+
7
+ desc "Run all specs for a specific rails version"
8
+ Spec::Rake::SpecTask.new() do |t|
9
+ t.spec_opts = ['--options', "\"spec.opts\""]
10
+ t.spec_files = FileList['**/*_spec.rb']
11
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,21 @@
1
+ class Donut < Pastry
2
+
3
+ has_defaults :flavor => "cream", :name => "Cream"
4
+ has_defaults :maker => proc { "Dunkin Donuts" }
5
+ has_defaults :weight => proc { weigh }
6
+
7
+ def flavor
8
+ @flavor_getter_called = true
9
+ read_attribute(:flavor)
10
+ end
11
+
12
+ def flavor=(value)
13
+ @flavor_setter_called = true
14
+ write_attribute(:flavor, value)
15
+ end
16
+
17
+ def weigh
18
+ "a lot"
19
+ end
20
+
21
+ end
@@ -0,0 +1,2 @@
1
+ class ModelWithoutDefaults < ActiveRecord::Base
2
+ end
@@ -0,0 +1,6 @@
1
+ class Pastry < ActiveRecord::Base
2
+
3
+ has_defaults :main_ingredient => "flour", :maker => "Mom"
4
+
5
+ end
6
+
@@ -0,0 +1,114 @@
1
+ # Allow customization of the rails framework path
2
+ RAILS_FRAMEWORK_ROOT = (ENV['RAILS_FRAMEWORK_ROOT'] || "#{File.dirname(__FILE__)}/../../../../../../vendor/rails") unless defined?(RAILS_FRAMEWORK_ROOT)
3
+
4
+ # Don't change this file!
5
+ # Configure your app in config/environment.rb and config/environments/*.rb
6
+
7
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
8
+
9
+ module Rails
10
+ class << self
11
+ def boot!
12
+ unless booted?
13
+ preinitialize
14
+ pick_boot.run
15
+ end
16
+ end
17
+
18
+ def booted?
19
+ defined? Rails::Initializer
20
+ end
21
+
22
+ def pick_boot
23
+ (vendor_rails? ? VendorBoot : GemBoot).new
24
+ end
25
+
26
+ def vendor_rails?
27
+ File.exist?(RAILS_FRAMEWORK_ROOT)
28
+ end
29
+
30
+ def preinitialize
31
+ load(preinitializer_path) if File.exist?(preinitializer_path)
32
+ end
33
+
34
+ def preinitializer_path
35
+ "#{RAILS_ROOT}/config/preinitializer.rb"
36
+ end
37
+ end
38
+
39
+ class Boot
40
+ def run
41
+ load_initializer
42
+ Rails::Initializer.run(:set_load_path)
43
+ end
44
+ end
45
+
46
+ class VendorBoot < Boot
47
+ def load_initializer
48
+ require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/initializer"
49
+ Rails::Initializer.run(:install_gem_spec_stubs)
50
+ end
51
+ end
52
+
53
+ class GemBoot < Boot
54
+ def load_initializer
55
+ self.class.load_rubygems
56
+ load_rails_gem
57
+ require 'initializer'
58
+ end
59
+
60
+ def load_rails_gem
61
+ if version = self.class.gem_version
62
+ gem 'rails', version
63
+ else
64
+ gem 'rails'
65
+ end
66
+ rescue Gem::LoadError => load_error
67
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
68
+ exit 1
69
+ end
70
+
71
+ class << self
72
+ def rubygems_version
73
+ Gem::RubyGemsVersion rescue nil
74
+ end
75
+
76
+ def gem_version
77
+ if defined? RAILS_GEM_VERSION
78
+ RAILS_GEM_VERSION
79
+ elsif ENV.include?('RAILS_GEM_VERSION')
80
+ ENV['RAILS_GEM_VERSION']
81
+ else
82
+ parse_gem_version(read_environment_rb)
83
+ end
84
+ end
85
+
86
+ def load_rubygems
87
+ require 'rubygems'
88
+ min_version = '1.1.1'
89
+ unless rubygems_version >= min_version
90
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
91
+ exit 1
92
+ end
93
+
94
+ rescue LoadError
95
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
96
+ exit 1
97
+ end
98
+
99
+ def parse_gem_version(text)
100
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
101
+ end
102
+
103
+ private
104
+ def read_environment_rb
105
+ environment_rb = "#{RAILS_ROOT}/config/environment.rb"
106
+ environment_rb = "#{HELPER_RAILS_ROOT}/config/environment.rb" unless File.exists?(environment_rb)
107
+ File.read(environment_rb)
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ # All that for this:
114
+ Rails.boot!
@@ -0,0 +1,21 @@
1
+ in_memory:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+ verbosity: quiet
5
+ sqlite:
6
+ adapter: sqlite
7
+ dbfile: plugin_test.sqlite.db
8
+ sqlite3:
9
+ adapter: sqlite3
10
+ dbfile: plugin_test.sqlite3.db
11
+ postgresql:
12
+ adapter: postgresql
13
+ username: postgres
14
+ password: postgres
15
+ database: plugin_test
16
+ mysql:
17
+ adapter: mysql
18
+ host: localhost
19
+ username: root
20
+ password:
21
+ database: plugin_test
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), 'boot')
2
+
3
+ Rails::Initializer.run do |config|
4
+ config.cache_classes = false
5
+ config.whiny_nils = true
6
+ config.action_controller.session = { :key => "_myapp_session", :secret => "gwirofjweroijger8924rt2zfwehfuiwehb1378rifowenfoqwphf23" }
7
+ config.plugin_locators.unshift(
8
+ Class.new(Rails::Plugin::Locator) do
9
+ def plugins
10
+ [Rails::Plugin.new(File.expand_path('.'))]
11
+ end
12
+ end
13
+ ) unless defined?(PluginTestHelper::PluginLocator)
14
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.connect ':controller/:action/:id'
3
+ map.connect ':controller/:action/:id.:format'
4
+ end
@@ -0,0 +1,18 @@
1
+ class CreatePastries < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_table :pastries do |t|
5
+ t.string :type
6
+ t.string :flavor
7
+ t.string :name
8
+ t.string :maker
9
+ t.string :weight
10
+ t.string :main_ingredient
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :pastries
16
+ end
17
+
18
+ end