Skip to content
Browse files

get tests passing in rails 3.0.10

  • Loading branch information...
1 parent 59b9420 commit 514220fc59f2137a70327bb4616cbd022b2fceb7 @cainlevy committed Dec 2, 2011
Showing with 242 additions and 83 deletions.
  1. +11 −31 Rakefile
  2. +7 −1 lib/components.rb
  3. +22 −14 lib/components/base.rb
  4. +2 −2 lib/components/caching.rb
  5. +0 −2 lib/components/view.rb
  6. +0 −3 rails/init.rb
  7. +0 −4 test/action_controller_test.rb
  8. +9 −4 test/components_test.rb
  9. +6 −0 test/r3/.gitignore
  10. +6 −0 test/r3/Gemfile
  11. +84 −0 test/r3/Gemfile.lock
  12. 0 test/{ → r3}/app/components/child/one.erb
  13. 0 test/{ → r3}/app/components/child_component.rb
  14. 0 test/{ → r3}/app/components/hello_world/bolded.erb
  15. 0 test/{ → r3}/app/components/hello_world/say_it_with_help.erb
  16. 0 test/{ → r3}/app/components/hello_world_component.rb
  17. 0 test/{ → r3}/app/components/namespace/hello_world_component.rb
  18. 0 test/{ → r3}/app/components/parent/one.erb
  19. 0 test/{ → r3}/app/components/parent/two.erb
  20. 0 test/{ → r3}/app/components/parent_component.rb
  21. +1 −1 test/{ → r3}/app/components/rich_view/form.erb
  22. 0 test/{ → r3}/app/components/rich_view/linker.erb
  23. 0 test/{ → r3}/app/components/rich_view/urler.erb
  24. 0 test/{ → r3}/app/components/rich_view_component.rb
  25. 0 test/{ → r3}/app/controllers/application_controller.rb
  26. 0 test/{ → r3}/app/controllers/hello_world_controller.rb
  27. +4 −0 test/r3/config.ru
  28. +12 −0 test/r3/config/application.rb
  29. +6 −0 test/r3/config/boot.rb
  30. +22 −0 test/r3/config/database.yml
  31. +5 −0 test/r3/config/environment.rb
  32. +35 −0 test/r3/config/environments/test.rb
  33. +3 −0 test/r3/config/routes.rb
  34. 0 test/r3/public/.gitkeep
  35. +7 −21 test/test_helper.rb
View
42 Rakefile
@@ -1,42 +1,22 @@
-require 'rubygems'
require 'rake'
+require 'rake/testtask'
+require 'rdoc/task'
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gem|
- gem.name = "components"
- gem.summary = %Q{TODO}
- gem.email = "alex@upstream-berlin.com"
- gem.homepage = "http://github.com/langalex/components"
- gem.authors = ["Lance Ivy", "Alexander Lang"]
-
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
- end
-rescue LoadError
- puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
-end
+desc 'Default: run unit tests.'
+task :default => :test
-require 'rake/testtask'
+desc 'Test the components gem.'
Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
+ test.libs << 'lib'
test.pattern = 'test/**/*_test.rb'
- test.verbose = false
+ test.verbose = true
end
-task :default => :test
-
-require 'rake/rdoctask'
+desc 'Generate documentation for the components gem.'
Rake::RDocTask.new do |rdoc|
- if File.exist?('VERSION.yml')
- config = YAML.load(File.read('VERSION.yml'))
- version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
- else
- version = ""
- end
-
rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "components #{version}"
+ rdoc.title = "components"
+ rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
-end
-
+end
View
8 lib/components.rb
@@ -1,7 +1,13 @@
+require 'components/rails'
+
module Components #:nodoc:
+ autoload :Base, 'components/base'
+ autoload :View, 'components/view'
+ autoload :Caching, 'components/caching'
+
def self.render(name, component_args = [], options = {})
klass, method = name.split(/\/(?=[^\/]*$)/)
-
+
component = (klass + "_component").camelcase.constantize.new
component.form_authenticity_token = options[:form_authenticity_token]
merge_standard_component_options!(component_args, options[:standard_component_options], component.method(method).arity)
View
36 lib/components/base.rb
@@ -1,6 +1,7 @@
module Components
class Base
- include ::ActionController::UrlWriter
+ include Rails.application.routes.url_helpers
+ include ::ActiveSupport::Configurable
include ::ActionController::Helpers
include ::Components::Caching
@@ -17,7 +18,7 @@ class << self
# "vendor/plugins/scaffolding/components/" to this array.
def view_paths
if read_inheritable_attribute(:view_paths).nil?
- default_path = File.join(RAILS_ROOT, 'app', 'components')
+ default_path = Rails.root.join('app', 'components').to_s
write_inheritable_attribute(:view_paths, [default_path])
end
read_inheritable_attribute(:view_paths)
@@ -31,7 +32,7 @@ def path #:nodoc:
# must be public for access from ActionView
def logger #:nodoc:
- RAILS_DEFAULT_LOGGER
+ Rails.logger
end
protected
@@ -77,17 +78,23 @@ def render(file = nil)
end
end
- template.render(:file => "#{component.path}/#{file}")
+ view_context.render(:file => "#{component.path}/#{file}")
end
- # creates and returns a view object for rendering the current action.
- # note that this freezes knowledge of view_paths and assigns.
- def template #:nodoc:
- if @template.nil?
- @template = Components::View.new(self.class.view_paths, assigns_for_view, self)
- @template.extend self.class.master_helper_module
+ def view_context #:nodoc:
+ self.class.view_context_class.new(self.class.view_paths, assigns_for_view, self)
+ end
+
+ class << self
+ def view_context_class
+ @view_context_class ||= begin
+ controller = self
+ Class.new(Components::View) do
+ include controller._routes.url_helpers
+ include controller._helpers
+ end
+ end
end
- @template
end
# should return a hash of all instance variables to assign to the view
@@ -102,11 +109,12 @@ def assigns_for_view #:nodoc:
def unassignable_instance_variables #:nodoc:
%w(@template @assigns_for_view)
end
-
+
private
-
+
+ # TODO: get rid of this inefficiency
def exists?(name)
- template.render(:file => name)
+ view_context.render(:file => name)
rescue ::ActionView::MissingTemplate
false
end
View
4 lib/components/caching.rb
@@ -107,7 +107,7 @@ def #{action}_with_caching(*args)
end
def cache_store #:nodoc:
- @cache_store ||= ActionController::Base.cache_store
+ ActionController::Base.cache_store
end
end
@@ -134,7 +134,7 @@ def with_caching(action, args, &block) #:nodoc:
end
def read_fragment(key, cache_options = nil) #:nodoc:
- returning self.class.cache_store.read(key, cache_options) do |content|
+ self.class.cache_store.read(key, cache_options).tap do |content|
logger.debug "Component Cache hit: #{key}" unless content.blank?
end
end
View
2 lib/components/view.rb
@@ -1,5 +1,3 @@
class Components::View < ActionView::Base
delegate :protect_against_forgery?, :form_authenticity_token, :to => :controller
-
- # TODO: rendering from a component view should be restricted to *just* rendering other components.
end
View
3 rails/init.rb
@@ -1,3 +0,0 @@
-ActiveSupport::Dependencies.load_paths << RAILS_ROOT + '/app/components'
-
-Components # trigger load
View
4 test/action_controller_test.rb
@@ -1,10 +1,6 @@
require File.dirname(__FILE__) + '/test_helper'
class ActionControllerTest < ActionController::TestCase
- ::ActionController::Routing::Routes.draw do |map|
- map.connect "/:controller/:action/:id"
- end
-
def setup
@controller = HelloWorldController.new
@request = ActionController::TestRequest.new
View
13 test/components_test.rb
@@ -26,11 +26,16 @@ def test_links_in_views
assert_select rendered, "a[href=http://example.com]"
end
- def test_form_in_views
+ def test_links_in_views
+ ActionController::Base.allow_forgery_protection, old_allow_forgery_protection = true, ActionController::Base.allow_forgery_protection
ActionController::Base.request_forgery_protection_token = :authenticity_token
- rendered = Components.render("rich_view/form", [], :form_authenticity_token => "bluetlecrashit")
- assert_select rendered, "form"
- assert_select rendered, "input[type=hidden][name=authenticity_token][value=bluetlecrashit]"
+ begin
+ rendered = Components.render("rich_view/form", [], :form_authenticity_token => "bluetlecrashit")
+ assert_select rendered, "form"
+ assert_select rendered, "input[type=hidden][name=authenticity_token][value=bluetlecrashit]"
+ ensure
+ ActionController::Base.allow_forgery_protection = old_allow_forgery_protection
+ end
end
def test_url_for_in_views
View
6 test/r3/.gitignore
@@ -0,0 +1,6 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/
+public/stylesheets/presenting
+public/javascripts/presenting
View
6 test/r3/Gemfile
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.10'
+gem 'sqlite3'
+
+gem 'components', :path => File.dirname(__FILE__) + '/../../'
View
84 test/r3/Gemfile.lock
@@ -0,0 +1,84 @@
+PATH
+ remote: /Users/cainlevy/Code/components
+ specs:
+ components (0.0.2)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.10)
+ actionpack (= 3.0.10)
+ mail (~> 2.2.19)
+ actionpack (3.0.10)
+ activemodel (= 3.0.10)
+ activesupport (= 3.0.10)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.5.0)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.14)
+ rack-test (~> 0.5.7)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.10)
+ activesupport (= 3.0.10)
+ builder (~> 2.1.2)
+ i18n (~> 0.5.0)
+ activerecord (3.0.10)
+ activemodel (= 3.0.10)
+ activesupport (= 3.0.10)
+ arel (~> 2.0.10)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.10)
+ activemodel (= 3.0.10)
+ activesupport (= 3.0.10)
+ activesupport (3.0.10)
+ arel (2.0.10)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.5.0)
+ json (1.6.1)
+ mail (2.2.19)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.17.2)
+ polyglot (0.3.3)
+ rack (1.2.4)
+ rack-mount (0.6.14)
+ rack (>= 1.0.0)
+ rack-test (0.5.7)
+ rack (>= 1.0)
+ rails (3.0.10)
+ actionmailer (= 3.0.10)
+ actionpack (= 3.0.10)
+ activerecord (= 3.0.10)
+ activeresource (= 3.0.10)
+ activesupport (= 3.0.10)
+ bundler (~> 1.0)
+ railties (= 3.0.10)
+ railties (3.0.10)
+ actionpack (= 3.0.10)
+ activesupport (= 3.0.10)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.4)
+ rake (0.9.2.2)
+ rdoc (3.11)
+ json (~> 1.4)
+ sqlite3 (1.3.4)
+ thor (0.14.6)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.31)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ components!
+ rails (= 3.0.10)
+ sqlite3
View
0 test/app/components/child/one.erb → test/r3/app/components/child/one.erb
File renamed without changes.
View
0 test/app/components/child_component.rb → test/r3/app/components/child_component.rb
File renamed without changes.
View
0 test/app/components/hello_world/bolded.erb → .../r3/app/components/hello_world/bolded.erb
File renamed without changes.
View
0 ...mponents/hello_world/say_it_with_help.erb → ...mponents/hello_world/say_it_with_help.erb
File renamed without changes.
View
0 test/app/components/hello_world_component.rb → ...3/app/components/hello_world_component.rb
File renamed without changes.
View
0 ...onents/namespace/hello_world_component.rb → ...onents/namespace/hello_world_component.rb
File renamed without changes.
View
0 test/app/components/parent/one.erb → test/r3/app/components/parent/one.erb
File renamed without changes.
View
0 test/app/components/parent/two.erb → test/r3/app/components/parent/two.erb
File renamed without changes.
View
0 test/app/components/parent_component.rb → test/r3/app/components/parent_component.rb
File renamed without changes.
View
2 test/app/components/rich_view/form.erb → test/r3/app/components/rich_view/form.erb
@@ -1,3 +1,3 @@
-<% form_tag "foo" do %>
+<%= form_tag "foo" do %>
It's important to test request forgery protection.
<% end %>
View
0 test/app/components/rich_view/linker.erb → test/r3/app/components/rich_view/linker.erb
File renamed without changes.
View
0 test/app/components/rich_view/urler.erb → test/r3/app/components/rich_view/urler.erb
File renamed without changes.
View
0 test/app/components/rich_view_component.rb → .../r3/app/components/rich_view_component.rb
File renamed without changes.
View
0 ...app/controllers/application_controller.rb → ...app/controllers/application_controller.rb
File renamed without changes.
View
0 ...app/controllers/hello_world_controller.rb → ...app/controllers/hello_world_controller.rb
File renamed without changes.
View
4 test/r3/config.ru
@@ -0,0 +1,4 @@
+# This file is used by Rack-based servers to start the application.
+
+require ::File.expand_path('../config/environment', __FILE__)
+run R3::Application
View
12 test/r3/config/application.rb
@@ -0,0 +1,12 @@
+require File.expand_path('../boot', __FILE__)
+
+require 'rails/all'
+
+Bundler.require(:default, Rails.env) if defined?(Bundler)
+
+module R3
+ class Application < Rails::Application
+ config.encoding = "utf-8"
+ config.filter_parameters += [:password]
+ end
+end
View
6 test/r3/config/boot.rb
@@ -0,0 +1,6 @@
+require 'rubygems'
+
+# Set up gems listed in the Gemfile.
+ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
+
+require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
View
22 test/r3/config/database.yml
@@ -0,0 +1,22 @@
+# SQLite version 3.x
+# gem install sqlite3
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+ pool: 5
+ timeout: 5000
+
+# Warning: The database defined as "test" will be erased and
+# re-generated from your development database when you run "rake".
+# Do not set this db to the same as development or production.
+test:
+ adapter: sqlite3
+ database: db/test.sqlite3
+ pool: 5
+ timeout: 5000
+
+production:
+ adapter: sqlite3
+ database: db/production.sqlite3
+ pool: 5
+ timeout: 5000
View
5 test/r3/config/environment.rb
@@ -0,0 +1,5 @@
+# Load the rails application
+require File.expand_path('../application', __FILE__)
+
+# Initialize the rails application
+R3::Application.initialize!
View
35 test/r3/config/environments/test.rb
@@ -0,0 +1,35 @@
+R3::Application.configure do
+ # Settings specified here will take precedence over those in config/application.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.consider_all_requests_local = true
+ config.action_controller.perform_caching = false
+
+ # Raise exceptions instead of rendering exception templates
+ config.action_dispatch.show_exceptions = false
+
+ # 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
+
+ # Print deprecation notices to the stderr
+ config.active_support.deprecation = :stderr
+end
View
3 test/r3/config/routes.rb
@@ -0,0 +1,3 @@
+R3::Application.routes.draw do
+ match ':controller(/:action(/:id))'
+end
View
0 test/r3/public/.gitkeep
No changes.
View
28 test/test_helper.rb
@@ -1,24 +1,10 @@
-# fake the rails root
-RAILS_ROOT = File.dirname(__FILE__)
-
-# require support libraries
-require 'test/unit'
+# bootstrap
require 'rubygems'
-gem 'rails', '2.3.2'
-require 'active_support'
-require 'action_controller'
-require 'action_controller/test_process' # for the assertions
-require 'action_view'
-require 'active_record'
-require 'logger'
+require 'test/unit'
require 'mocha'
-RAILS_DEFAULT_LOGGER = Logger.new(File.dirname(__FILE__) + '/debug.log')
-
-%w(../lib app/controllers).each do |load_path|
- ActiveSupport::Dependencies.load_paths << File.dirname(__FILE__) + "/" + load_path
-end
-
-require File.dirname(__FILE__) + '/../init'
-
-ActionController::Base.cache_store = :memory_store
+# load the test app, which will load gems
+ENV['RAILS_ENV'] = 'test'
+require File.join(File.dirname(__FILE__), 'r3', 'config', 'environment.rb')
+require 'rails/test_help'
+Rails.backtrace_cleaner.remove_silencers!

0 comments on commit 514220f

Please sign in to comment.
Something went wrong with that request. Please try again.