Permalink
Browse files

Fix a bug where defaults were not run in Rails 2. Run specs in 3 diff…

…erent versions of Rails.
  • Loading branch information...
triskweline committed Jul 5, 2012
1 parent fe130f9 commit 4e1e2337484c45327357f7c295fb7caa8ce44528
Showing with 612 additions and 40 deletions.
  1. +1 −2 .gitignore
  2. +0 −3 Gemfile
  3. +26 −5 Rakefile
  4. +0 −5 lib/assignable_values/active_record.rb
  5. +17 −1 lib/assignable_values/active_record/restriction/base.rb
  6. +1 −1 lib/assignable_values/version.rb
  7. 0 spec/app_root/config/environments/sqlite.rb
  8. 0 spec/app_root/config/environments/sqlite3.rb
  9. +8 −0 spec/rails-2.3/Gemfile
  10. +11 −0 spec/rails-2.3/Rakefile
  11. +1 −0 spec/rails-2.3/app_root/app
  12. +129 −0 spec/rails-2.3/app_root/config/boot.rb
  13. +1 −1 spec/{ → rails-2.3}/app_root/config/database.yml
  14. +14 −0 spec/rails-2.3/app_root/config/environment.rb
  15. +28 −0 spec/rails-2.3/app_root/config/environments/test.rb
  16. +1 −0 spec/rails-2.3/app_root/config/locales
  17. +20 −0 spec/rails-2.3/app_root/config/preinitializer.rb
  18. +4 −0 spec/rails-2.3/app_root/config/routes.rb
  19. +1 −0 spec/rails-2.3/app_root/db
  20. +4 −0 spec/rails-2.3/app_root/lib/console_with_fixtures.rb
  21. +1 −0 spec/rails-2.3/app_root/log/.gitignore
  22. +7 −0 spec/rails-2.3/app_root/script/console
  23. 0 spec/{ → rails-2.3}/rcov.opts
  24. +4 −0 spec/rails-2.3/spec.opts
  25. +24 −0 spec/rails-2.3/spec_helper.rb
  26. +2 −0 spec/rails-3.0/.rspec
  27. +9 −0 spec/rails-3.0/Gemfile
  28. +10 −0 spec/rails-3.0/Rakefile
  29. 0 spec/{ → rails-3.0}/app_root/.gitignore
  30. +1 −0 spec/rails-3.0/app_root/app
  31. +2 −1 spec/{ → rails-3.0}/app_root/config/application.rb
  32. 0 spec/{ → rails-3.0}/app_root/config/boot.rb
  33. +4 −0 spec/rails-3.0/app_root/config/database.yml
  34. +1 −1 spec/{ → rails-3.0}/app_root/config/environment.rb
  35. +2 −2 spec/{ → rails-3.0}/app_root/config/environments/test.rb
  36. 0 spec/{ → rails-3.0}/app_root/config/initializers/backtrace_silencers.rb
  37. 0 spec/{ → rails-3.0}/app_root/config/initializers/inflections.rb
  38. 0 spec/{ → rails-3.0}/app_root/config/initializers/mime_types.rb
  39. +1 −1 spec/{ → rails-3.0}/app_root/config/initializers/secret_token.rb
  40. +2 −2 spec/{ → rails-3.0}/app_root/config/initializers/session_store.rb
  41. +1 −0 spec/rails-3.0/app_root/config/locales
  42. +1 −1 spec/{ → rails-3.0}/app_root/config/routes.rb
  43. +1 −0 spec/rails-3.0/app_root/db
  44. 0 spec/{ → rails-3.0}/app_root/lib/tasks/.gitkeep
  45. 0 spec/{app_root/config/environments/in_memory.rb → rails-3.0/app_root/log/.gitkeep}
  46. 0 spec/{ → rails-3.0}/app_root/script/rails
  47. +2 −0 spec/rails-3.0/rcov.opts
  48. +8 −4 spec/{ → rails-3.0}/spec_helper.rb
  49. +2 −0 spec/rails-3.2/.rspec
  50. +9 −0 spec/rails-3.2/Gemfile
  51. +10 −0 spec/rails-3.2/Rakefile
  52. +4 −0 spec/rails-3.2/app_root/.gitignore
  53. +1 −0 spec/rails-3.2/app_root/app
  54. +32 −0 spec/rails-3.2/app_root/config/application.rb
  55. +13 −0 spec/rails-3.2/app_root/config/boot.rb
  56. +4 −0 spec/rails-3.2/app_root/config/database.yml
  57. +5 −0 spec/rails-3.2/app_root/config/environment.rb
  58. +35 −0 spec/rails-3.2/app_root/config/environments/test.rb
  59. +7 −0 spec/rails-3.2/app_root/config/initializers/backtrace_silencers.rb
  60. +10 −0 spec/rails-3.2/app_root/config/initializers/inflections.rb
  61. +5 −0 spec/rails-3.2/app_root/config/initializers/mime_types.rb
  62. +7 −0 spec/rails-3.2/app_root/config/initializers/secret_token.rb
  63. +8 −0 spec/rails-3.2/app_root/config/initializers/session_store.rb
  64. +1 −0 spec/rails-3.2/app_root/config/locales
  65. +58 −0 spec/rails-3.2/app_root/config/routes.rb
  66. +1 −0 spec/rails-3.2/app_root/db
  67. 0 spec/{app_root/config/environments/mysql.rb → rails-3.2/app_root/lib/tasks/.gitkeep}
  68. 0 spec/{app_root/config/environments/postgresql.rb → rails-3.2/app_root/log/.gitkeep}
  69. +6 −0 spec/rails-3.2/app_root/script/rails
  70. +2 −0 spec/rails-3.2/rcov.opts
  71. +25 −0 spec/rails-3.2/spec_helper.rb
  72. 0 spec/{ → shared}/app_root/app/controllers/application_controller.rb
  73. 0 spec/{ → shared}/app_root/app/models/artist.rb
  74. 0 spec/{ → shared}/app_root/app/models/song.rb
  75. +5 −1 spec/{ → shared}/app_root/config/locales/en.yml
  76. 0 spec/{ → shared}/app_root/db/migrate/001_create_artists.rb
  77. 0 spec/{ → shared}/app_root/db/migrate/002_create_songs.rb
  78. +12 −9 spec/{ → shared}/assignable_values/active_record_spec.rb
View
@@ -2,7 +2,6 @@ doc
pkg
*.gem
.idea
-spec/app_root/log/*
+spec/*/log/*
Gemfile.lock
-
View
@@ -1,3 +0,0 @@
-source 'http://rubygems.org'
-
-gemspec
View
@@ -1,11 +1,32 @@
require 'rake'
-require 'rspec/core/rake_task'
require 'bundler/gem_tasks'
desc 'Default: Run all specs.'
-task :default => :spec
+task :default => 'all:spec'
-desc "Run all specs"
-RSpec::Core::RakeTask.new do |t|
-# t.spec_files = FileList['spec/**/*_spec.rb']
+namespace :all do
+
+ desc "Run specs on all spec apps"
+ task :spec do
+ for_each_directory_of('spec/**/Rakefile') do |directory|
+ env = "SPEC=../../#{ENV['SPEC']} " if ENV['SPEC']
+ system("cd #{directory} && #{env} bundle exec rake spec")
+ end
+ end
+
+ desc "Bundle all spec apps"
+ task :bundle do
+ for_each_directory_of('spec/**/Gemfile') do |directory|
+ system("cd #{directory} && bundle install")
+ end
+ end
+
+end
+
+def for_each_directory_of(path, &block)
+ Dir[path].sort.each do |rakefile|
+ directory = File.dirname(rakefile)
+ puts '', "\033[44m#{directory}\033[0m", ''
+ block.call(directory)
+ end
end
@@ -8,11 +8,6 @@ def assignable_values_for(property, options = {}, &values)
restriction_type.new(self, property, options, &values)
end
- #def authorize_values_for(property, options = {})
- # method_defined?(:power) or attr_accessor :power
- # assignable_values_for property, options.merge(:through => :power)
- #end
-
def belongs_to_association?(property)
reflection = reflect_on_association(property)
reflection && reflection.macro == :belongs_to
@@ -21,13 +21,17 @@ def validate_record(record)
unless allow_blank? && value.blank?
begin
assignable_values = assignable_values(record)
- assignable_values.include?(value) or record.errors.add(property, I18n.t('errors.messages.inclusion'))
+ assignable_values.include?(value) or record.errors.add(property, not_included_error_message)
rescue DelegateUnavailable
# if the delegate is unavailable, the validation is skipped
end
end
end
+ def not_included_error_message
+ I18n.t('errors.messages.inclusion', :default => 'is not included in the list')
+ end
+
def assignable_values(record)
assignable_values = []
old_value = previously_saved_value(record)
@@ -85,6 +89,7 @@ def enhance_model(&block)
end
def setup_default
+ ensure_after_initialize_callback_enabled
@default = options[:default]
restriction = self
enhance_model do
@@ -96,6 +101,17 @@ def setup_default
end
end
+ def ensure_after_initialize_callback_enabled
+ if Rails.version < '3'
+ enhance_model do
+ # Old ActiveRecord version only call after_initialize callbacks only if this method is defined in a class.
+ unless method_defined?(:after_initialize)
+ define_method(:after_initialize) {}
+ end
+ end
+ end
+ end
+
def setup_validation
restriction = self
enhance_model do
@@ -1,3 +1,3 @@
module AssignableValues
- VERSION = '0.2.0'
+ VERSION = '0.2.1'
end
View
@@ -0,0 +1,8 @@
+source 'http://rubygems.org'
+
+gem 'sqlite3'
+gem 'rails', '=2.3.10'
+gem 'rspec', '<2'
+gem 'rspec-rails', '<2'
+gem 'ruby-debug'
+gem 'assignable_values', :path => '../..'
View
@@ -0,0 +1,11 @@
+require 'rake'
+require 'spec/rake/spectask'
+
+desc 'Default: Run all specs for a specific rails version.'
+task :default => :spec
+
+desc "Run all specs for a specific rails version"
+Spec::Rake::SpecTask.new() do |t|
+ t.spec_opts = ['--options', "\"spec.opts\""]
+ t.spec_files = defined?(SPEC) ? SPEC : FileList['**/*_spec.rb', '../shared/**/*_spec.rb']
+end
@@ -0,0 +1,129 @@
+# Allow customization of the rails framework path
+RAILS_FRAMEWORK_ROOT = (ENV['RAILS_FRAMEWORK_ROOT'] || "#{File.dirname(__FILE__)}/../../../../../../vendor/rails") unless defined?(RAILS_FRAMEWORK_ROOT)
+
+# Don't change this file!
+# Configure your app in config/environment.rb and config/environments/*.rb
+
+RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
+
+module Rails
+ class << self
+ def boot!
+ unless booted?
+ preinitialize
+ pick_boot.run
+ end
+ end
+
+ def booted?
+ defined? Rails::Initializer
+ end
+
+ def pick_boot
+ (vendor_rails? ? VendorBoot : GemBoot).new
+ end
+
+ def vendor_rails?
+ File.exist?(RAILS_FRAMEWORK_ROOT)
+ end
+
+ def preinitialize
+ load(preinitializer_path) if File.exist?(preinitializer_path)
+ end
+
+ def preinitializer_path
+ "#{RAILS_ROOT}/config/preinitializer.rb"
+ end
+ end
+
+ class Boot
+ def run
+ load_initializer
+ Rails::Initializer.run(:set_load_path)
+ end
+ end
+
+ class VendorBoot < Boot
+ def load_initializer
+ require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/initializer"
+ Rails::Initializer.run(:install_gem_spec_stubs)
+ end
+ end
+
+ class GemBoot < Boot
+ def load_initializer
+ self.class.load_rubygems
+ load_rails_gem
+ require 'initializer'
+ end
+
+ def load_rails_gem
+ if version = self.class.gem_version
+ gem 'rails', version
+ else
+ gem 'rails'
+ end
+ rescue Gem::LoadError => load_error
+ $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.)
+ exit 1
+ end
+
+ class << self
+ def rubygems_version
+ Gem::RubyGemsVersion rescue nil
+ end
+
+ def gem_version
+ if defined? RAILS_GEM_VERSION
+ RAILS_GEM_VERSION
+ elsif ENV.include?('RAILS_GEM_VERSION')
+ ENV['RAILS_GEM_VERSION']
+ else
+ parse_gem_version(read_environment_rb)
+ end
+ end
+
+ def load_rubygems
+ require 'rubygems'
+ min_version = '1.1.1'
+ unless rubygems_version >= min_version
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
+ exit 1
+ end
+
+ rescue LoadError
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
+ exit 1
+ end
+
+ def parse_gem_version(text)
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
+ end
+
+ private
+ def read_environment_rb
+ environment_rb = "#{RAILS_ROOT}/config/environment.rb"
+ environment_rb = "#{HELPER_RAILS_ROOT}/config/environment.rb" unless File.exists?(environment_rb)
+ File.read(environment_rb)
+ end
+ end
+ end
+end
+
+
+class Rails::Boot
+ def run
+ load_initializer
+
+ Rails::Initializer.class_eval do
+ def load_gems
+ @bundler_loaded ||= Bundler.require :default, Rails.env
+ end
+ end
+
+ Rails::Initializer.run(:set_load_path)
+ end
+end
+
+# All that for this:
+Rails.boot!
@@ -1,4 +1,4 @@
-in_memory:
+test:
adapter: sqlite3
database: ":memory:"
verbosity: quiet
@@ -0,0 +1,14 @@
+require File.join(File.dirname(__FILE__), 'boot')
+
+Rails::Initializer.run do |config|
+ config.cache_classes = false
+ config.whiny_nils = true
+ config.action_controller.session = { :key => "_myapp_session", :secret => "gwirofjweroijger8924rt2zfwehfuiwehb1378rifowenfoqwphf23" }
+ #config.plugin_locators.unshift(
+ # Class.new(Rails::Plugin::Locator) do
+ # def plugins
+ # [Rails::Plugin.new(File.expand_path('.'))]
+ # end
+ # end
+ #) unless defined?(PluginTestHelper::PluginLocator)
+end
@@ -0,0 +1,28 @@
+# Settings specified here will take precedence over those in config/environment.rb
+
+# The test environment is used exclusively to run your application's
+# test suite. You never need to work with it otherwise. Remember that
+# your test database is "scratch space" for the test suite and is wiped
+# and recreated between test runs. Don't rely on the data there!
+config.cache_classes = true
+
+# Log error messages when you accidentally call methods on nil.
+config.whiny_nils = true
+
+# Show full error reports and disable caching
+config.action_controller.consider_all_requests_local = true
+config.action_controller.perform_caching = false
+config.action_view.cache_template_loading = true
+
+# Disable request forgery protection in test environment
+config.action_controller.allow_forgery_protection = false
+
+# Tell Action Mailer not to deliver emails to the real world.
+# The :test delivery method accumulates sent emails in the
+# ActionMailer::Base.deliveries array.
+config.action_mailer.delivery_method = :test
+
+# Use SQL instead of Active Record's schema dumper when creating the test database.
+# This is necessary if your schema can't be completely dumped by the schema dumper,
+# like if you have constraints or database-specific column types
+# config.active_record.schema_format = :sql
@@ -0,0 +1,20 @@
+begin
+ require "rubygems"
+ require "bundler"
+rescue LoadError
+ raise "Could not load the bundler gem. Install it with `gem install bundler`."
+end
+
+if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.24")
+ raise RuntimeError, "Your bundler version is too old for Rails 2.3." +
+ "Run `gem install bundler` to upgrade."
+end
+
+begin
+ # Set up load paths for all bundled gems
+ ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
+ Bundler.setup
+rescue Bundler::GemNotFound
+ raise RuntimeError, "Bundler couldn't find some gems." +
+ "Did you run `bundle install`?"
+end
@@ -0,0 +1,4 @@
+ActionController::Routing::Routes.draw do |map|
+ map.connect ':controller/:action/:id'
+ map.connect ':controller/:action/:id.:format'
+end
@@ -0,0 +1,4 @@
+# Loads fixtures into the database when running the test app via the console
+(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(Rails.root, '../fixtures/*.{yml,csv}'))).each do |fixture_file|
+ Fixtures.create_fixtures(File.join(Rails.root, '../fixtures'), File.basename(fixture_file, '.*'))
+end
@@ -0,0 +1 @@
+*.log
@@ -0,0 +1,7 @@
+irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
+libs = " -r irb/completion"
+libs << " -r test/test_helper"
+libs << " -r console_app"
+libs << " -r console_with_helpers"
+libs << " -r console_with_fixtures"
+exec "#{irb} #{libs} --simple-prompt"
File renamed without changes.
View
@@ -0,0 +1,4 @@
+--colour
+--format progress
+--loadby mtime
+--reverse
@@ -0,0 +1,24 @@
+$: << File.join(File.dirname(__FILE__), "/../lib" )
+
+# Set the default environment to sqlite3's in_memory database
+ENV['RAILS_ENV'] ||= 'test'
+
+# Load the Rails environment and testing framework
+require "#{File.dirname(__FILE__)}/app_root/config/environment"
+require 'spec/rails'
+
+# Load dependencies
+# require 'has_defaults'
+
+# Require support code
+# Dir["../shared/support/**/*.rb"].each {|f| require f}
+
+# Run the migrations
+print "\033[30m" # dark gray text
+ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
+print "\033[0m"
+
+Spec::Runner.configure do |config|
+ config.use_transactional_fixtures = true
+ config.use_instantiated_fixtures = false
+end
View
@@ -0,0 +1,2 @@
+--colour
+--format progress
Oops, something went wrong.

0 comments on commit 4e1e233

Please sign in to comment.