Initial import
This commit is contained in:
22
vendor/plugins/shoulda/MIT-LICENSE
vendored
Normal file
22
vendor/plugins/shoulda/MIT-LICENSE
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
Copyright (c) 2007, Tammer Saleh, Thoughtbot, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
123
vendor/plugins/shoulda/README
vendored
Normal file
123
vendor/plugins/shoulda/README
vendored
Normal file
@@ -0,0 +1,123 @@
|
||||
= Shoulda - Making tests easy on the fingers and eyes
|
||||
|
||||
Shoulda makes it easy to write elegant, understandable, and maintainable tests. Shoulda consists of test macros, assertions, and helpers added on to the Test::Unit framework. It's fully compatible with your existing tests, and requires no retooling to use.
|
||||
|
||||
Helpers:: #context and #should give you rSpec like test blocks.
|
||||
In addition, you get nested contexts and a much more readable syntax.
|
||||
Macros:: Generate hundreds of lines of Controller and ActiveRecord tests with these powerful macros.
|
||||
They get you started quickly, and can help you ensure that your application is conforming to best practices.
|
||||
Assertions:: Many common rails testing idioms have been distilled into a set of useful assertions.
|
||||
|
||||
= Usage
|
||||
|
||||
=== Context Helpers (ThoughtBot::Shoulda::Context)
|
||||
|
||||
Stop killing your fingers with all of those underscores... Name your tests with plain sentences!
|
||||
|
||||
class UserTest << Test::Unit::TestCase
|
||||
context "A User instance" do
|
||||
setup do
|
||||
@user = User.find(:first)
|
||||
end
|
||||
|
||||
should "return its full name"
|
||||
assert_equal 'John Doe', @user.full_name
|
||||
end
|
||||
|
||||
context "with a profile" do
|
||||
setup do
|
||||
@user.profile = Profile.find(:first)
|
||||
end
|
||||
|
||||
should "return true when sent #has_profile?"
|
||||
assert @user.has_profile?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Produces the following test methods:
|
||||
|
||||
"test: A User instance should return its full name."
|
||||
"test: A User instance with a profile should return true when sent #has_profile?."
|
||||
|
||||
So readable!
|
||||
|
||||
=== ActiveRecord Tests (ThoughtBot::Shoulda::ActiveRecord)
|
||||
|
||||
Quick macro tests for your ActiveRecord associations and validations:
|
||||
|
||||
class PostTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
|
||||
should_belong_to :user
|
||||
should_have_many :tags, :through => :taggings
|
||||
|
||||
should_require_unique_attributes :title
|
||||
should_require_attributes :body, :message => /wtf/
|
||||
should_require_attributes :title
|
||||
should_only_allow_numeric_values_for :user_id
|
||||
end
|
||||
|
||||
class UserTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
|
||||
should_have_many :posts
|
||||
|
||||
should_not_allow_values_for :email, "blah", "b lah"
|
||||
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
|
||||
should_ensure_length_in_range :email, 1..100
|
||||
should_ensure_value_in_range :age, 1..100
|
||||
should_protect_attributes :password
|
||||
end
|
||||
|
||||
Makes TDD so much easier.
|
||||
|
||||
=== Controller Tests (ThoughtBot::Shoulda::Controller::ClassMethods)
|
||||
|
||||
Macros to test the most common controller patterns...
|
||||
|
||||
context "on GET to :show for first record" do
|
||||
setup do
|
||||
get :show, :id => 1
|
||||
end
|
||||
|
||||
should_assign_to :user
|
||||
should_respond_with :success
|
||||
should_render_template :show
|
||||
should_not_set_the_flash
|
||||
|
||||
should "do something else really cool" do
|
||||
assert_equal 1, assigns(:user).id
|
||||
end
|
||||
end
|
||||
|
||||
Test entire controllers in a few lines...
|
||||
|
||||
class PostsControllerTest < Test::Unit::TestCase
|
||||
should_be_restful do |resource|
|
||||
resource.parent = :user
|
||||
|
||||
resource.create.params = { :title => "first post", :body => 'blah blah blah'}
|
||||
resource.update.params = { :title => "changed" }
|
||||
end
|
||||
end
|
||||
|
||||
should_be_restful generates 40 tests on the fly, for both html and xml requests.
|
||||
|
||||
=== Helpful Assertions (ThoughtBot::Shoulda::General)
|
||||
|
||||
More to come here, but have fun with what's there.
|
||||
|
||||
load_all_fixtures
|
||||
assert_same_elements([:a, :b, :c], [:c, :a, :b])
|
||||
assert_contains(['a', '1'], /\d/)
|
||||
assert_contains(['a', '1'], 'a')
|
||||
|
||||
= Credits
|
||||
|
||||
Shoulda is maintained by {Tammer Saleh}[mailto:tsaleh@thoughtbot.com], and is funded by Thoughtbot[http://www.thoughtbot.com], inc.
|
||||
|
||||
= License
|
||||
|
||||
Shoulda is Copyright © 2006-2007 Tammer Saleh, Thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
|
||||
32
vendor/plugins/shoulda/Rakefile
vendored
Normal file
32
vendor/plugins/shoulda/Rakefile
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
require 'rake'
|
||||
require 'rake/testtask'
|
||||
require 'rake/rdoctask'
|
||||
|
||||
# Test::Unit::UI::VERBOSE
|
||||
|
||||
Rake::TestTask.new do |t|
|
||||
t.libs << 'lib'
|
||||
t.pattern = 'test/{unit,functional,other}/**/*_test.rb'
|
||||
t.verbose = false
|
||||
end
|
||||
|
||||
Rake::RDocTask.new { |rdoc|
|
||||
rdoc.rdoc_dir = 'doc'
|
||||
rdoc.title = "Shoulda -- Making tests easy on the fingers and eyes"
|
||||
rdoc.options << '--line-numbers' << '--inline-source'
|
||||
rdoc.template = "#{ENV['template']}.rb" if ENV['template']
|
||||
rdoc.rdoc_files.include('README', 'lib/**/*.rb')
|
||||
}
|
||||
|
||||
desc 'Update documentation on website'
|
||||
task :sync_docs => 'rdoc' do
|
||||
`rsync -ave ssh doc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/shoulda`
|
||||
end
|
||||
|
||||
desc 'Default: run tests.'
|
||||
task :default => ['test']
|
||||
|
||||
Dir['tasks/*.rake'].each do |f|
|
||||
load f
|
||||
end
|
||||
|
||||
40
vendor/plugins/shoulda/bin/convert_to_should_syntax
vendored
Executable file
40
vendor/plugins/shoulda/bin/convert_to_should_syntax
vendored
Executable file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env ruby
|
||||
require 'fileutils'
|
||||
|
||||
def usage(msg = nil)
|
||||
puts "Error: #{msg}" if msg
|
||||
puts if msg
|
||||
puts "Usage: #{File.basename(__FILE__)} normal_test_file.rb"
|
||||
puts
|
||||
puts "Will convert an existing test file with names like "
|
||||
puts
|
||||
puts " def test_should_do_stuff"
|
||||
puts " ..."
|
||||
puts " end"
|
||||
puts
|
||||
puts "to one using the new syntax: "
|
||||
puts
|
||||
puts " should \"be super cool\" do"
|
||||
puts " ..."
|
||||
puts " end"
|
||||
puts
|
||||
puts "A copy of the old file will be left under /tmp/ in case this script just seriously screws up"
|
||||
puts
|
||||
exit (msg ? 2 : 0)
|
||||
end
|
||||
|
||||
usage("Wrong number of arguments.") unless ARGV.size == 1
|
||||
usage("This system doesn't have a /tmp directory. wtf?") unless File.directory?('/tmp')
|
||||
|
||||
file = ARGV.shift
|
||||
tmpfile = "/tmp/#{File.basename(file)}"
|
||||
usage("File '#{file}' doesn't exist") unless File.exists?(file)
|
||||
|
||||
FileUtils.cp(file, tmpfile)
|
||||
contents = File.read(tmpfile)
|
||||
contents.gsub!(/def test_should_(.*)\s*$/, 'should "\1" do')
|
||||
contents.gsub!(/def test_(.*)\s*$/, 'should "RENAME ME: test \1" do')
|
||||
contents.gsub!(/should ".*" do$/) {|line| line.tr!('_', ' ')}
|
||||
File.open(file, 'w') { |f| f.write(contents) }
|
||||
|
||||
puts "File '#{file}' has been converted to 'should' syntax. Old version has been stored in '#{tmpfile}'"
|
||||
1
vendor/plugins/shoulda/cruise_control.rb
vendored
Symbolic link
1
vendor/plugins/shoulda/cruise_control.rb
vendored
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/builder/public_repo_cruise_config.rb
|
||||
3
vendor/plugins/shoulda/init.rb
vendored
Normal file
3
vendor/plugins/shoulda/init.rb
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
require 'rubygems'
|
||||
require 'active_support'
|
||||
require 'shoulda'
|
||||
45
vendor/plugins/shoulda/lib/shoulda.rb
vendored
Normal file
45
vendor/plugins/shoulda/lib/shoulda.rb
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
require 'yaml'
|
||||
require 'shoulda/private_helpers'
|
||||
require 'shoulda/general'
|
||||
require 'shoulda/gem/shoulda'
|
||||
require 'shoulda/active_record_helpers'
|
||||
require 'shoulda/controller_tests/controller_tests.rb'
|
||||
|
||||
shoulda_options = {}
|
||||
|
||||
possible_config_paths = []
|
||||
possible_config_paths << File.join(ENV["HOME"], ".shoulda.conf") if ENV["HOME"]
|
||||
possible_config_paths << "shoulda.conf"
|
||||
possible_config_paths << File.join("test", "shoulda.conf")
|
||||
possible_config_paths << File.join(RAILS_ROOT, "test", "shoulda.conf") if defined?(RAILS_ROOT)
|
||||
|
||||
possible_config_paths.each do |config_file|
|
||||
if File.exists? config_file
|
||||
shoulda_options = YAML.load_file(config_file).symbolize_keys
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
require 'shoulda/color' if shoulda_options[:color]
|
||||
|
||||
module Test # :nodoc: all
|
||||
module Unit
|
||||
class TestCase
|
||||
|
||||
include ThoughtBot::Shoulda::Controller
|
||||
include ThoughtBot::Shoulda::General
|
||||
|
||||
class << self
|
||||
include ThoughtBot::Shoulda::ActiveRecord
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module ActionController #:nodoc: all
|
||||
module Integration
|
||||
class Session
|
||||
include ThoughtBot::Shoulda::General
|
||||
end
|
||||
end
|
||||
end
|
||||
446
vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb
vendored
Normal file
446
vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb
vendored
Normal file
@@ -0,0 +1,446 @@
|
||||
module ThoughtBot # :nodoc:
|
||||
module Shoulda # :nodoc:
|
||||
# = Macro test helpers for your active record models
|
||||
#
|
||||
# These helpers will test most of the validations and associations for your ActiveRecord models.
|
||||
#
|
||||
# class UserTest < Test::Unit::TestCase
|
||||
# should_require_attributes :name, :phone_number
|
||||
# should_not_allow_values_for :phone_number, "abcd", "1234"
|
||||
# should_allow_values_for :phone_number, "(123) 456-7890"
|
||||
#
|
||||
# should_protect_attributes :password
|
||||
#
|
||||
# should_have_one :profile
|
||||
# should_have_many :dogs
|
||||
# should_have_many :messes, :through => :dogs
|
||||
# should_belong_to :lover
|
||||
# end
|
||||
#
|
||||
# For all of these helpers, the last parameter may be a hash of options.
|
||||
#
|
||||
module ActiveRecord
|
||||
# Ensures that the model cannot be saved if one of the attributes listed is not present.
|
||||
# Requires an existing record.
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/blank/</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_require_attributes :name, :phone_number
|
||||
#
|
||||
def should_require_attributes(*attributes)
|
||||
message = get_options!(attributes, :message)
|
||||
message ||= /blank/
|
||||
klass = model_class
|
||||
|
||||
attributes.each do |attribute|
|
||||
should "require #{attribute} to be set" do
|
||||
object = klass.new
|
||||
assert !object.valid?, "#{klass.name} does not require #{attribute}."
|
||||
assert object.errors.on(attribute), "#{klass.name} does not require #{attribute}."
|
||||
assert_contains(object.errors.on(attribute), message)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensures that the model cannot be saved if one of the attributes listed is not unique.
|
||||
# Requires an existing record
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/taken/</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_require_unique_attributes :keyword, :username
|
||||
#
|
||||
def should_require_unique_attributes(*attributes)
|
||||
message, scope = get_options!(attributes, :message, :scoped_to)
|
||||
message ||= /taken/
|
||||
|
||||
klass = model_class
|
||||
attributes.each do |attribute|
|
||||
attribute = attribute.to_sym
|
||||
should "require unique value for #{attribute}#{" scoped to #{scope}" if scope}" do
|
||||
assert existing = klass.find(:first), "Can't find first #{klass}"
|
||||
object = klass.new
|
||||
|
||||
object.send(:"#{attribute}=", existing.send(attribute))
|
||||
if scope
|
||||
assert_respond_to object, :"#{scope}=", "#{klass.name} doesn't seem to have a #{scope} attribute."
|
||||
object.send(:"#{scope}=", existing.send(scope))
|
||||
end
|
||||
|
||||
assert !object.valid?, "#{klass.name} does not require a unique value for #{attribute}."
|
||||
assert object.errors.on(attribute), "#{klass.name} does not require a unique value for #{attribute}."
|
||||
|
||||
assert_contains(object.errors.on(attribute), message)
|
||||
|
||||
if scope
|
||||
# Now test that the object is valid when changing the scoped attribute
|
||||
# TODO: actually find all values for scope and create a unique one.
|
||||
object.send(:"#{scope}=", existing.send(scope).nil? ? 1 : existing.send(scope).next)
|
||||
object.errors.clear
|
||||
object.valid?
|
||||
assert_does_not_contain(object.errors.on(attribute), message,
|
||||
"after :#{scope} set to #{object.send(scope.to_sym)}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensures that the attribute cannot be set on update
|
||||
# Requires an existing record
|
||||
#
|
||||
# should_protect_attributes :password, :admin_flag
|
||||
#
|
||||
def should_protect_attributes(*attributes)
|
||||
get_options!(attributes)
|
||||
klass = model_class
|
||||
attributes.each do |attribute|
|
||||
attribute = attribute.to_sym
|
||||
should "not allow #{attribute} to be changed by update" do
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
value = object[attribute]
|
||||
# TODO: 1 may not be a valid value for the attribute (due to validations)
|
||||
assert object.update_attributes({ attribute => 1 }),
|
||||
"Cannot update #{klass} with { :#{attribute} => 1 }, #{object.errors.full_messages.to_sentence}"
|
||||
assert object.valid?, "#{klass} isn't valid after changing #{attribute}"
|
||||
assert_equal value, object[attribute], "Was able to change #{klass}##{attribute}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensures that the attribute cannot be set to the given values
|
||||
# Requires an existing record
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/invalid/</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_not_allow_values_for :isbn, "bad 1", "bad 2"
|
||||
#
|
||||
def should_not_allow_values_for(attribute, *bad_values)
|
||||
message = get_options!(bad_values, :message)
|
||||
message ||= /invalid/
|
||||
klass = model_class
|
||||
bad_values.each do |v|
|
||||
should "not allow #{attribute} to be set to \"#{v}\"" do
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send("#{attribute}=", v)
|
||||
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
|
||||
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
|
||||
assert_contains(object.errors.on(attribute), message, "when set to \"#{v}\"")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensures that the attribute can be set to the given values.
|
||||
# Requires an existing record
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/invalid/</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
|
||||
#
|
||||
def should_allow_values_for(attribute, *good_values)
|
||||
message = get_options!(good_values, :message)
|
||||
message ||= /invalid/
|
||||
klass = model_class
|
||||
good_values.each do |v|
|
||||
should "allow #{attribute} to be set to \"#{v}\"" do
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send("#{attribute}=", v)
|
||||
object.save
|
||||
assert_does_not_contain(object.errors.on(attribute), message, "when set to \"#{v}\"")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensures that the length of the attribute is in the given range
|
||||
# Requires an existing record
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/short/</tt>
|
||||
# * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/long/</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_ensure_length_in_range :password, (6..20)
|
||||
#
|
||||
def should_ensure_length_in_range(attribute, range, opts = {})
|
||||
short_message, long_message = get_options!([opts], :short_message, :long_message)
|
||||
short_message ||= /short/
|
||||
long_message ||= /long/
|
||||
|
||||
klass = model_class
|
||||
min_length = range.first
|
||||
max_length = range.last
|
||||
|
||||
if min_length > 0
|
||||
min_value = "x" * (min_length - 1)
|
||||
should "not allow #{attribute} to be less than #{min_length} chars long" do
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send("#{attribute}=", min_value)
|
||||
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{min_value}\""
|
||||
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{min_value}\""
|
||||
assert_contains(object.errors.on(attribute), short_message, "when set to \"#{min_value}\"")
|
||||
end
|
||||
end
|
||||
|
||||
max_value = "x" * (max_length + 1)
|
||||
should "not allow #{attribute} to be more than #{max_length} chars long" do
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send("#{attribute}=", max_value)
|
||||
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{max_value}\""
|
||||
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{max_value}\""
|
||||
assert_contains(object.errors.on(attribute), long_message, "when set to \"#{max_value}\"")
|
||||
end
|
||||
end
|
||||
|
||||
# Ensure that the attribute is in the range specified
|
||||
# Requires an existing record
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/included/</tt>
|
||||
# * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/included/</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_ensure_value_in_range :age, (0..100)
|
||||
#
|
||||
def should_ensure_value_in_range(attribute, range, opts = {})
|
||||
low_message, high_message = get_options!([opts], :low_message, :high_message)
|
||||
low_message ||= /included/
|
||||
high_message ||= /included/
|
||||
|
||||
klass = model_class
|
||||
min = range.first
|
||||
max = range.last
|
||||
|
||||
should "not allow #{attribute} to be less than #{min}" do
|
||||
v = min - 1
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send("#{attribute}=", v)
|
||||
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
|
||||
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
|
||||
assert_contains(object.errors.on(attribute), low_message, "when set to \"#{v}\"")
|
||||
end
|
||||
|
||||
should "not allow #{attribute} to be more than #{max}" do
|
||||
v = max + 1
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send("#{attribute}=", v)
|
||||
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
|
||||
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
|
||||
assert_contains(object.errors.on(attribute), high_message, "when set to \"#{v}\"")
|
||||
end
|
||||
end
|
||||
|
||||
# Ensure that the attribute is numeric
|
||||
# Requires an existing record
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/number/</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_only_allow_numeric_values_for :age
|
||||
#
|
||||
def should_only_allow_numeric_values_for(*attributes)
|
||||
message = get_options!(attributes, :message)
|
||||
message ||= /number/
|
||||
klass = model_class
|
||||
attributes.each do |attribute|
|
||||
attribute = attribute.to_sym
|
||||
should "only allow numeric values for #{attribute}" do
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send(:"#{attribute}=", "abcd")
|
||||
assert !object.valid?, "Instance is still valid"
|
||||
assert_contains(object.errors.on(attribute), message)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensures that the has_many relationship exists.
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:through</tt> - association name for <tt>has_many :through</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_have_many :friends
|
||||
# should_have_many :enemies, :through => :friends
|
||||
#
|
||||
def should_have_many(*associations)
|
||||
through = get_options!(associations, :through)
|
||||
klass = model_class
|
||||
associations.each do |association|
|
||||
name = "have many #{association}"
|
||||
name += " through #{through}" if through
|
||||
should name do
|
||||
reflection = klass.reflect_on_association(association)
|
||||
assert reflection, "#{klass.name} does not have any relationship to #{association}"
|
||||
assert_equal :has_many, reflection.macro
|
||||
|
||||
if through
|
||||
through_reflection = klass.reflect_on_association(through)
|
||||
assert through_reflection, "#{klass.name} does not have any relationship to #{through}"
|
||||
assert_equal(through, reflection.options[:through])
|
||||
end
|
||||
|
||||
unless reflection.options[:through]
|
||||
# This is not a through association, so check for the existence of the foreign key on the other table
|
||||
if reflection.options[:foreign_key]
|
||||
fk = reflection.options[:foreign_key]
|
||||
elsif reflection.options[:as]
|
||||
fk = reflection.options[:as].to_s.foreign_key
|
||||
else
|
||||
fk = klass.name.foreign_key
|
||||
end
|
||||
associated_klass = (reflection.options[:class_name] || association.to_s.classify).constantize
|
||||
assert associated_klass.column_names.include?(fk.to_s), "#{associated_klass.name} does not have a #{fk} foreign key."
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensures that the has_and_belongs_to_many relationship exists.
|
||||
#
|
||||
# should_have_and_belong_to_many :posts, :cars
|
||||
#
|
||||
def should_have_and_belong_to_many(*associations)
|
||||
get_options!(associations)
|
||||
klass = model_class
|
||||
associations.each do |association|
|
||||
should "should have and belong to many #{association}" do
|
||||
assert klass.reflect_on_association(association), "#{klass.name} does not have any relationship to #{association}"
|
||||
assert_equal :has_and_belongs_to_many, klass.reflect_on_association(association).macro
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensure that the has_one relationship exists.
|
||||
#
|
||||
# should_have_one :god # unless hindu
|
||||
#
|
||||
def should_have_one(*associations)
|
||||
get_options!(associations)
|
||||
klass = model_class
|
||||
associations.each do |association|
|
||||
should "have one #{association}" do
|
||||
reflection = klass.reflect_on_association(association)
|
||||
assert reflection, "#{klass.name} does not have any relationship to #{association}"
|
||||
assert_equal :has_one, reflection.macro
|
||||
|
||||
if reflection.options[:foreign_key]
|
||||
fk = reflection.options[:foreign_key]
|
||||
elsif reflection.options[:as]
|
||||
fk = reflection.options[:as].to_s.foreign_key
|
||||
else
|
||||
fk = klass.name.foreign_key
|
||||
end
|
||||
associated_klass = (reflection.options[:class_name] || association.to_s.classify).constantize
|
||||
assert associated_klass.column_names.include?(fk.to_s), "#{associated_klass.name} does not have a #{fk} foreign key."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensure that the belongs_to relationship exists.
|
||||
#
|
||||
# should_belong_to :parent
|
||||
#
|
||||
def should_belong_to(*associations)
|
||||
get_options!(associations)
|
||||
klass = model_class
|
||||
associations.each do |association|
|
||||
should "belong_to #{association}" do
|
||||
reflection = klass.reflect_on_association(association)
|
||||
assert reflection, "#{klass.name} does not have any relationship to #{association}"
|
||||
assert_equal :belongs_to, reflection.macro
|
||||
|
||||
unless reflection.options[:polymorphic]
|
||||
associated_klass = (reflection.options[:class_name] || association.to_s.classify).constantize
|
||||
fk = reflection.options[:foreign_key] || associated_klass.name.foreign_key
|
||||
assert klass.column_names.include?(fk.to_s), "#{klass.name} does not have a #{fk} foreign key."
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensure that the given class methods are defined on the model.
|
||||
#
|
||||
# should_have_class_methods :find, :destroy
|
||||
#
|
||||
def should_have_class_methods(*methods)
|
||||
get_options!(methods)
|
||||
klass = model_class
|
||||
methods.each do |method|
|
||||
should "respond to class method #{method}" do
|
||||
assert_respond_to klass, method, "#{klass.name} does not have class method #{method}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensure that the given instance methods are defined on the model.
|
||||
#
|
||||
# should_have_instance_methods :email, :name, :name=
|
||||
#
|
||||
def should_have_instance_methods(*methods)
|
||||
get_options!(methods)
|
||||
klass = model_class
|
||||
methods.each do |method|
|
||||
should "respond to instance method #{method}" do
|
||||
assert_respond_to klass.new, method, "#{klass.name} does not have instance method #{method}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensure that the given columns are defined on the models backing SQL table.
|
||||
#
|
||||
# should_have_db_columns :id, :email, :name, :created_at
|
||||
#
|
||||
def should_have_db_columns(*columns)
|
||||
column_type = get_options!(columns, :type)
|
||||
klass = model_class
|
||||
columns.each do |name|
|
||||
test_name = "have column #{name}"
|
||||
test_name += " of type #{column_type}" if column_type
|
||||
should test_name do
|
||||
column = klass.columns.detect {|c| c.name == name.to_s }
|
||||
assert column, "#{klass.name} does not have column #{name}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Ensure that the given column is defined on the models backing SQL table. The options are the same as
|
||||
# the instance variables defined on the column definition: :precision, :limit, :default, :null,
|
||||
# :primary, :type, :scale, and :sql_type.
|
||||
#
|
||||
# should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
|
||||
# :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
|
||||
#
|
||||
def should_have_db_column(name, opts = {})
|
||||
klass = model_class
|
||||
test_name = "have column named :#{name}"
|
||||
test_name += " with options " + opts.inspect unless opts.empty?
|
||||
should test_name do
|
||||
column = klass.columns.detect {|c| c.name == name.to_s }
|
||||
assert column, "#{klass.name} does not have column #{name}"
|
||||
opts.each do |k, v|
|
||||
assert_equal column.instance_variable_get("@#{k}").to_s, v.to_s, ":#{name} column on table for #{klass} does not match option :#{k}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
include ThoughtBot::Shoulda::Private
|
||||
end
|
||||
end
|
||||
end
|
||||
77
vendor/plugins/shoulda/lib/shoulda/color.rb
vendored
Normal file
77
vendor/plugins/shoulda/lib/shoulda/color.rb
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
require 'test/unit/ui/console/testrunner'
|
||||
|
||||
# Completely stolen from redgreen gem
|
||||
#
|
||||
# Adds colored output to your tests. Specify <tt>color: true</tt> in
|
||||
# your <tt>~/.shoulda.conf</tt> file to enable.
|
||||
#
|
||||
# *Bug*: for some reason, this adds another line of output to the end of
|
||||
# every rake task, as though there was another (empty) set of tests.
|
||||
# A fix would be most welcome.
|
||||
#
|
||||
module ThoughtBot::Shoulda::Color
|
||||
COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 } # :nodoc:
|
||||
def self.method_missing(color_name, *args) # :nodoc:
|
||||
color(color_name) + args.first + color(:clear)
|
||||
end
|
||||
def self.color(color) # :nodoc:
|
||||
"\e[#{COLORS[color.to_sym]}m"
|
||||
end
|
||||
end
|
||||
|
||||
module Test # :nodoc:
|
||||
module Unit # :nodoc:
|
||||
class TestResult # :nodoc:
|
||||
alias :old_to_s :to_s
|
||||
def to_s
|
||||
if old_to_s =~ /\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors/
|
||||
ThoughtBot::Shoulda::Color.send($1.to_i != 0 || $2.to_i != 0 ? :red : :green, $&)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class AutoRunner # :nodoc:
|
||||
alias :old_initialize :initialize
|
||||
def initialize(standalone)
|
||||
old_initialize(standalone)
|
||||
@runner = proc do |r|
|
||||
Test::Unit::UI::Console::RedGreenTestRunner
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Failure # :nodoc:
|
||||
alias :old_long_display :long_display
|
||||
def long_display
|
||||
# old_long_display.sub('Failure', ThoughtBot::Shoulda::Color.red('Failure'))
|
||||
ThoughtBot::Shoulda::Color.red(old_long_display)
|
||||
end
|
||||
end
|
||||
|
||||
class Error # :nodoc:
|
||||
alias :old_long_display :long_display
|
||||
def long_display
|
||||
# old_long_display.sub('Error', ThoughtBot::Shoulda::Color.yellow('Error'))
|
||||
ThoughtBot::Shoulda::Color.yellow(old_long_display)
|
||||
end
|
||||
end
|
||||
|
||||
module UI # :nodoc:
|
||||
module Console # :nodoc:
|
||||
class RedGreenTestRunner < Test::Unit::UI::Console::TestRunner # :nodoc:
|
||||
def output_single(something, level=NORMAL)
|
||||
return unless (output?(level))
|
||||
something = case something
|
||||
when '.' then ThoughtBot::Shoulda::Color.green('.')
|
||||
when 'F' then ThoughtBot::Shoulda::Color.red("F")
|
||||
when 'E' then ThoughtBot::Shoulda::Color.yellow("E")
|
||||
else something
|
||||
end
|
||||
@io.write(something)
|
||||
@io.flush
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
465
vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb
vendored
Normal file
465
vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb
vendored
Normal file
@@ -0,0 +1,465 @@
|
||||
module ThoughtBot # :nodoc:
|
||||
module Shoulda # :nodoc:
|
||||
module Controller
|
||||
def self.included(other) # :nodoc:
|
||||
other.class_eval do
|
||||
extend ThoughtBot::Shoulda::Controller::ClassMethods
|
||||
include ThoughtBot::Shoulda::Controller::InstanceMethods
|
||||
ThoughtBot::Shoulda::Controller::ClassMethods::VALID_FORMATS.each do |format|
|
||||
include "ThoughtBot::Shoulda::Controller::#{format.to_s.upcase}".constantize
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# = Macro test helpers for your controllers
|
||||
#
|
||||
# By using the macro helpers you can quickly and easily create concise and easy to read test suites.
|
||||
#
|
||||
# This code segment:
|
||||
# context "on GET to :show for first record" do
|
||||
# setup do
|
||||
# get :show, :id => 1
|
||||
# end
|
||||
#
|
||||
# should_assign_to :user
|
||||
# should_respond_with :success
|
||||
# should_render_template :show
|
||||
# should_not_set_the_flash
|
||||
#
|
||||
# should "do something else really cool" do
|
||||
# assert_equal 1, assigns(:user).id
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# Would produce 5 tests for the +show+ action
|
||||
#
|
||||
# Furthermore, the should_be_restful helper will create an entire set of tests which will verify that your
|
||||
# controller responds restfully to a variety of requested formats.
|
||||
module ClassMethods
|
||||
# Formats tested by #should_be_restful. Defaults to [:html, :xml]
|
||||
VALID_FORMATS = Dir.glob(File.join(File.dirname(__FILE__), 'formats', '*.rb')).map { |f| File.basename(f, '.rb') }.map(&:to_sym) # :doc:
|
||||
VALID_FORMATS.each {|f| require "shoulda/controller_tests/formats/#{f}.rb"}
|
||||
|
||||
# Actions tested by #should_be_restful
|
||||
VALID_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy] # :doc:
|
||||
|
||||
# A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller.
|
||||
#
|
||||
# Example:
|
||||
# class UsersControllerTest < Test::Unit::TestCase
|
||||
# load_all_fixtures
|
||||
#
|
||||
# def setup
|
||||
# ...normal setup code...
|
||||
# @user = User.find(:first)
|
||||
# end
|
||||
#
|
||||
# should_be_restful do |resource|
|
||||
# resource.identifier = :id
|
||||
# resource.klass = User
|
||||
# resource.object = :user
|
||||
# resource.parent = []
|
||||
# resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
|
||||
# resource.formats = [:html, :xml]
|
||||
#
|
||||
# resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
|
||||
# resource.update.params = { :name => "sue" }
|
||||
#
|
||||
# resource.create.redirect = "user_url(@user)"
|
||||
# resource.update.redirect = "user_url(@user)"
|
||||
# resource.destroy.redirect = "users_url"
|
||||
#
|
||||
# resource.create.flash = /created/i
|
||||
# resource.update.flash = /updated/i
|
||||
# resource.destroy.flash = /removed/i
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# Whenever possible, the resource attributes will be set to sensible defaults.
|
||||
#
|
||||
class ResourceOptions
|
||||
# Configuration options for the create, update, destroy actions under should_be_restful
|
||||
class ActionOptions
|
||||
# String evaled to get the target of the redirection.
|
||||
# All of the instance variables set by the controller will be available to the
|
||||
# evaled code.
|
||||
#
|
||||
# Example:
|
||||
# resource.create.redirect = "user_url(@user.company, @user)"
|
||||
#
|
||||
# Defaults to a generated url based on the name of the controller, the action, and the resource.parents list.
|
||||
attr_accessor :redirect
|
||||
|
||||
# String or Regexp describing a value expected in the flash. Will match against any flash key.
|
||||
#
|
||||
# Defaults:
|
||||
# destroy:: /removed/
|
||||
# create:: /created/
|
||||
# update:: /updated/
|
||||
attr_accessor :flash
|
||||
|
||||
# Hash describing the params that should be sent in with this action.
|
||||
attr_accessor :params
|
||||
end
|
||||
|
||||
# Configuration options for the denied actions under should_be_restful
|
||||
#
|
||||
# Example:
|
||||
# context "The public" do
|
||||
# setup do
|
||||
# @request.session[:logged_in] = false
|
||||
# end
|
||||
#
|
||||
# should_be_restful do |resource|
|
||||
# resource.parent = :user
|
||||
#
|
||||
# resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
|
||||
# resource.denied.flash = /get outta here/i
|
||||
# resource.denied.redirect = 'new_session_url'
|
||||
# end
|
||||
# end
|
||||
#
|
||||
class DeniedOptions
|
||||
# String evaled to get the target of the redirection.
|
||||
# All of the instance variables set by the controller will be available to the
|
||||
# evaled code.
|
||||
#
|
||||
# Example:
|
||||
# resource.create.redirect = "user_url(@user.company, @user)"
|
||||
attr_accessor :redirect
|
||||
|
||||
# String or Regexp describing a value expected in the flash. Will match against any flash key.
|
||||
#
|
||||
# Example:
|
||||
# resource.create.flash = /created/
|
||||
attr_accessor :flash
|
||||
|
||||
# Actions that should be denied (only used by resource.denied). <i>Note that these actions will
|
||||
# only be tested if they are also listed in +resource.actions+</i>
|
||||
# The special value of :all will deny all of the REST actions.
|
||||
attr_accessor :actions
|
||||
end
|
||||
|
||||
# Name of key in params that references the primary key.
|
||||
# Will almost always be :id (default), unless you are using a plugin or have patched rails.
|
||||
attr_accessor :identifier
|
||||
|
||||
# Name of the ActiveRecord class this resource is responsible for. Automatically determined from
|
||||
# test class if not explicitly set. UserTest => :user
|
||||
attr_accessor :klass
|
||||
|
||||
# Name of the instantiated ActiveRecord object that should be used by some of the tests.
|
||||
# Defaults to the underscored name of the AR class. CompanyManager => :company_manager
|
||||
attr_accessor :object
|
||||
|
||||
# Name of the parent AR objects.
|
||||
#
|
||||
# Example:
|
||||
# # in the routes...
|
||||
# map.resources :companies do
|
||||
# map.resources :people do
|
||||
# map.resources :limbs
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# # in the tests...
|
||||
# class PeopleControllerTest < Test::Unit::TestCase
|
||||
# should_be_restful do |resource|
|
||||
# resource.parent = :companies
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# class LimbsControllerTest < Test::Unit::TestCase
|
||||
# should_be_restful do |resource|
|
||||
# resource.parents = [:companies, :people]
|
||||
# end
|
||||
# end
|
||||
attr_accessor :parent
|
||||
alias parents parent
|
||||
alias parents= parent=
|
||||
|
||||
# Actions that should be tested. Must be a subset of VALID_ACTIONS (default).
|
||||
# Tests for each actionw will only be generated if the action is listed here.
|
||||
# The special value of :all will test all of the REST actions.
|
||||
#
|
||||
# Example (for a read-only controller):
|
||||
# resource.actions = [:show, :index]
|
||||
attr_accessor :actions
|
||||
|
||||
# Formats that should be tested. Must be a subset of VALID_FORMATS (default).
|
||||
# Each action will be tested against the formats listed here. The special value
|
||||
# of :all will test all of the supported formats.
|
||||
#
|
||||
# Example:
|
||||
# resource.actions = [:html, :xml]
|
||||
attr_accessor :formats
|
||||
|
||||
# ActionOptions object specifying options for the create action.
|
||||
attr_accessor :create
|
||||
|
||||
# ActionOptions object specifying options for the update action.
|
||||
attr_accessor :update
|
||||
|
||||
# ActionOptions object specifying options for the desrtoy action.
|
||||
attr_accessor :destroy
|
||||
|
||||
# DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
|
||||
attr_accessor :denied
|
||||
|
||||
def initialize # :nodoc:
|
||||
@create = ActionOptions.new
|
||||
@update = ActionOptions.new
|
||||
@destroy = ActionOptions.new
|
||||
@denied = DeniedOptions.new
|
||||
|
||||
@create.flash ||= /created/i
|
||||
@update.flash ||= /updated/i
|
||||
@destroy.flash ||= /removed/i
|
||||
@denied.flash ||= /denied/i
|
||||
|
||||
@create.params ||= {}
|
||||
@update.params ||= {}
|
||||
|
||||
@actions = VALID_ACTIONS
|
||||
@formats = VALID_FORMATS
|
||||
@denied.actions = []
|
||||
end
|
||||
|
||||
def normalize!(target) # :nodoc:
|
||||
@denied.actions = VALID_ACTIONS if @denied.actions == :all
|
||||
@actions = VALID_ACTIONS if @actions == :all
|
||||
@formats = VALID_FORMATS if @formats == :all
|
||||
|
||||
@denied.actions = @denied.actions.map(&:to_sym)
|
||||
@actions = @actions.map(&:to_sym)
|
||||
@formats = @formats.map(&:to_sym)
|
||||
|
||||
ensure_valid_members(@actions, VALID_ACTIONS, 'actions')
|
||||
ensure_valid_members(@denied.actions, VALID_ACTIONS, 'denied.actions')
|
||||
ensure_valid_members(@formats, VALID_FORMATS, 'formats')
|
||||
|
||||
@identifier ||= :id
|
||||
@klass ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize
|
||||
@object ||= @klass.name.tableize.singularize
|
||||
@parent ||= []
|
||||
@parent = [@parent] unless @parent.is_a? Array
|
||||
|
||||
collection_helper = [@parent, @object.pluralize, 'url'].flatten.join('_')
|
||||
collection_args = @parent.map {|n| "@#{object}.#{n}"}.join(', ')
|
||||
@destroy.redirect ||= "#{collection_helper}(#{collection_args})"
|
||||
|
||||
member_helper = [@parent, @object, 'url'].flatten.join('_')
|
||||
member_args = [@parent.map {|n| "@#{object}.#{n}"}, "@#{object}"].flatten.join(', ')
|
||||
@create.redirect ||= "#{member_helper}(#{member_args})"
|
||||
@update.redirect ||= "#{member_helper}(#{member_args})"
|
||||
@denied.redirect ||= "new_session_url"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_valid_members(ary, valid_members, name) # :nodoc:
|
||||
invalid = ary - valid_members
|
||||
raise ArgumentError, "Unsupported #{name}: #{invalid.inspect}" unless invalid.empty?
|
||||
end
|
||||
end
|
||||
|
||||
# :section: should_be_restful
|
||||
# Generates a full suite of tests for a restful controller.
|
||||
#
|
||||
# The following definition will generate tests for the +index+, +show+, +new+,
|
||||
# +edit+, +create+, +update+ and +destroy+ actions, in both +html+ and +xml+ formats:
|
||||
#
|
||||
# should_be_restful do |resource|
|
||||
# resource.parent = :user
|
||||
#
|
||||
# resource.create.params = { :title => "first post", :body => 'blah blah blah'}
|
||||
# resource.update.params = { :title => "changed" }
|
||||
# end
|
||||
#
|
||||
# This generates about 40 tests, all of the format:
|
||||
# "on GET to :show should assign @user."
|
||||
# "on GET to :show should not set the flash."
|
||||
# "on GET to :show should render 'show' template."
|
||||
# "on GET to :show should respond with success."
|
||||
# "on GET to :show as xml should assign @user."
|
||||
# "on GET to :show as xml should have ContentType set to 'application/xml'."
|
||||
# "on GET to :show as xml should respond with success."
|
||||
# "on GET to :show as xml should return <user/> as the root element."
|
||||
# The +resource+ parameter passed into the block is a ResourceOptions object, and
|
||||
# is used to configure the tests for the details of your resources.
|
||||
#
|
||||
def should_be_restful(&blk) # :yields: resource
|
||||
resource = ResourceOptions.new
|
||||
blk.call(resource)
|
||||
resource.normalize!(self)
|
||||
|
||||
resource.formats.each do |format|
|
||||
resource.actions.each do |action|
|
||||
if self.respond_to? :"make_#{action}_#{format}_tests"
|
||||
self.send(:"make_#{action}_#{format}_tests", resource)
|
||||
else
|
||||
should "test #{action} #{format}" do
|
||||
flunk "Test for #{action} as #{format} not implemented"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# :section: Test macros
|
||||
|
||||
# Macro that creates a test asserting that the flash contains the given value.
|
||||
# val can be a String, a Regex, or nil (indicating that the flash should not be set)
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# should_set_the_flash_to "Thank you for placing this order."
|
||||
# should_set_the_flash_to /created/i
|
||||
# should_set_the_flash_to nil
|
||||
def should_set_the_flash_to(val)
|
||||
if val
|
||||
should "have #{val.inspect} in the flash" do
|
||||
assert_contains flash.values, val, ", Flash: #{flash.inspect}"
|
||||
end
|
||||
else
|
||||
should "not set the flash" do
|
||||
assert_equal({}, flash, "Flash was set to:\n#{flash.inspect}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Macro that creates a test asserting that the flash is empty. Same as
|
||||
# @should_set_the_flash_to nil@
|
||||
def should_not_set_the_flash
|
||||
should_set_the_flash_to nil
|
||||
end
|
||||
|
||||
# Macro that creates a test asserting that the controller assigned to @name
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# should_assign_to :user
|
||||
def should_assign_to(name)
|
||||
should "assign @#{name}" do
|
||||
assert assigns(name.to_sym), "The action isn't assigning to @#{name}"
|
||||
end
|
||||
end
|
||||
|
||||
# Macro that creates a test asserting that the controller did not assign to @name
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# should_not_assign_to :user
|
||||
def should_not_assign_to(name)
|
||||
should "not assign to @#{name}" do
|
||||
assert !assigns(name.to_sym), "@#{name} was visible"
|
||||
end
|
||||
end
|
||||
|
||||
# Macro that creates a test asserting that the controller responded with a 'response' status code.
|
||||
# Example:
|
||||
#
|
||||
# should_respond_with :success
|
||||
def should_respond_with(response)
|
||||
should "respond with #{response}" do
|
||||
assert_response response
|
||||
end
|
||||
end
|
||||
|
||||
# Macro that creates a test asserting that the controller rendered the given template.
|
||||
# Example:
|
||||
#
|
||||
# should_render_template :new
|
||||
def should_render_template(template)
|
||||
should "render '#{template}' template" do
|
||||
assert_template template.to_s
|
||||
end
|
||||
end
|
||||
|
||||
# Macro that creates a test asserting that the controller returned a redirect to the given path.
|
||||
# The given string is evaled to produce the resulting redirect path. All of the instance variables
|
||||
# set by the controller are available to the evaled string.
|
||||
# Example:
|
||||
#
|
||||
# should_redirect_to '"/"'
|
||||
# should_redirect_to "users_url(@user)"
|
||||
def should_redirect_to(url)
|
||||
should "redirect to \"#{url}\"" do
|
||||
instantiate_variables_from_assigns do
|
||||
assert_redirected_to eval(url, self.send(:binding), __FILE__, __LINE__)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Macro that creates a test asserting that the rendered view contains a <form> element.
|
||||
def should_render_a_form
|
||||
should "display a form" do
|
||||
assert_select "form", true, "The template doesn't contain a <form> element"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods # :nodoc:
|
||||
|
||||
private # :enddoc:
|
||||
|
||||
SPECIAL_INSTANCE_VARIABLES = %w{
|
||||
_cookies
|
||||
_flash
|
||||
_headers
|
||||
_params
|
||||
_request
|
||||
_response
|
||||
_session
|
||||
action_name
|
||||
before_filter_chain_aborted
|
||||
cookies
|
||||
flash
|
||||
headers
|
||||
ignore_missing_templates
|
||||
logger
|
||||
params
|
||||
request
|
||||
request_origin
|
||||
response
|
||||
session
|
||||
template
|
||||
template_class
|
||||
template_root
|
||||
url
|
||||
variables_added
|
||||
}.map(&:to_s)
|
||||
|
||||
def instantiate_variables_from_assigns(*names, &blk)
|
||||
old = {}
|
||||
names = (@response.template.assigns.keys - SPECIAL_INSTANCE_VARIABLES) if names.empty?
|
||||
names.each do |name|
|
||||
old[name] = instance_variable_get("@#{name}")
|
||||
instance_variable_set("@#{name}", assigns(name.to_sym))
|
||||
end
|
||||
blk.call
|
||||
names.each do |name|
|
||||
instance_variable_set("@#{name}", old[name])
|
||||
end
|
||||
end
|
||||
|
||||
def get_existing_record(res) # :nodoc:
|
||||
returning(instance_variable_get("@#{res.object}")) do |record|
|
||||
assert(record, "This test requires you to set @#{res.object} in your setup block")
|
||||
end
|
||||
end
|
||||
|
||||
def make_parent_params(resource, record = nil, parent_names = nil) # :nodoc:
|
||||
parent_names ||= resource.parents.reverse
|
||||
return {} if parent_names == [] # Base case
|
||||
parent_name = parent_names.shift
|
||||
parent = record ? record.send(parent_name) : parent_name.to_s.classify.constantize.find(:first)
|
||||
|
||||
{ :"#{parent_name}_id" => parent.id }.merge(make_parent_params(resource, parent, parent_names))
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
195
vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb
vendored
Normal file
195
vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb
vendored
Normal file
@@ -0,0 +1,195 @@
|
||||
module ThoughtBot # :nodoc:
|
||||
module Shoulda # :nodoc:
|
||||
module Controller # :nodoc:
|
||||
module HTML # :nodoc: all
|
||||
def self.included(other)
|
||||
other.class_eval do
|
||||
extend ThoughtBot::Shoulda::Controller::HTML::ClassMethods
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def make_show_html_tests(res)
|
||||
context "on GET to :show" do
|
||||
setup do
|
||||
record = get_existing_record(res)
|
||||
parent_params = make_parent_params(res, record)
|
||||
get :show, parent_params.merge({ res.identifier => record.to_param })
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:show)
|
||||
should_not_assign_to res.object
|
||||
should_redirect_to res.denied.redirect
|
||||
should_set_the_flash_to res.denied.flash
|
||||
else
|
||||
should_assign_to res.object
|
||||
should_respond_with :success
|
||||
should_render_template :show
|
||||
should_not_set_the_flash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_edit_html_tests(res)
|
||||
context "on GET to :edit" do
|
||||
setup do
|
||||
@record = get_existing_record(res)
|
||||
parent_params = make_parent_params(res, @record)
|
||||
get :edit, parent_params.merge({ res.identifier => @record.to_param })
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:edit)
|
||||
should_not_assign_to res.object
|
||||
should_redirect_to res.denied.redirect
|
||||
should_set_the_flash_to res.denied.flash
|
||||
else
|
||||
should_assign_to res.object
|
||||
should_respond_with :success
|
||||
should_render_template :edit
|
||||
should_not_set_the_flash
|
||||
should_render_a_form
|
||||
should "set @#{res.object} to requested instance" do
|
||||
assert_equal @record, assigns(res.object)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_index_html_tests(res)
|
||||
context "on GET to :index" do
|
||||
setup do
|
||||
record = get_existing_record(res) rescue nil
|
||||
parent_params = make_parent_params(res, record)
|
||||
get(:index, parent_params)
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:index)
|
||||
should_not_assign_to res.object.to_s.pluralize
|
||||
should_redirect_to res.denied.redirect
|
||||
should_set_the_flash_to res.denied.flash
|
||||
else
|
||||
should_respond_with :success
|
||||
should_assign_to res.object.to_s.pluralize
|
||||
should_render_template :index
|
||||
should_not_set_the_flash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_new_html_tests(res)
|
||||
context "on GET to :new" do
|
||||
setup do
|
||||
record = get_existing_record(res) rescue nil
|
||||
parent_params = make_parent_params(res, record)
|
||||
get(:new, parent_params)
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:new)
|
||||
should_not_assign_to res.object
|
||||
should_redirect_to res.denied.redirect
|
||||
should_set_the_flash_to res.denied.flash
|
||||
else
|
||||
should_respond_with :success
|
||||
should_assign_to res.object
|
||||
should_not_set_the_flash
|
||||
should_render_template :new
|
||||
should_render_a_form
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_destroy_html_tests(res)
|
||||
context "on DELETE to :destroy" do
|
||||
setup do
|
||||
@record = get_existing_record(res)
|
||||
parent_params = make_parent_params(res, @record)
|
||||
delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:destroy)
|
||||
should_redirect_to res.denied.redirect
|
||||
should_set_the_flash_to res.denied.flash
|
||||
|
||||
should "not destroy record" do
|
||||
assert_nothing_raised { assert @record.reload }
|
||||
end
|
||||
else
|
||||
should_set_the_flash_to res.destroy.flash
|
||||
if res.destroy.redirect.is_a? Symbol
|
||||
should_respond_with res.destroy.redirect
|
||||
else
|
||||
should_redirect_to res.destroy.redirect
|
||||
end
|
||||
|
||||
should "destroy record" do
|
||||
assert_raises(::ActiveRecord::RecordNotFound) { @record.reload }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_create_html_tests(res)
|
||||
context "on POST to :create with #{res.create.params.inspect}" do
|
||||
setup do
|
||||
record = get_existing_record(res) rescue nil
|
||||
parent_params = make_parent_params(res, record)
|
||||
@count = res.klass.count
|
||||
post :create, parent_params.merge(res.object => res.create.params)
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:create)
|
||||
should_redirect_to res.denied.redirect
|
||||
should_set_the_flash_to res.denied.flash
|
||||
should_not_assign_to res.object
|
||||
|
||||
should "not create new record" do
|
||||
assert_equal @count, res.klass.count
|
||||
end
|
||||
else
|
||||
should_assign_to res.object
|
||||
should_set_the_flash_to res.create.flash
|
||||
if res.create.redirect.is_a? Symbol
|
||||
should_respond_with res.create.redirect
|
||||
else
|
||||
should_redirect_to res.create.redirect
|
||||
end
|
||||
|
||||
should "not have errors on @#{res.object}" do
|
||||
assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_update_html_tests(res)
|
||||
context "on PUT to :update with #{res.create.params.inspect}" do
|
||||
setup do
|
||||
@record = get_existing_record(res)
|
||||
parent_params = make_parent_params(res, @record)
|
||||
put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:update)
|
||||
should_not_assign_to res.object
|
||||
should_redirect_to res.denied.redirect
|
||||
should_set_the_flash_to res.denied.flash
|
||||
else
|
||||
should_assign_to res.object
|
||||
should_set_the_flash_to(res.update.flash)
|
||||
if res.update.redirect.is_a? Symbol
|
||||
should_respond_with res.update.redirect
|
||||
else
|
||||
should_redirect_to res.update.redirect
|
||||
end
|
||||
|
||||
should "not have errors on @#{res.object}" do
|
||||
assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
162
vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb
vendored
Normal file
162
vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb
vendored
Normal file
@@ -0,0 +1,162 @@
|
||||
module ThoughtBot # :nodoc:
|
||||
module Shoulda # :nodoc:
|
||||
module Controller # :nodoc:
|
||||
module XML
|
||||
def self.included(other) #:nodoc:
|
||||
other.class_eval do
|
||||
extend ThoughtBot::Shoulda::Controller::XML::ClassMethods
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Macro that creates a test asserting that the controller responded with an XML content-type
|
||||
# and that the XML contains +<name/>+ as the root element.
|
||||
def should_respond_with_xml_for(name = nil)
|
||||
should "have ContentType set to 'application/xml'" do
|
||||
assert_xml_response
|
||||
end
|
||||
|
||||
if name
|
||||
should "return <#{name}/> as the root element" do
|
||||
body = @response.body.first(100).map {|l| " #{l}"}
|
||||
assert_select name.to_s.dasherize, 1, "Body:\n#{body}...\nDoes not have <#{name}/> as the root element."
|
||||
end
|
||||
end
|
||||
end
|
||||
alias should_respond_with_xml should_respond_with_xml_for
|
||||
|
||||
protected
|
||||
|
||||
def make_show_xml_tests(res) # :nodoc:
|
||||
context "on GET to :show as xml" do
|
||||
setup do
|
||||
request_xml
|
||||
record = get_existing_record(res)
|
||||
parent_params = make_parent_params(res, record)
|
||||
get :show, parent_params.merge({ res.identifier => record.to_param })
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:show)
|
||||
should_not_assign_to res.object
|
||||
should_respond_with 401
|
||||
else
|
||||
should_assign_to res.object
|
||||
should_respond_with :success
|
||||
should_respond_with_xml_for res.object
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_edit_xml_tests(res) # :nodoc:
|
||||
# XML doesn't need an :edit action
|
||||
end
|
||||
|
||||
def make_new_xml_tests(res) # :nodoc:
|
||||
# XML doesn't need a :new action
|
||||
end
|
||||
|
||||
def make_index_xml_tests(res) # :nodoc:
|
||||
context "on GET to :index as xml" do
|
||||
setup do
|
||||
request_xml
|
||||
parent_params = make_parent_params(res)
|
||||
get(:index, parent_params)
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:index)
|
||||
should_not_assign_to res.object.to_s.pluralize
|
||||
should_respond_with 401
|
||||
else
|
||||
should_respond_with :success
|
||||
should_respond_with_xml_for res.object.to_s.pluralize
|
||||
should_assign_to res.object.to_s.pluralize
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_destroy_xml_tests(res) # :nodoc:
|
||||
context "on DELETE to :destroy as xml" do
|
||||
setup do
|
||||
request_xml
|
||||
@record = get_existing_record(res)
|
||||
parent_params = make_parent_params(res, @record)
|
||||
delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:destroy)
|
||||
should_respond_with 401
|
||||
|
||||
should "not destroy record" do
|
||||
assert @record.reload
|
||||
end
|
||||
else
|
||||
should "destroy record" do
|
||||
assert_raises(::ActiveRecord::RecordNotFound) { @record.reload }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_create_xml_tests(res) # :nodoc:
|
||||
context "on POST to :create as xml" do
|
||||
setup do
|
||||
request_xml
|
||||
parent_params = make_parent_params(res)
|
||||
@count = res.klass.count
|
||||
post :create, parent_params.merge(res.object => res.create.params)
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:create)
|
||||
should_respond_with 401
|
||||
should_not_assign_to res.object
|
||||
|
||||
should "not create new record" do
|
||||
assert_equal @count, res.klass.count
|
||||
end
|
||||
else
|
||||
should_assign_to res.object
|
||||
|
||||
should "not have errors on @#{res.object}" do
|
||||
assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def make_update_xml_tests(res) # :nodoc:
|
||||
context "on PUT to :update as xml" do
|
||||
setup do
|
||||
request_xml
|
||||
@record = get_existing_record(res)
|
||||
parent_params = make_parent_params(res, @record)
|
||||
put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
|
||||
end
|
||||
|
||||
if res.denied.actions.include?(:update)
|
||||
should_not_assign_to res.object
|
||||
should_respond_with 401
|
||||
else
|
||||
should_assign_to res.object
|
||||
|
||||
should "not have errors on @#{res.object}" do
|
||||
assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Sets the next request's format to 'application/xml'
|
||||
def request_xml
|
||||
@request.accept = "application/xml"
|
||||
end
|
||||
|
||||
# Asserts that the controller's response was 'application/xml'
|
||||
def assert_xml_response
|
||||
assert_equal "application/xml", @response.content_type, "Body: #{@response.body.first(100).chomp}..."
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
14
vendor/plugins/shoulda/lib/shoulda/gem/proc_extensions.rb
vendored
Normal file
14
vendor/plugins/shoulda/lib/shoulda/gem/proc_extensions.rb
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
# Stolen straight from ActiveSupport
|
||||
|
||||
class Proc #:nodoc:
|
||||
def bind(object)
|
||||
block, time = self, Time.now
|
||||
(class << object; self end).class_eval do
|
||||
method_name = "__bind_#{time.to_i}_#{time.usec}"
|
||||
define_method(method_name, &block)
|
||||
method = instance_method(method_name)
|
||||
remove_method(method_name)
|
||||
method
|
||||
end.bind(object)
|
||||
end
|
||||
end
|
||||
165
vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb
vendored
Normal file
165
vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb
vendored
Normal file
@@ -0,0 +1,165 @@
|
||||
require File.join(File.dirname(__FILE__), 'proc_extensions')
|
||||
|
||||
module Thoughtbot
|
||||
class Shoulda
|
||||
VERSION = '1.0.0'
|
||||
|
||||
# = context and should blocks
|
||||
#
|
||||
# A context block groups should statements under a common setup/teardown method.
|
||||
# Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
|
||||
# and readability of your test code.
|
||||
#
|
||||
# A context block can contain setup, should, should_eventually, and teardown blocks.
|
||||
#
|
||||
# class UserTest << Test::Unit::TestCase
|
||||
# context "a User instance" do
|
||||
# setup do
|
||||
# @user = User.find(:first)
|
||||
# end
|
||||
#
|
||||
# should "return its full name"
|
||||
# assert_equal 'John Doe', @user.full_name
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# This code will produce the method <tt>"test a User instance should return its full name"</tt>.
|
||||
#
|
||||
# Contexts may be nested. Nested contexts run their setup blocks from out to in before each test.
|
||||
# They then run their teardown blocks from in to out after each test.
|
||||
#
|
||||
# class UserTest << Test::Unit::TestCase
|
||||
# context "a User instance" do
|
||||
# setup do
|
||||
# @user = User.find(:first)
|
||||
# end
|
||||
#
|
||||
# should "return its full name"
|
||||
# assert_equal 'John Doe', @user.full_name
|
||||
# end
|
||||
#
|
||||
# context "with a profile" do
|
||||
# setup do
|
||||
# @user.profile = Profile.find(:first)
|
||||
# end
|
||||
#
|
||||
# should "return true when sent :has_profile?"
|
||||
# assert @user.has_profile?
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# This code will produce the following methods
|
||||
# * <tt>"test: a User instance should return its full name."</tt>
|
||||
# * <tt>"test: a User instance with a profile should return true when sent :has_profile?."</tt>
|
||||
#
|
||||
# <b>A context block can exist next to normal <tt>def test_the_old_way; end</tt> tests</b>,
|
||||
# meaning you do not have to fully commit to the context/should syntax in a test file.
|
||||
#
|
||||
|
||||
module ClassMethods
|
||||
def self.included(other) # :nodoc:
|
||||
@@context_names = []
|
||||
@@setup_blocks = []
|
||||
@@teardown_blocks = []
|
||||
end
|
||||
|
||||
# Defines a test method. Can be called either inside our outside of a context.
|
||||
# Optionally specify <tt>:unimplimented => true</tt> (see should_eventually).
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# class UserTest << Test::Unit::TestCase
|
||||
# should "return first user on find(:first)"
|
||||
# assert_equal users(:first), User.find(:first)
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# Would create a test named
|
||||
# 'test: should return first user on find(:first)'
|
||||
#
|
||||
def should(name, opts = {}, &should_block)
|
||||
test_name = ["test:", @@context_names, "should", "#{name}. "].flatten.join(' ').to_sym
|
||||
|
||||
name_defined = eval("self.instance_methods.include?('#{test_name.to_s.gsub(/['"]/, '\$1')}')", should_block.binding)
|
||||
raise ArgumentError, "'#{test_name}' is already defined" and return if name_defined
|
||||
|
||||
setup_blocks = @@setup_blocks.dup
|
||||
teardown_blocks = @@teardown_blocks.dup
|
||||
|
||||
if opts[:unimplemented]
|
||||
define_method test_name do |*args|
|
||||
# XXX find a better way of doing this.
|
||||
assert true
|
||||
STDOUT.putc "X" # Tests for this model are missing.
|
||||
end
|
||||
else
|
||||
define_method test_name do |*args|
|
||||
begin
|
||||
setup_blocks.each {|b| b.bind(self).call }
|
||||
should_block.bind(self).call(*args)
|
||||
ensure
|
||||
teardown_blocks.reverse.each {|b| b.bind(self).call }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Creates a context block with the given name.
|
||||
def context(name, &context_block)
|
||||
saved_setups = @@setup_blocks.dup
|
||||
saved_teardowns = @@teardown_blocks.dup
|
||||
saved_contexts = @@context_names.dup
|
||||
|
||||
@@setup_defined = false
|
||||
|
||||
@@context_names << name
|
||||
context_block.bind(self).call
|
||||
|
||||
@@context_names = saved_contexts
|
||||
@@setup_blocks = saved_setups
|
||||
@@teardown_blocks = saved_teardowns
|
||||
end
|
||||
|
||||
# Run before every should block in the current context.
|
||||
# If a setup block appears in a nested context, it will be run after the setup blocks
|
||||
# in the parent contexts.
|
||||
def setup(&setup_block)
|
||||
if @@setup_defined
|
||||
raise RuntimeError, "Either you have two setup blocks in one context, " +
|
||||
"or a setup block outside of a context. Both are equally bad."
|
||||
end
|
||||
@@setup_defined = true
|
||||
|
||||
@@setup_blocks << setup_block
|
||||
end
|
||||
|
||||
# Run after every should block in the current context.
|
||||
# If a teardown block appears in a nested context, it will be run before the teardown
|
||||
# blocks in the parent contexts.
|
||||
def teardown(&teardown_block)
|
||||
@@teardown_blocks << teardown_block
|
||||
end
|
||||
|
||||
# Defines a specification that is not yet implemented.
|
||||
# Will be displayed as an 'X' when running tests, and failures will not be shown.
|
||||
# This is equivalent to:
|
||||
# should(name, {:unimplemented => true}, &block)
|
||||
def should_eventually(name, &block)
|
||||
should("eventually #{name}", {:unimplemented => true}, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Test # :nodoc: all
|
||||
module Unit
|
||||
class TestCase
|
||||
class << self
|
||||
include Thoughtbot::Shoulda::ClassMethods
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
101
vendor/plugins/shoulda/lib/shoulda/general.rb
vendored
Normal file
101
vendor/plugins/shoulda/lib/shoulda/general.rb
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
module ThoughtBot # :nodoc:
|
||||
module Shoulda # :nodoc:
|
||||
module General
|
||||
def self.included(other) # :nodoc:
|
||||
other.class_eval do
|
||||
extend ThoughtBot::Shoulda::General::ClassMethods
|
||||
# include ThoughtBot::Shoulda::General::InstanceMethods
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Loads all fixture files (<tt>test/fixtures/*.yml</tt>)
|
||||
def load_all_fixtures
|
||||
all_fixtures = Dir.glob(File.join(Test::Unit::TestCase.fixture_path, "*.yml")).collect do |f|
|
||||
File.basename(f, '.yml').to_sym
|
||||
end
|
||||
fixtures *all_fixtures
|
||||
end
|
||||
end
|
||||
|
||||
# Prints a message to stdout, tagged with the name of the calling method.
|
||||
def report!(msg = "")
|
||||
puts("#{caller.first}: #{msg}")
|
||||
end
|
||||
|
||||
# Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
|
||||
#
|
||||
# assert_same_elements([:a, :b, :c], [:c, :a, :b]) => passes
|
||||
def assert_same_elements(a1, a2, msg = nil)
|
||||
[:select, :inject, :size].each do |m|
|
||||
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") }
|
||||
end
|
||||
|
||||
assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
|
||||
assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }
|
||||
|
||||
assert_equal(a1h, a2h, msg)
|
||||
end
|
||||
|
||||
# Asserts that the given collection contains item x. If x is a regular expression, ensure that
|
||||
# at least one element from the collection matches x. +extra_msg+ is appended to the error message if the assertion fails.
|
||||
#
|
||||
# assert_contains(['a', '1'], /\d/) => passes
|
||||
# assert_contains(['a', '1'], 'a') => passes
|
||||
# assert_contains(['a', '1'], /not there/) => fails
|
||||
def assert_contains(collection, x, extra_msg = "")
|
||||
collection = [collection] unless collection.is_a?(Array)
|
||||
msg = "#{x.inspect} not found in #{collection.to_a.inspect} " + extra_msg
|
||||
case x
|
||||
when Regexp: assert(collection.detect { |e| e =~ x }, msg)
|
||||
else assert(collection.include?(x), msg)
|
||||
end
|
||||
end
|
||||
|
||||
# Asserts that the given collection does not contain item x. If x is a regular expression, ensure that
|
||||
# none of the elements from the collection match x.
|
||||
def assert_does_not_contain(collection, x, extra_msg = "")
|
||||
collection = [collection] unless collection.is_a?(Array)
|
||||
msg = "#{x.inspect} found in #{collection.to_a.inspect} " + extra_msg
|
||||
case x
|
||||
when Regexp: assert(!collection.detect { |e| e =~ x }, msg)
|
||||
else assert(!collection.include?(x), msg)
|
||||
end
|
||||
end
|
||||
|
||||
# Asserts that the given object can be saved
|
||||
#
|
||||
# assert_save User.new(params)
|
||||
def assert_save(obj)
|
||||
assert obj.save, "Errors: #{obj.errors.full_messages.join('; ')}"
|
||||
obj.reload
|
||||
end
|
||||
|
||||
# Asserts that the given object is valid
|
||||
#
|
||||
# assert_save User.new(params)
|
||||
def assert_valid(obj)
|
||||
assert obj.valid?, "Errors: #{obj.errors.full_messages.join('; ')}"
|
||||
end
|
||||
|
||||
# Asserts that the block uses ActionMailer to send emails
|
||||
#
|
||||
# assert_sends_email(2) { Mailer.deliver_messages }
|
||||
def assert_sends_email(num = 1, &blk)
|
||||
ActionMailer::Base.deliveries.clear
|
||||
blk.call
|
||||
msg = "Sent #{ActionMailer::Base.deliveries.size} emails, when #{num} expected:\n"
|
||||
ActionMailer::Base.deliveries.each { |m| msg << " '#{m.subject}' sent to #{m.to.to_sentence}\n" }
|
||||
assert(num == ActionMailer::Base.deliveries.size, msg)
|
||||
end
|
||||
|
||||
# Asserts that the block does not send emails thorough ActionMailer
|
||||
#
|
||||
# assert_does_not_send_email { # do nothing }
|
||||
def assert_does_not_send_email(&blk)
|
||||
assert_sends_email 0, &blk
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
17
vendor/plugins/shoulda/lib/shoulda/private_helpers.rb
vendored
Normal file
17
vendor/plugins/shoulda/lib/shoulda/private_helpers.rb
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
module ThoughtBot # :nodoc:
|
||||
module Shoulda # :nodoc:
|
||||
module Private # :nodoc:
|
||||
def get_options!(args, *wanted)
|
||||
ret = []
|
||||
opts = (args.last.is_a?(Hash) ? args.pop : {})
|
||||
wanted.each {|w| ret << opts.delete(w)}
|
||||
raise ArgumentError, "Unsuported options given: #{opts.keys.join(', ')}" unless opts.keys.empty?
|
||||
return *ret
|
||||
end
|
||||
|
||||
def model_class
|
||||
self.name.gsub(/Test$/, '').constantize
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
40
vendor/plugins/shoulda/tasks/list_tests.rake
vendored
Normal file
40
vendor/plugins/shoulda/tasks/list_tests.rake
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace :shoulda do
|
||||
desc "List the names of the test methods in a specification like format"
|
||||
task :list do
|
||||
|
||||
require 'test/unit'
|
||||
require 'rubygems'
|
||||
require 'active_support'
|
||||
|
||||
# bug in test unit. Set to true to stop from running.
|
||||
Test::Unit.run = true
|
||||
|
||||
test_files = Dir.glob(File.join('test', '**', '*_test.rb'))
|
||||
test_files.each do |file|
|
||||
load file
|
||||
klass = File.basename(file, '.rb').classify.constantize
|
||||
|
||||
puts
|
||||
puts "#{klass.name.gsub(/Test$/, '')}"
|
||||
test_methods = klass.instance_methods.grep(/^test/).map {|s| s.gsub(/^test: /, '')}.sort
|
||||
test_methods.each {|m| puts " - #{m}" }
|
||||
# puts "#{klass.name.gsub(/Test$/, '')}"
|
||||
# test_methods = klass.instance_methods.grep(/^test/).sort
|
||||
#
|
||||
# method_hash = test_methods.inject({}) do |h, name|
|
||||
# header = name.gsub(/^test: (.*)should.*$/, '\1')
|
||||
# test = name.gsub(/^test:.*should (.*)$/, '\1')
|
||||
# h[header] ||= []
|
||||
# h[header] << test
|
||||
# h
|
||||
# end
|
||||
#
|
||||
# method_hash.keys.sort.each do |header|
|
||||
# puts " #{header.chomp} should"
|
||||
# method_hash[header].each do |test|
|
||||
# puts " - #{test}"
|
||||
# end
|
||||
# end
|
||||
end
|
||||
end
|
||||
end
|
||||
28
vendor/plugins/shoulda/tasks/yaml_to_shoulda.rake
vendored
Normal file
28
vendor/plugins/shoulda/tasks/yaml_to_shoulda.rake
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace :shoulda do
|
||||
# From http://blog.internautdesign.com/2007/11/2/a-yaml_to_shoulda-rake-task
|
||||
# David.Lowenfels@gmail.com
|
||||
desc "Converts a YAML file (FILE=./path/to/yaml) into a Shoulda skeleton"
|
||||
task :from_yaml do
|
||||
require 'yaml'
|
||||
|
||||
def yaml_to_context(hash, indent = 0)
|
||||
indent1 = ' ' * indent
|
||||
indent2 = ' ' * (indent + 1)
|
||||
hash.each_pair do |context, shoulds|
|
||||
puts indent1 + "context \"#{context}\" do"
|
||||
puts
|
||||
shoulds.each do |should|
|
||||
yaml_to_context( should, indent + 1 ) and next if should.is_a?( Hash )
|
||||
puts indent2 + "should_eventually \"" + should.gsub(/^should +/,'') + "\" do"
|
||||
puts indent2 + "end"
|
||||
puts
|
||||
end
|
||||
puts indent1 + "end"
|
||||
end
|
||||
end
|
||||
|
||||
puts("Please pass in a FILE argument.") and exit unless ENV['FILE']
|
||||
|
||||
yaml_to_context( YAML.load_file( ENV['FILE'] ) )
|
||||
end
|
||||
end
|
||||
8
vendor/plugins/shoulda/test/README
vendored
Normal file
8
vendor/plugins/shoulda/test/README
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
The tests for should have two dependencies that I know of:
|
||||
|
||||
* Rails version 1.2.3
|
||||
* A working sqlite3 installation.
|
||||
|
||||
If you have problems running these tests, please notify the shoulda mailing list: shoulda@googlegroups.com
|
||||
|
||||
- Tammer Saleh
|
||||
5
vendor/plugins/shoulda/test/fixtures/posts.yml
vendored
Normal file
5
vendor/plugins/shoulda/test/fixtures/posts.yml
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
first:
|
||||
id: 1
|
||||
title: My Cute Kitten!
|
||||
body: This is totally a cute kitten
|
||||
user_id: 1
|
||||
0
vendor/plugins/shoulda/test/fixtures/taggings.yml
vendored
Normal file
0
vendor/plugins/shoulda/test/fixtures/taggings.yml
vendored
Normal file
9
vendor/plugins/shoulda/test/fixtures/tags.yml
vendored
Normal file
9
vendor/plugins/shoulda/test/fixtures/tags.yml
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
first:
|
||||
id: 1
|
||||
name: Stuff
|
||||
second:
|
||||
id: 2
|
||||
name: Rails
|
||||
third:
|
||||
id: 3
|
||||
name: Nothing
|
||||
5
vendor/plugins/shoulda/test/fixtures/users.yml
vendored
Normal file
5
vendor/plugins/shoulda/test/fixtures/users.yml
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
first:
|
||||
id: 1
|
||||
name: Some dude
|
||||
age: 2
|
||||
email: none@none.com
|
||||
43
vendor/plugins/shoulda/test/functional/posts_controller_test.rb
vendored
Normal file
43
vendor/plugins/shoulda/test/functional/posts_controller_test.rb
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
require 'posts_controller'
|
||||
|
||||
# Re-raise errors caught by the controller.
|
||||
class PostsController; def rescue_action(e) raise e end; end
|
||||
|
||||
class PostsControllerTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
|
||||
def setup
|
||||
@controller = PostsController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@post = Post.find(:first)
|
||||
end
|
||||
|
||||
context "The public" do
|
||||
setup do
|
||||
@request.session[:logged_in] = false
|
||||
end
|
||||
|
||||
should_be_restful do |resource|
|
||||
resource.parent = :user
|
||||
|
||||
resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
|
||||
resource.denied.flash = /what/i
|
||||
resource.denied.redirect = '"/"'
|
||||
end
|
||||
end
|
||||
|
||||
context "Logged in" do
|
||||
setup do
|
||||
@request.session[:logged_in] = true
|
||||
end
|
||||
|
||||
should_be_restful do |resource|
|
||||
resource.parent = :user
|
||||
|
||||
resource.create.params = { :title => "first post", :body => 'blah blah blah'}
|
||||
resource.update.params = { :title => "changed" }
|
||||
end
|
||||
end
|
||||
end
|
||||
36
vendor/plugins/shoulda/test/functional/users_controller_test.rb
vendored
Normal file
36
vendor/plugins/shoulda/test/functional/users_controller_test.rb
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
require 'users_controller'
|
||||
|
||||
# Re-raise errors caught by the controller.
|
||||
class UsersController; def rescue_action(e) raise e end; end
|
||||
|
||||
class UsersControllerTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
|
||||
def setup
|
||||
@controller = UsersController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
@user = User.find(:first)
|
||||
end
|
||||
|
||||
should_be_restful do |resource|
|
||||
resource.identifier = :id
|
||||
resource.klass = User
|
||||
resource.object = :user
|
||||
resource.parent = []
|
||||
resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
|
||||
resource.formats = [:html, :xml]
|
||||
|
||||
resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
|
||||
resource.update.params = { :name => "sue" }
|
||||
|
||||
resource.create.redirect = "user_url(@user)"
|
||||
resource.update.redirect = "user_url(@user)"
|
||||
resource.destroy.redirect = "users_url"
|
||||
|
||||
resource.create.flash = /created/i
|
||||
resource.update.flash = /updated/i
|
||||
resource.destroy.flash = /removed/i
|
||||
end
|
||||
end
|
||||
71
vendor/plugins/shoulda/test/other/context_test.rb
vendored
Normal file
71
vendor/plugins/shoulda/test/other/context_test.rb
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
||||
|
||||
class ContextTest < Test::Unit::TestCase # :nodoc:
|
||||
|
||||
context "context with setup block" do
|
||||
setup do
|
||||
@blah = "blah"
|
||||
end
|
||||
|
||||
should "have @blah == 'blah'" do
|
||||
assert_equal "blah", @blah
|
||||
end
|
||||
|
||||
should "have name set right" do
|
||||
assert_match(/^test: context with setup block/, self.to_s)
|
||||
end
|
||||
|
||||
context "and a subcontext" do
|
||||
setup do
|
||||
@blah = "#{@blah} twice"
|
||||
end
|
||||
|
||||
should "be named correctly" do
|
||||
assert_match(/^test: context with setup block and a subcontext should be named correctly/, self.to_s)
|
||||
end
|
||||
|
||||
should "run the setup methods in order" do
|
||||
assert_equal @blah, "blah twice"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "another context with setup block" do
|
||||
setup do
|
||||
@blah = "foo"
|
||||
end
|
||||
|
||||
should "have @blah == 'foo'" do
|
||||
assert_equal "foo", @blah
|
||||
end
|
||||
|
||||
should "have name set right" do
|
||||
assert_match(/^test: another context with setup block/, self.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
context "context with method definition" do
|
||||
setup do
|
||||
def hello; "hi"; end
|
||||
end
|
||||
|
||||
should "be able to read that method" do
|
||||
assert_equal "hi", hello
|
||||
end
|
||||
|
||||
should "have name set right" do
|
||||
assert_match(/^test: context with method definition/, self.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
context "another context" do
|
||||
should "not define @blah" do
|
||||
assert_nil @blah
|
||||
end
|
||||
end
|
||||
|
||||
should_eventually "should pass, since it's unimplemented" do
|
||||
flunk "what?"
|
||||
end
|
||||
|
||||
end
|
||||
40
vendor/plugins/shoulda/test/other/helpers_test.rb
vendored
Normal file
40
vendor/plugins/shoulda/test/other/helpers_test.rb
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
||||
|
||||
class Val
|
||||
@@val = 0
|
||||
def self.val; @@val; end
|
||||
def self.inc(i=1); @@val += i; end
|
||||
end
|
||||
|
||||
class HelpersTest < Test::Unit::TestCase # :nodoc:
|
||||
|
||||
context "an array of values" do
|
||||
setup do
|
||||
@a = ['abc', 'def', 3]
|
||||
end
|
||||
|
||||
[/b/, 'abc', 3].each do |x|
|
||||
should "contain #{x.inspect}" do
|
||||
assert_raises(Test::Unit::AssertionFailedError) do
|
||||
assert_does_not_contain @a, x
|
||||
end
|
||||
assert_contains @a, x
|
||||
end
|
||||
end
|
||||
|
||||
should "not contain 'wtf'" do
|
||||
assert_raises(Test::Unit::AssertionFailedError) {assert_contains @a, 'wtf'}
|
||||
assert_does_not_contain @a, 'wtf'
|
||||
end
|
||||
|
||||
should "be the same as another array, ordered differently" do
|
||||
assert_same_elements(@a, [3, "def", "abc"])
|
||||
assert_raises(Test::Unit::AssertionFailedError) do
|
||||
assert_same_elements(@a, [3, 3, "def", "abc"])
|
||||
end
|
||||
assert_raises(Test::Unit::AssertionFailedError) do
|
||||
assert_same_elements([@a, "abc"].flatten, [3, 3, "def", "abc"])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
26
vendor/plugins/shoulda/test/other/private_helpers_test.rb
vendored
Normal file
26
vendor/plugins/shoulda/test/other/private_helpers_test.rb
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
||||
|
||||
class PrivateHelpersTest < Test::Unit::TestCase # :nodoc:
|
||||
include ThoughtBot::Shoulda::ActiveRecord
|
||||
context "get_options!" do
|
||||
should "remove opts from args" do
|
||||
args = [:a, :b, {}]
|
||||
get_options!(args)
|
||||
assert_equal [:a, :b], args
|
||||
end
|
||||
|
||||
should "return wanted opts in order" do
|
||||
args = [{:one => 1, :two => 2}]
|
||||
one, two = get_options!(args, :one, :two)
|
||||
assert_equal 1, one
|
||||
assert_equal 2, two
|
||||
end
|
||||
|
||||
should "raise ArgumentError if given unwanted option" do
|
||||
args = [{:one => 1, :two => 2}]
|
||||
assert_raises ArgumentError do
|
||||
get_options!(args, :one)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
10
vendor/plugins/shoulda/test/rails_root/Rakefile
vendored
Normal file
10
vendor/plugins/shoulda/test/rails_root/Rakefile
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
||||
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
||||
|
||||
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
|
||||
|
||||
require 'rake'
|
||||
require 'rake/testtask'
|
||||
require 'rake/rdoctask'
|
||||
|
||||
require 'tasks/rails'
|
||||
25
vendor/plugins/shoulda/test/rails_root/app/controllers/application.rb
vendored
Normal file
25
vendor/plugins/shoulda/test/rails_root/app/controllers/application.rb
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
# Filters added to this controller apply to all controllers in the application.
|
||||
# Likewise, all the methods added will be available for all controllers.
|
||||
|
||||
class ApplicationController < ActionController::Base
|
||||
# Pick a unique cookie name to distinguish our session data from others'
|
||||
session :session_key => '_rails_root_session_id'
|
||||
|
||||
def ensure_logged_in
|
||||
unless session[:logged_in]
|
||||
respond_to do |accepts|
|
||||
accepts.html do
|
||||
flash[:error] = 'What do you think you\'re doing?'
|
||||
redirect_to '/'
|
||||
end
|
||||
accepts.xml do
|
||||
headers["Status"] = "Unauthorized"
|
||||
headers["WWW-Authenticate"] = %(Basic realm="Web Password")
|
||||
render :text => "Couldn't authenticate you", :status => '401 Unauthorized'
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
78
vendor/plugins/shoulda/test/rails_root/app/controllers/posts_controller.rb
vendored
Normal file
78
vendor/plugins/shoulda/test/rails_root/app/controllers/posts_controller.rb
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
class PostsController < ApplicationController
|
||||
before_filter :ensure_logged_in
|
||||
before_filter :load_user
|
||||
|
||||
def index
|
||||
@posts = @user.posts
|
||||
|
||||
respond_to do |format|
|
||||
format.html # index.rhtml
|
||||
format.xml { render :xml => @posts.to_xml }
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
@post = @user.posts.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
format.html # show.rhtml
|
||||
format.xml { render :xml => @post.to_xml }
|
||||
end
|
||||
end
|
||||
|
||||
def new
|
||||
@post = @user.posts.build
|
||||
end
|
||||
|
||||
def edit
|
||||
@post = @user.posts.find(params[:id])
|
||||
end
|
||||
|
||||
def create
|
||||
@post = @user.posts.build(params[:post])
|
||||
|
||||
respond_to do |format|
|
||||
if @post.save
|
||||
flash[:notice] = 'Post was successfully created.'
|
||||
format.html { redirect_to post_url(@post.user, @post) }
|
||||
format.xml { head :created, :location => post_url(@post.user, @post) }
|
||||
else
|
||||
format.html { render :action => "new" }
|
||||
format.xml { render :xml => @post.errors.to_xml }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
@post = @user.posts.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
if @post.update_attributes(params[:post])
|
||||
flash[:notice] = 'Post was successfully updated.'
|
||||
format.html { redirect_to post_url(@post.user, @post) }
|
||||
format.xml { head :ok }
|
||||
else
|
||||
format.html { render :action => "edit" }
|
||||
format.xml { render :xml => @post.errors.to_xml }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@post = @user.posts.find(params[:id])
|
||||
@post.destroy
|
||||
|
||||
flash[:notice] = "Post was removed"
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to posts_url(@post.user) }
|
||||
format.xml { head :ok }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_user
|
||||
@user = User.find(params[:user_id])
|
||||
end
|
||||
end
|
||||
81
vendor/plugins/shoulda/test/rails_root/app/controllers/users_controller.rb
vendored
Normal file
81
vendor/plugins/shoulda/test/rails_root/app/controllers/users_controller.rb
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
class UsersController < ApplicationController
|
||||
# GET /users
|
||||
# GET /users.xml
|
||||
def index
|
||||
@users = User.find(:all)
|
||||
|
||||
respond_to do |format|
|
||||
format.html # index.rhtml
|
||||
format.xml { render :xml => @users.to_xml }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /users/1
|
||||
# GET /users/1.xml
|
||||
def show
|
||||
@user = User.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
format.html # show.rhtml
|
||||
format.xml { render :xml => @user.to_xml }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /users/new
|
||||
def new
|
||||
@user = User.new
|
||||
end
|
||||
|
||||
# GET /users/1;edit
|
||||
def edit
|
||||
@user = User.find(params[:id])
|
||||
end
|
||||
|
||||
# POST /users
|
||||
# POST /users.xml
|
||||
def create
|
||||
@user = User.new(params[:user])
|
||||
|
||||
respond_to do |format|
|
||||
if @user.save
|
||||
flash[:notice] = 'User was successfully created.'
|
||||
format.html { redirect_to user_url(@user) }
|
||||
format.xml { head :created, :location => user_url(@user) }
|
||||
else
|
||||
format.html { render :action => "new" }
|
||||
format.xml { render :xml => @user.errors.to_xml }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PUT /users/1
|
||||
# PUT /users/1.xml
|
||||
def update
|
||||
@user = User.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
if @user.update_attributes(params[:user])
|
||||
flash[:notice] = 'User was successfully updated.'
|
||||
format.html { redirect_to user_url(@user) }
|
||||
format.xml { head :ok }
|
||||
else
|
||||
format.html { render :action => "edit" }
|
||||
format.xml { render :xml => @user.errors.to_xml }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /users/1
|
||||
# DELETE /users/1.xml
|
||||
def destroy
|
||||
@user = User.find(params[:id])
|
||||
@user.destroy
|
||||
|
||||
flash[:notice] = "User was removed"
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to users_url }
|
||||
format.xml { head :ok }
|
||||
end
|
||||
end
|
||||
end
|
||||
3
vendor/plugins/shoulda/test/rails_root/app/helpers/application_helper.rb
vendored
Normal file
3
vendor/plugins/shoulda/test/rails_root/app/helpers/application_helper.rb
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# Methods added to this helper will be available to all templates in the application.
|
||||
module ApplicationHelper
|
||||
end
|
||||
2
vendor/plugins/shoulda/test/rails_root/app/helpers/posts_helper.rb
vendored
Normal file
2
vendor/plugins/shoulda/test/rails_root/app/helpers/posts_helper.rb
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
module PostsHelper
|
||||
end
|
||||
2
vendor/plugins/shoulda/test/rails_root/app/helpers/users_helper.rb
vendored
Normal file
2
vendor/plugins/shoulda/test/rails_root/app/helpers/users_helper.rb
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
module UsersHelper
|
||||
end
|
||||
3
vendor/plugins/shoulda/test/rails_root/app/models/dog.rb
vendored
Normal file
3
vendor/plugins/shoulda/test/rails_root/app/models/dog.rb
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
class Dog < ActiveRecord::Base
|
||||
belongs_to :user, :foreign_key => :owner_id
|
||||
end
|
||||
11
vendor/plugins/shoulda/test/rails_root/app/models/post.rb
vendored
Normal file
11
vendor/plugins/shoulda/test/rails_root/app/models/post.rb
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
class Post < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :owner, :foreign_key => :user_id, :class_name => 'User'
|
||||
has_many :taggings
|
||||
has_many :tags, :through => :taggings
|
||||
|
||||
validates_uniqueness_of :title
|
||||
validates_presence_of :title
|
||||
validates_presence_of :body, :message => 'Seriously... wtf'
|
||||
validates_numericality_of :user_id
|
||||
end
|
||||
4
vendor/plugins/shoulda/test/rails_root/app/models/tag.rb
vendored
Normal file
4
vendor/plugins/shoulda/test/rails_root/app/models/tag.rb
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
class Tag < ActiveRecord::Base
|
||||
has_many :taggings
|
||||
has_many :posts, :through => :taggings
|
||||
end
|
||||
4
vendor/plugins/shoulda/test/rails_root/app/models/tagging.rb
vendored
Normal file
4
vendor/plugins/shoulda/test/rails_root/app/models/tagging.rb
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
class Tagging < ActiveRecord::Base
|
||||
belongs_to :post
|
||||
belongs_to :tag
|
||||
end
|
||||
9
vendor/plugins/shoulda/test/rails_root/app/models/user.rb
vendored
Normal file
9
vendor/plugins/shoulda/test/rails_root/app/models/user.rb
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
class User < ActiveRecord::Base
|
||||
has_many :posts
|
||||
has_many :dogs, :foreign_key => :owner_id
|
||||
|
||||
attr_protected :password
|
||||
validates_format_of :email, :with => /\w*@\w*.com/
|
||||
validates_length_of :email, :in => 1..100
|
||||
validates_inclusion_of :age, :in => 1..100
|
||||
end
|
||||
17
vendor/plugins/shoulda/test/rails_root/app/views/layouts/posts.rhtml
vendored
Normal file
17
vendor/plugins/shoulda/test/rails_root/app/views/layouts/posts.rhtml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
||||
<title>Posts: <%= controller.action_name %></title>
|
||||
<%= stylesheet_link_tag 'scaffold' %>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p style="color: green"><%= flash[:notice] %></p>
|
||||
|
||||
<%= yield %>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
17
vendor/plugins/shoulda/test/rails_root/app/views/layouts/users.rhtml
vendored
Normal file
17
vendor/plugins/shoulda/test/rails_root/app/views/layouts/users.rhtml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
||||
<title>Users: <%= controller.action_name %></title>
|
||||
<%= stylesheet_link_tag 'scaffold' %>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p style="color: green"><%= flash[:notice] %></p>
|
||||
|
||||
<%= yield %>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
27
vendor/plugins/shoulda/test/rails_root/app/views/posts/edit.rhtml
vendored
Normal file
27
vendor/plugins/shoulda/test/rails_root/app/views/posts/edit.rhtml
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<h1>Editing post</h1>
|
||||
|
||||
<%= error_messages_for :post %>
|
||||
|
||||
<% form_for(:post, :url => post_path(@post.user, @post), :html => { :method => :put }) do |f| %>
|
||||
<p>
|
||||
<b>User</b><br />
|
||||
<%= f.text_field :user_id %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Title</b><br />
|
||||
<%= f.text_field :title %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Body</b><br />
|
||||
<%= f.text_area :body %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= submit_tag "Update" %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Show', post_path(@post.user, @post) %> |
|
||||
<%= link_to 'Back', posts_path(@post.user) %>
|
||||
24
vendor/plugins/shoulda/test/rails_root/app/views/posts/index.rhtml
vendored
Normal file
24
vendor/plugins/shoulda/test/rails_root/app/views/posts/index.rhtml
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
<h1>Listing posts</h1>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>User</th>
|
||||
<th>Title</th>
|
||||
<th>Body</th>
|
||||
</tr>
|
||||
|
||||
<% for post in @posts %>
|
||||
<tr>
|
||||
<td><%=h post.user_id %></td>
|
||||
<td><%=h post.title %></td>
|
||||
<td><%=h post.body %></td>
|
||||
<td><%= link_to 'Show', post_path(post.user, post) %></td>
|
||||
<td><%= link_to 'Edit', edit_post_path(post.user, post) %></td>
|
||||
<td><%= link_to 'Destroy', post_path(post.user, post), :confirm => 'Are you sure?', :method => :delete %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<br />
|
||||
|
||||
<%= link_to 'New post', new_post_path(post.user) %>
|
||||
26
vendor/plugins/shoulda/test/rails_root/app/views/posts/new.rhtml
vendored
Normal file
26
vendor/plugins/shoulda/test/rails_root/app/views/posts/new.rhtml
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
<h1>New post</h1>
|
||||
|
||||
<%= error_messages_for :post %>
|
||||
|
||||
<% form_for(:post, :url => posts_path(@user)) do |f| %>
|
||||
<p>
|
||||
<b>User</b><br />
|
||||
<%= f.text_field :user_id %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Title</b><br />
|
||||
<%= f.text_field :title %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Body</b><br />
|
||||
<%= f.text_area :body %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= submit_tag "Create" %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Back', posts_path(@user) %>
|
||||
18
vendor/plugins/shoulda/test/rails_root/app/views/posts/show.rhtml
vendored
Normal file
18
vendor/plugins/shoulda/test/rails_root/app/views/posts/show.rhtml
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
<p>
|
||||
<b>User:</b>
|
||||
<%=h @post.user_id %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Title:</b>
|
||||
<%=h @post.title %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Body:</b>
|
||||
<%=h @post.body %>
|
||||
</p>
|
||||
|
||||
|
||||
<%= link_to 'Edit', edit_post_path(@post.user, @post) %> |
|
||||
<%= link_to 'Back', posts_path(@post.user) %>
|
||||
22
vendor/plugins/shoulda/test/rails_root/app/views/users/edit.rhtml
vendored
Normal file
22
vendor/plugins/shoulda/test/rails_root/app/views/users/edit.rhtml
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
<h1>Editing user</h1>
|
||||
|
||||
<%= error_messages_for :user %>
|
||||
|
||||
<% form_for(:user, :url => user_path(@user), :html => { :method => :put }) do |f| %>
|
||||
<p>
|
||||
<b>Email</b><br />
|
||||
<%= f.text_field :email %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Age</b><br />
|
||||
<%= f.text_field :age %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= submit_tag "Update" %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Show', user_path(@user) %> |
|
||||
<%= link_to 'Back', users_path %>
|
||||
22
vendor/plugins/shoulda/test/rails_root/app/views/users/index.rhtml
vendored
Normal file
22
vendor/plugins/shoulda/test/rails_root/app/views/users/index.rhtml
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
<h1>Listing users</h1>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Email</th>
|
||||
<th>Age</th>
|
||||
</tr>
|
||||
|
||||
<% for user in @users %>
|
||||
<tr>
|
||||
<td><%=h user.email %></td>
|
||||
<td><%=h user.age %></td>
|
||||
<td><%= link_to 'Show', user_path(user) %></td>
|
||||
<td><%= link_to 'Edit', edit_user_path(user) %></td>
|
||||
<td><%= link_to 'Destroy', user_path(user), :confirm => 'Are you sure?', :method => :delete %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
<br />
|
||||
|
||||
<%= link_to 'New user', new_user_path %>
|
||||
21
vendor/plugins/shoulda/test/rails_root/app/views/users/new.rhtml
vendored
Normal file
21
vendor/plugins/shoulda/test/rails_root/app/views/users/new.rhtml
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
<h1>New user</h1>
|
||||
|
||||
<%= error_messages_for :user %>
|
||||
|
||||
<% form_for(:user, :url => users_path) do |f| %>
|
||||
<p>
|
||||
<b>Email</b><br />
|
||||
<%= f.text_field :email %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Age</b><br />
|
||||
<%= f.text_field :age %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= submit_tag "Create" %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to 'Back', users_path %>
|
||||
13
vendor/plugins/shoulda/test/rails_root/app/views/users/show.rhtml
vendored
Normal file
13
vendor/plugins/shoulda/test/rails_root/app/views/users/show.rhtml
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<p>
|
||||
<b>Email:</b>
|
||||
<%=h @user.email %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Age:</b>
|
||||
<%=h @user.age %>
|
||||
</p>
|
||||
|
||||
|
||||
<%= link_to 'Edit', edit_user_path(@user) %> |
|
||||
<%= link_to 'Back', users_path %>
|
||||
45
vendor/plugins/shoulda/test/rails_root/config/boot.rb
vendored
Normal file
45
vendor/plugins/shoulda/test/rails_root/config/boot.rb
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
# Don't change this file. Configuration is done in config/environment.rb and config/environments/*.rb
|
||||
|
||||
unless defined?(RAILS_ROOT)
|
||||
root_path = File.join(File.dirname(__FILE__), '..')
|
||||
|
||||
unless RUBY_PLATFORM =~ /(:?mswin|mingw)/
|
||||
require 'pathname'
|
||||
root_path = Pathname.new(root_path).cleanpath(true).to_s
|
||||
end
|
||||
|
||||
RAILS_ROOT = root_path
|
||||
end
|
||||
|
||||
unless defined?(Rails::Initializer)
|
||||
if File.directory?("#{RAILS_ROOT}/vendor/rails")
|
||||
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
|
||||
else
|
||||
require 'rubygems'
|
||||
|
||||
environment_without_comments = IO.readlines(File.dirname(__FILE__) + '/environment.rb').reject { |l| l =~ /^#/ }.join
|
||||
environment_without_comments =~ /[^#]RAILS_GEM_VERSION = '([\d.]+)'/
|
||||
rails_gem_version = $1
|
||||
|
||||
if version = defined?(RAILS_GEM_VERSION) ? RAILS_GEM_VERSION : rails_gem_version
|
||||
# Asking for 1.1.6 will give you 1.1.6.5206, if available -- makes it easier to use beta gems
|
||||
rails_gem = Gem.cache.search('rails', "~>#{version}.0").sort_by { |g| g.version.version }.last
|
||||
|
||||
if rails_gem
|
||||
gem "rails", "=#{rails_gem.version.version}"
|
||||
require rails_gem.full_gem_path + '/lib/initializer'
|
||||
else
|
||||
STDERR.puts %(Cannot find gem for Rails ~>#{version}.0:
|
||||
Install the missing gem with 'gem install -v=#{version} rails', or
|
||||
change environment.rb to define RAILS_GEM_VERSION with your desired version.
|
||||
)
|
||||
exit 1
|
||||
end
|
||||
else
|
||||
gem "rails"
|
||||
require 'initializer'
|
||||
end
|
||||
end
|
||||
|
||||
Rails::Initializer.run(:set_load_path)
|
||||
end
|
||||
4
vendor/plugins/shoulda/test/rails_root/config/database.yml
vendored
Normal file
4
vendor/plugins/shoulda/test/rails_root/config/database.yml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
sqlite3:
|
||||
:adapter: sqlite3
|
||||
# :dbfile: db/sqlite3.db
|
||||
:dbfile: ":memory:"
|
||||
19
vendor/plugins/shoulda/test/rails_root/config/environment.rb
vendored
Normal file
19
vendor/plugins/shoulda/test/rails_root/config/environment.rb
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
# Specifies gem version of Rails to use when vendor/rails is not present
|
||||
old_verbose, $VERBOSE = $VERBOSE, nil
|
||||
RAILS_GEM_VERSION = '1.2.3'
|
||||
$VERBOSE = old_verbose
|
||||
|
||||
require File.join(File.dirname(__FILE__), 'boot')
|
||||
|
||||
Rails::Initializer.run do |config|
|
||||
# Someday, I'm going to find a way of getting rid of that symlink...
|
||||
# config.plugin_paths = ['../../../']
|
||||
# config.plugins = [:shoulda]
|
||||
config.log_level = :debug
|
||||
config.cache_classes = false
|
||||
config.whiny_nils = true
|
||||
config.breakpoint_server = true
|
||||
config.load_paths << File.join(File.dirname(__FILE__), *%w{.. .. .. lib})
|
||||
end
|
||||
|
||||
Dependencies.log_activity = true
|
||||
0
vendor/plugins/shoulda/test/rails_root/config/environments/sqlite3.rb
vendored
Normal file
0
vendor/plugins/shoulda/test/rails_root/config/environments/sqlite3.rb
vendored
Normal file
5
vendor/plugins/shoulda/test/rails_root/config/routes.rb
vendored
Normal file
5
vendor/plugins/shoulda/test/rails_root/config/routes.rb
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
ActionController::Routing::Routes.draw do |map|
|
||||
map.resources :users do |user|
|
||||
user.resources :posts
|
||||
end
|
||||
end
|
||||
13
vendor/plugins/shoulda/test/rails_root/db/migrate/001_create_users.rb
vendored
Normal file
13
vendor/plugins/shoulda/test/rails_root/db/migrate/001_create_users.rb
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
class CreateUsers < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :users do |t|
|
||||
t.column :name, :string
|
||||
t.column :email, :string
|
||||
t.column :age, :integer
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :users
|
||||
end
|
||||
end
|
||||
13
vendor/plugins/shoulda/test/rails_root/db/migrate/002_create_posts.rb
vendored
Normal file
13
vendor/plugins/shoulda/test/rails_root/db/migrate/002_create_posts.rb
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
class CreatePosts < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :posts do |t|
|
||||
t.column :user_id, :integer
|
||||
t.column :title, :string
|
||||
t.column :body, :text
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :posts
|
||||
end
|
||||
end
|
||||
12
vendor/plugins/shoulda/test/rails_root/db/migrate/003_create_taggings.rb
vendored
Normal file
12
vendor/plugins/shoulda/test/rails_root/db/migrate/003_create_taggings.rb
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
class CreateTaggings < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :taggings do |t|
|
||||
t.column :post_id, :integer
|
||||
t.column :tag_id, :integer
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :taggings
|
||||
end
|
||||
end
|
||||
11
vendor/plugins/shoulda/test/rails_root/db/migrate/004_create_tags.rb
vendored
Normal file
11
vendor/plugins/shoulda/test/rails_root/db/migrate/004_create_tags.rb
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
class CreateTags < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :tags do |t|
|
||||
t.column :name, :string
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :tags
|
||||
end
|
||||
end
|
||||
11
vendor/plugins/shoulda/test/rails_root/db/migrate/005_create_dogs.rb
vendored
Normal file
11
vendor/plugins/shoulda/test/rails_root/db/migrate/005_create_dogs.rb
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
class CreateDogs < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :dogs do |t|
|
||||
t.column :owner_id, :integer
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :dogs
|
||||
end
|
||||
end
|
||||
0
vendor/plugins/shoulda/test/rails_root/db/schema.rb
vendored
Normal file
0
vendor/plugins/shoulda/test/rails_root/db/schema.rb
vendored
Normal file
2
vendor/plugins/shoulda/test/rails_root/doc/README_FOR_APP
vendored
Normal file
2
vendor/plugins/shoulda/test/rails_root/doc/README_FOR_APP
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
Use this README file to introduce your application and point to useful places in the API for learning more.
|
||||
Run "rake appdoc" to generate API documentation for your models and controllers.
|
||||
40
vendor/plugins/shoulda/test/rails_root/public/.htaccess
vendored
Normal file
40
vendor/plugins/shoulda/test/rails_root/public/.htaccess
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
# General Apache options
|
||||
AddHandler fastcgi-script .fcgi
|
||||
AddHandler cgi-script .cgi
|
||||
Options +FollowSymLinks +ExecCGI
|
||||
|
||||
# If you don't want Rails to look in certain directories,
|
||||
# use the following rewrite rules so that Apache won't rewrite certain requests
|
||||
#
|
||||
# Example:
|
||||
# RewriteCond %{REQUEST_URI} ^/notrails.*
|
||||
# RewriteRule .* - [L]
|
||||
|
||||
# Redirect all requests not available on the filesystem to Rails
|
||||
# By default the cgi dispatcher is used which is very slow
|
||||
#
|
||||
# For better performance replace the dispatcher with the fastcgi one
|
||||
#
|
||||
# Example:
|
||||
# RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
|
||||
RewriteEngine On
|
||||
|
||||
# If your Rails application is accessed via an Alias directive,
|
||||
# then you MUST also set the RewriteBase in this htaccess file.
|
||||
#
|
||||
# Example:
|
||||
# Alias /myrailsapp /path/to/myrailsapp/public
|
||||
# RewriteBase /myrailsapp
|
||||
|
||||
RewriteRule ^$ index.html [QSA]
|
||||
RewriteRule ^([^.]+)$ $1.html [QSA]
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
|
||||
|
||||
# In case Rails experiences terminal errors
|
||||
# Instead of displaying this message you can supply a file here which will be rendered instead
|
||||
#
|
||||
# Example:
|
||||
# ErrorDocument 500 /500.html
|
||||
|
||||
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
|
||||
30
vendor/plugins/shoulda/test/rails_root/public/404.html
vendored
Normal file
30
vendor/plugins/shoulda/test/rails_root/public/404.html
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
||||
<title>The page you were looking for doesn't exist (404)</title>
|
||||
<style type="text/css">
|
||||
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
|
||||
div.dialog {
|
||||
width: 25em;
|
||||
padding: 0 4em;
|
||||
margin: 4em auto 0 auto;
|
||||
border: 1px solid #ccc;
|
||||
border-right-color: #999;
|
||||
border-bottom-color: #999;
|
||||
}
|
||||
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<!-- This file lives in public/404.html -->
|
||||
<div class="dialog">
|
||||
<h1>The page you were looking for doesn't exist.</h1>
|
||||
<p>You may have mistyped the address or the page may have moved.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
30
vendor/plugins/shoulda/test/rails_root/public/500.html
vendored
Normal file
30
vendor/plugins/shoulda/test/rails_root/public/500.html
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
||||
<title>We're sorry, but something went wrong</title>
|
||||
<style type="text/css">
|
||||
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
|
||||
div.dialog {
|
||||
width: 25em;
|
||||
padding: 0 4em;
|
||||
margin: 4em auto 0 auto;
|
||||
border: 1px solid #ccc;
|
||||
border-right-color: #999;
|
||||
border-bottom-color: #999;
|
||||
}
|
||||
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<!-- This file lives in public/500.html -->
|
||||
<div class="dialog">
|
||||
<h1>We're sorry, but something went wrong.</h1>
|
||||
<p>We've been notified about this issue and we'll take a look at it shortly.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
10
vendor/plugins/shoulda/test/rails_root/public/dispatch.cgi
vendored
Executable file
10
vendor/plugins/shoulda/test/rails_root/public/dispatch.cgi
vendored
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/opt/local/bin/ruby
|
||||
|
||||
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
|
||||
|
||||
# If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
|
||||
# "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
|
||||
require "dispatcher"
|
||||
|
||||
ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
|
||||
Dispatcher.dispatch
|
||||
24
vendor/plugins/shoulda/test/rails_root/public/dispatch.fcgi
vendored
Executable file
24
vendor/plugins/shoulda/test/rails_root/public/dispatch.fcgi
vendored
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/opt/local/bin/ruby
|
||||
#
|
||||
# You may specify the path to the FastCGI crash log (a log of unhandled
|
||||
# exceptions which forced the FastCGI instance to exit, great for debugging)
|
||||
# and the number of requests to process before running garbage collection.
|
||||
#
|
||||
# By default, the FastCGI crash log is RAILS_ROOT/log/fastcgi.crash.log
|
||||
# and the GC period is nil (turned off). A reasonable number of requests
|
||||
# could range from 10-100 depending on the memory footprint of your app.
|
||||
#
|
||||
# Example:
|
||||
# # Default log path, normal GC behavior.
|
||||
# RailsFCGIHandler.process!
|
||||
#
|
||||
# # Default log path, 50 requests between GC.
|
||||
# RailsFCGIHandler.process! nil, 50
|
||||
#
|
||||
# # Custom log path, normal GC behavior.
|
||||
# RailsFCGIHandler.process! '/var/log/myapp_fcgi_crash.log'
|
||||
#
|
||||
require File.dirname(__FILE__) + "/../config/environment"
|
||||
require 'fcgi_handler'
|
||||
|
||||
RailsFCGIHandler.process!
|
||||
10
vendor/plugins/shoulda/test/rails_root/public/dispatch.rb
vendored
Executable file
10
vendor/plugins/shoulda/test/rails_root/public/dispatch.rb
vendored
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/opt/local/bin/ruby
|
||||
|
||||
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
|
||||
|
||||
# If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
|
||||
# "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
|
||||
require "dispatcher"
|
||||
|
||||
ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
|
||||
Dispatcher.dispatch
|
||||
0
vendor/plugins/shoulda/test/rails_root/public/favicon.ico
vendored
Normal file
0
vendor/plugins/shoulda/test/rails_root/public/favicon.ico
vendored
Normal file
BIN
vendor/plugins/shoulda/test/rails_root/public/images/rails.png
vendored
Normal file
BIN
vendor/plugins/shoulda/test/rails_root/public/images/rails.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
277
vendor/plugins/shoulda/test/rails_root/public/index.html
vendored
Normal file
277
vendor/plugins/shoulda/test/rails_root/public/index.html
vendored
Normal file
@@ -0,0 +1,277 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
|
||||
<title>Ruby on Rails: Welcome aboard</title>
|
||||
<style type="text/css" media="screen">
|
||||
body {
|
||||
margin: 0;
|
||||
margin-bottom: 25px;
|
||||
padding: 0;
|
||||
background-color: #f0f0f0;
|
||||
font-family: "Lucida Grande", "Bitstream Vera Sans", "Verdana";
|
||||
font-size: 13px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 28px;
|
||||
color: #000;
|
||||
}
|
||||
|
||||
a {color: #03c}
|
||||
a:hover {
|
||||
background-color: #03c;
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
|
||||
#page {
|
||||
background-color: #f0f0f0;
|
||||
width: 750px;
|
||||
margin: 0;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
||||
|
||||
#content {
|
||||
float: left;
|
||||
background-color: white;
|
||||
border: 3px solid #aaa;
|
||||
border-top: none;
|
||||
padding: 25px;
|
||||
width: 500px;
|
||||
}
|
||||
|
||||
#sidebar {
|
||||
float: right;
|
||||
width: 175px;
|
||||
}
|
||||
|
||||
#footer {
|
||||
clear: both;
|
||||
}
|
||||
|
||||
|
||||
#header, #about, #getting-started {
|
||||
padding-left: 75px;
|
||||
padding-right: 30px;
|
||||
}
|
||||
|
||||
|
||||
#header {
|
||||
background-image: url("images/rails.png");
|
||||
background-repeat: no-repeat;
|
||||
background-position: top left;
|
||||
height: 64px;
|
||||
}
|
||||
#header h1, #header h2 {margin: 0}
|
||||
#header h2 {
|
||||
color: #888;
|
||||
font-weight: normal;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
|
||||
#about h3 {
|
||||
margin: 0;
|
||||
margin-bottom: 10px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
#about-content {
|
||||
background-color: #ffd;
|
||||
border: 1px solid #fc0;
|
||||
margin-left: -11px;
|
||||
}
|
||||
#about-content table {
|
||||
margin-top: 10px;
|
||||
margin-bottom: 10px;
|
||||
font-size: 11px;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
#about-content td {
|
||||
padding: 10px;
|
||||
padding-top: 3px;
|
||||
padding-bottom: 3px;
|
||||
}
|
||||
#about-content td.name {color: #555}
|
||||
#about-content td.value {color: #000}
|
||||
|
||||
#about-content.failure {
|
||||
background-color: #fcc;
|
||||
border: 1px solid #f00;
|
||||
}
|
||||
#about-content.failure p {
|
||||
margin: 0;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
|
||||
#getting-started {
|
||||
border-top: 1px solid #ccc;
|
||||
margin-top: 25px;
|
||||
padding-top: 15px;
|
||||
}
|
||||
#getting-started h1 {
|
||||
margin: 0;
|
||||
font-size: 20px;
|
||||
}
|
||||
#getting-started h2 {
|
||||
margin: 0;
|
||||
font-size: 14px;
|
||||
font-weight: normal;
|
||||
color: #333;
|
||||
margin-bottom: 25px;
|
||||
}
|
||||
#getting-started ol {
|
||||
margin-left: 0;
|
||||
padding-left: 0;
|
||||
}
|
||||
#getting-started li {
|
||||
font-size: 18px;
|
||||
color: #888;
|
||||
margin-bottom: 25px;
|
||||
}
|
||||
#getting-started li h2 {
|
||||
margin: 0;
|
||||
font-weight: normal;
|
||||
font-size: 18px;
|
||||
color: #333;
|
||||
}
|
||||
#getting-started li p {
|
||||
color: #555;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
|
||||
#search {
|
||||
margin: 0;
|
||||
padding-top: 10px;
|
||||
padding-bottom: 10px;
|
||||
font-size: 11px;
|
||||
}
|
||||
#search input {
|
||||
font-size: 11px;
|
||||
margin: 2px;
|
||||
}
|
||||
#search-text {width: 170px}
|
||||
|
||||
|
||||
#sidebar ul {
|
||||
margin-left: 0;
|
||||
padding-left: 0;
|
||||
}
|
||||
#sidebar ul h3 {
|
||||
margin-top: 25px;
|
||||
font-size: 16px;
|
||||
padding-bottom: 10px;
|
||||
border-bottom: 1px solid #ccc;
|
||||
}
|
||||
#sidebar li {
|
||||
list-style-type: none;
|
||||
}
|
||||
#sidebar ul.links li {
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
</style>
|
||||
<script type="text/javascript" src="javascripts/prototype.js"></script>
|
||||
<script type="text/javascript" src="javascripts/effects.js"></script>
|
||||
<script type="text/javascript">
|
||||
function about() {
|
||||
if (Element.empty('about-content')) {
|
||||
new Ajax.Updater('about-content', 'rails/info/properties', {
|
||||
method: 'get',
|
||||
onFailure: function() {Element.classNames('about-content').add('failure')},
|
||||
onComplete: function() {new Effect.BlindDown('about-content', {duration: 0.25})}
|
||||
});
|
||||
} else {
|
||||
new Effect[Element.visible('about-content') ?
|
||||
'BlindUp' : 'BlindDown']('about-content', {duration: 0.25});
|
||||
}
|
||||
}
|
||||
|
||||
window.onload = function() {
|
||||
$('search-text').value = '';
|
||||
$('search').onsubmit = function() {
|
||||
$('search-text').value = 'site:rubyonrails.org ' + $F('search-text');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="page">
|
||||
<div id="sidebar">
|
||||
<ul id="sidebar-items">
|
||||
<li>
|
||||
<form id="search" action="http://www.google.com/search" method="get">
|
||||
<input type="hidden" name="hl" value="en" />
|
||||
<input type="text" id="search-text" name="q" value="site:rubyonrails.org " />
|
||||
<input type="submit" value="Search" /> the Rails site
|
||||
</form>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<h3>Join the community</h3>
|
||||
<ul class="links">
|
||||
<li><a href="http://www.rubyonrails.org/">Ruby on Rails</a></li>
|
||||
<li><a href="http://weblog.rubyonrails.org/">Official weblog</a></li>
|
||||
<li><a href="http://lists.rubyonrails.org/">Mailing lists</a></li>
|
||||
<li><a href="http://wiki.rubyonrails.org/rails/pages/IRC">IRC channel</a></li>
|
||||
<li><a href="http://wiki.rubyonrails.org/">Wiki</a></li>
|
||||
<li><a href="http://dev.rubyonrails.org/">Bug tracker</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<h3>Browse the documentation</h3>
|
||||
<ul class="links">
|
||||
<li><a href="http://api.rubyonrails.org/">Rails API</a></li>
|
||||
<li><a href="http://stdlib.rubyonrails.org/">Ruby standard library</a></li>
|
||||
<li><a href="http://corelib.rubyonrails.org/">Ruby core</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div id="content">
|
||||
<div id="header">
|
||||
<h1>Welcome aboard</h1>
|
||||
<h2>You’re riding the Rails!</h2>
|
||||
</div>
|
||||
|
||||
<div id="about">
|
||||
<h3><a href="rails/info/properties" onclick="about(); return false">About your application’s environment</a></h3>
|
||||
<div id="about-content" style="display: none"></div>
|
||||
</div>
|
||||
|
||||
<div id="getting-started">
|
||||
<h1>Getting started</h1>
|
||||
<h2>Here’s how to get rolling:</h2>
|
||||
|
||||
<ol>
|
||||
<li>
|
||||
<h2>Create your databases and edit <tt>config/database.yml</tt></h2>
|
||||
<p>Rails needs to know your login and password.</p>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<h2>Use <tt>script/generate</tt> to create your models and controllers</h2>
|
||||
<p>To see all available options, run it without parameters.</p>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<h2>Set up a default route and remove or rename this file</h2>
|
||||
<p>Routes are setup in config/routes.rb.</p>
|
||||
</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="footer"> </div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
2
vendor/plugins/shoulda/test/rails_root/public/javascripts/application.js
vendored
Normal file
2
vendor/plugins/shoulda/test/rails_root/public/javascripts/application.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
// Place your application-specific JavaScript functions and classes here
|
||||
// This file is automatically included by javascript_include_tag :defaults
|
||||
833
vendor/plugins/shoulda/test/rails_root/public/javascripts/controls.js
vendored
Normal file
833
vendor/plugins/shoulda/test/rails_root/public/javascripts/controls.js
vendored
Normal file
@@ -0,0 +1,833 @@
|
||||
// Copyright (c) 2005, 2006 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
|
||||
// (c) 2005, 2006 Ivan Krstic (http://blogs.law.harvard.edu/ivan)
|
||||
// (c) 2005, 2006 Jon Tirsen (http://www.tirsen.com)
|
||||
// Contributors:
|
||||
// Richard Livsey
|
||||
// Rahul Bhargava
|
||||
// Rob Wills
|
||||
//
|
||||
// script.aculo.us is freely distributable under the terms of an MIT-style license.
|
||||
// For details, see the script.aculo.us web site: http://script.aculo.us/
|
||||
|
||||
// Autocompleter.Base handles all the autocompletion functionality
|
||||
// that's independent of the data source for autocompletion. This
|
||||
// includes drawing the autocompletion menu, observing keyboard
|
||||
// and mouse events, and similar.
|
||||
//
|
||||
// Specific autocompleters need to provide, at the very least,
|
||||
// a getUpdatedChoices function that will be invoked every time
|
||||
// the text inside the monitored textbox changes. This method
|
||||
// should get the text for which to provide autocompletion by
|
||||
// invoking this.getToken(), NOT by directly accessing
|
||||
// this.element.value. This is to allow incremental tokenized
|
||||
// autocompletion. Specific auto-completion logic (AJAX, etc)
|
||||
// belongs in getUpdatedChoices.
|
||||
//
|
||||
// Tokenized incremental autocompletion is enabled automatically
|
||||
// when an autocompleter is instantiated with the 'tokens' option
|
||||
// in the options parameter, e.g.:
|
||||
// new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' });
|
||||
// will incrementally autocomplete with a comma as the token.
|
||||
// Additionally, ',' in the above example can be replaced with
|
||||
// a token array, e.g. { tokens: [',', '\n'] } which
|
||||
// enables autocompletion on multiple tokens. This is most
|
||||
// useful when one of the tokens is \n (a newline), as it
|
||||
// allows smart autocompletion after linebreaks.
|
||||
|
||||
if(typeof Effect == 'undefined')
|
||||
throw("controls.js requires including script.aculo.us' effects.js library");
|
||||
|
||||
var Autocompleter = {}
|
||||
Autocompleter.Base = function() {};
|
||||
Autocompleter.Base.prototype = {
|
||||
baseInitialize: function(element, update, options) {
|
||||
this.element = $(element);
|
||||
this.update = $(update);
|
||||
this.hasFocus = false;
|
||||
this.changed = false;
|
||||
this.active = false;
|
||||
this.index = 0;
|
||||
this.entryCount = 0;
|
||||
|
||||
if(this.setOptions)
|
||||
this.setOptions(options);
|
||||
else
|
||||
this.options = options || {};
|
||||
|
||||
this.options.paramName = this.options.paramName || this.element.name;
|
||||
this.options.tokens = this.options.tokens || [];
|
||||
this.options.frequency = this.options.frequency || 0.4;
|
||||
this.options.minChars = this.options.minChars || 1;
|
||||
this.options.onShow = this.options.onShow ||
|
||||
function(element, update){
|
||||
if(!update.style.position || update.style.position=='absolute') {
|
||||
update.style.position = 'absolute';
|
||||
Position.clone(element, update, {
|
||||
setHeight: false,
|
||||
offsetTop: element.offsetHeight
|
||||
});
|
||||
}
|
||||
Effect.Appear(update,{duration:0.15});
|
||||
};
|
||||
this.options.onHide = this.options.onHide ||
|
||||
function(element, update){ new Effect.Fade(update,{duration:0.15}) };
|
||||
|
||||
if(typeof(this.options.tokens) == 'string')
|
||||
this.options.tokens = new Array(this.options.tokens);
|
||||
|
||||
this.observer = null;
|
||||
|
||||
this.element.setAttribute('autocomplete','off');
|
||||
|
||||
Element.hide(this.update);
|
||||
|
||||
Event.observe(this.element, "blur", this.onBlur.bindAsEventListener(this));
|
||||
Event.observe(this.element, "keypress", this.onKeyPress.bindAsEventListener(this));
|
||||
},
|
||||
|
||||
show: function() {
|
||||
if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update);
|
||||
if(!this.iefix &&
|
||||
(navigator.appVersion.indexOf('MSIE')>0) &&
|
||||
(navigator.userAgent.indexOf('Opera')<0) &&
|
||||
(Element.getStyle(this.update, 'position')=='absolute')) {
|
||||
new Insertion.After(this.update,
|
||||
'<iframe id="' + this.update.id + '_iefix" '+
|
||||
'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
|
||||
'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
|
||||
this.iefix = $(this.update.id+'_iefix');
|
||||
}
|
||||
if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
|
||||
},
|
||||
|
||||
fixIEOverlapping: function() {
|
||||
Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)});
|
||||
this.iefix.style.zIndex = 1;
|
||||
this.update.style.zIndex = 2;
|
||||
Element.show(this.iefix);
|
||||
},
|
||||
|
||||
hide: function() {
|
||||
this.stopIndicator();
|
||||
if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update);
|
||||
if(this.iefix) Element.hide(this.iefix);
|
||||
},
|
||||
|
||||
startIndicator: function() {
|
||||
if(this.options.indicator) Element.show(this.options.indicator);
|
||||
},
|
||||
|
||||
stopIndicator: function() {
|
||||
if(this.options.indicator) Element.hide(this.options.indicator);
|
||||
},
|
||||
|
||||
onKeyPress: function(event) {
|
||||
if(this.active)
|
||||
switch(event.keyCode) {
|
||||
case Event.KEY_TAB:
|
||||
case Event.KEY_RETURN:
|
||||
this.selectEntry();
|
||||
Event.stop(event);
|
||||
case Event.KEY_ESC:
|
||||
this.hide();
|
||||
this.active = false;
|
||||
Event.stop(event);
|
||||
return;
|
||||
case Event.KEY_LEFT:
|
||||
case Event.KEY_RIGHT:
|
||||
return;
|
||||
case Event.KEY_UP:
|
||||
this.markPrevious();
|
||||
this.render();
|
||||
if(navigator.appVersion.indexOf('AppleWebKit')>0) Event.stop(event);
|
||||
return;
|
||||
case Event.KEY_DOWN:
|
||||
this.markNext();
|
||||
this.render();
|
||||
if(navigator.appVersion.indexOf('AppleWebKit')>0) Event.stop(event);
|
||||
return;
|
||||
}
|
||||
else
|
||||
if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN ||
|
||||
(navigator.appVersion.indexOf('AppleWebKit') > 0 && event.keyCode == 0)) return;
|
||||
|
||||
this.changed = true;
|
||||
this.hasFocus = true;
|
||||
|
||||
if(this.observer) clearTimeout(this.observer);
|
||||
this.observer =
|
||||
setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
|
||||
},
|
||||
|
||||
activate: function() {
|
||||
this.changed = false;
|
||||
this.hasFocus = true;
|
||||
this.getUpdatedChoices();
|
||||
},
|
||||
|
||||
onHover: function(event) {
|
||||
var element = Event.findElement(event, 'LI');
|
||||
if(this.index != element.autocompleteIndex)
|
||||
{
|
||||
this.index = element.autocompleteIndex;
|
||||
this.render();
|
||||
}
|
||||
Event.stop(event);
|
||||
},
|
||||
|
||||
onClick: function(event) {
|
||||
var element = Event.findElement(event, 'LI');
|
||||
this.index = element.autocompleteIndex;
|
||||
this.selectEntry();
|
||||
this.hide();
|
||||
},
|
||||
|
||||
onBlur: function(event) {
|
||||
// needed to make click events working
|
||||
setTimeout(this.hide.bind(this), 250);
|
||||
this.hasFocus = false;
|
||||
this.active = false;
|
||||
},
|
||||
|
||||
render: function() {
|
||||
if(this.entryCount > 0) {
|
||||
for (var i = 0; i < this.entryCount; i++)
|
||||
this.index==i ?
|
||||
Element.addClassName(this.getEntry(i),"selected") :
|
||||
Element.removeClassName(this.getEntry(i),"selected");
|
||||
|
||||
if(this.hasFocus) {
|
||||
this.show();
|
||||
this.active = true;
|
||||
}
|
||||
} else {
|
||||
this.active = false;
|
||||
this.hide();
|
||||
}
|
||||
},
|
||||
|
||||
markPrevious: function() {
|
||||
if(this.index > 0) this.index--
|
||||
else this.index = this.entryCount-1;
|
||||
this.getEntry(this.index).scrollIntoView(true);
|
||||
},
|
||||
|
||||
markNext: function() {
|
||||
if(this.index < this.entryCount-1) this.index++
|
||||
else this.index = 0;
|
||||
this.getEntry(this.index).scrollIntoView(false);
|
||||
},
|
||||
|
||||
getEntry: function(index) {
|
||||
return this.update.firstChild.childNodes[index];
|
||||
},
|
||||
|
||||
getCurrentEntry: function() {
|
||||
return this.getEntry(this.index);
|
||||
},
|
||||
|
||||
selectEntry: function() {
|
||||
this.active = false;
|
||||
this.updateElement(this.getCurrentEntry());
|
||||
},
|
||||
|
||||
updateElement: function(selectedElement) {
|
||||
if (this.options.updateElement) {
|
||||
this.options.updateElement(selectedElement);
|
||||
return;
|
||||
}
|
||||
var value = '';
|
||||
if (this.options.select) {
|
||||
var nodes = document.getElementsByClassName(this.options.select, selectedElement) || [];
|
||||
if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select);
|
||||
} else
|
||||
value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal');
|
||||
|
||||
var lastTokenPos = this.findLastToken();
|
||||
if (lastTokenPos != -1) {
|
||||
var newValue = this.element.value.substr(0, lastTokenPos + 1);
|
||||
var whitespace = this.element.value.substr(lastTokenPos + 1).match(/^\s+/);
|
||||
if (whitespace)
|
||||
newValue += whitespace[0];
|
||||
this.element.value = newValue + value;
|
||||
} else {
|
||||
this.element.value = value;
|
||||
}
|
||||
this.element.focus();
|
||||
|
||||
if (this.options.afterUpdateElement)
|
||||
this.options.afterUpdateElement(this.element, selectedElement);
|
||||
},
|
||||
|
||||
updateChoices: function(choices) {
|
||||
if(!this.changed && this.hasFocus) {
|
||||
this.update.innerHTML = choices;
|
||||
Element.cleanWhitespace(this.update);
|
||||
Element.cleanWhitespace(this.update.down());
|
||||
|
||||
if(this.update.firstChild && this.update.down().childNodes) {
|
||||
this.entryCount =
|
||||
this.update.down().childNodes.length;
|
||||
for (var i = 0; i < this.entryCount; i++) {
|
||||
var entry = this.getEntry(i);
|
||||
entry.autocompleteIndex = i;
|
||||
this.addObservers(entry);
|
||||
}
|
||||
} else {
|
||||
this.entryCount = 0;
|
||||
}
|
||||
|
||||
this.stopIndicator();
|
||||
this.index = 0;
|
||||
|
||||
if(this.entryCount==1 && this.options.autoSelect) {
|
||||
this.selectEntry();
|
||||
this.hide();
|
||||
} else {
|
||||
this.render();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
addObservers: function(element) {
|
||||
Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
|
||||
Event.observe(element, "click", this.onClick.bindAsEventListener(this));
|
||||
},
|
||||
|
||||
onObserverEvent: function() {
|
||||
this.changed = false;
|
||||
if(this.getToken().length>=this.options.minChars) {
|
||||
this.startIndicator();
|
||||
this.getUpdatedChoices();
|
||||
} else {
|
||||
this.active = false;
|
||||
this.hide();
|
||||
}
|
||||
},
|
||||
|
||||
getToken: function() {
|
||||
var tokenPos = this.findLastToken();
|
||||
if (tokenPos != -1)
|
||||
var ret = this.element.value.substr(tokenPos + 1).replace(/^\s+/,'').replace(/\s+$/,'');
|
||||
else
|
||||
var ret = this.element.value;
|
||||
|
||||
return /\n/.test(ret) ? '' : ret;
|
||||
},
|
||||
|
||||
findLastToken: function() {
|
||||
var lastTokenPos = -1;
|
||||
|
||||
for (var i=0; i<this.options.tokens.length; i++) {
|
||||
var thisTokenPos = this.element.value.lastIndexOf(this.options.tokens[i]);
|
||||
if (thisTokenPos > lastTokenPos)
|
||||
lastTokenPos = thisTokenPos;
|
||||
}
|
||||
return lastTokenPos;
|
||||
}
|
||||
}
|
||||
|
||||
Ajax.Autocompleter = Class.create();
|
||||
Object.extend(Object.extend(Ajax.Autocompleter.prototype, Autocompleter.Base.prototype), {
|
||||
initialize: function(element, update, url, options) {
|
||||
this.baseInitialize(element, update, options);
|
||||
this.options.asynchronous = true;
|
||||
this.options.onComplete = this.onComplete.bind(this);
|
||||
this.options.defaultParams = this.options.parameters || null;
|
||||
this.url = url;
|
||||
},
|
||||
|
||||
getUpdatedChoices: function() {
|
||||
entry = encodeURIComponent(this.options.paramName) + '=' +
|
||||
encodeURIComponent(this.getToken());
|
||||
|
||||
this.options.parameters = this.options.callback ?
|
||||
this.options.callback(this.element, entry) : entry;
|
||||
|
||||
if(this.options.defaultParams)
|
||||
this.options.parameters += '&' + this.options.defaultParams;
|
||||
|
||||
new Ajax.Request(this.url, this.options);
|
||||
},
|
||||
|
||||
onComplete: function(request) {
|
||||
this.updateChoices(request.responseText);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// The local array autocompleter. Used when you'd prefer to
|
||||
// inject an array of autocompletion options into the page, rather
|
||||
// than sending out Ajax queries, which can be quite slow sometimes.
|
||||
//
|
||||
// The constructor takes four parameters. The first two are, as usual,
|
||||
// the id of the monitored textbox, and id of the autocompletion menu.
|
||||
// The third is the array you want to autocomplete from, and the fourth
|
||||
// is the options block.
|
||||
//
|
||||
// Extra local autocompletion options:
|
||||
// - choices - How many autocompletion choices to offer
|
||||
//
|
||||
// - partialSearch - If false, the autocompleter will match entered
|
||||
// text only at the beginning of strings in the
|
||||
// autocomplete array. Defaults to true, which will
|
||||
// match text at the beginning of any *word* in the
|
||||
// strings in the autocomplete array. If you want to
|
||||
// search anywhere in the string, additionally set
|
||||
// the option fullSearch to true (default: off).
|
||||
//
|
||||
// - fullSsearch - Search anywhere in autocomplete array strings.
|
||||
//
|
||||
// - partialChars - How many characters to enter before triggering
|
||||
// a partial match (unlike minChars, which defines
|
||||
// how many characters are required to do any match
|
||||
// at all). Defaults to 2.
|
||||
//
|
||||
// - ignoreCase - Whether to ignore case when autocompleting.
|
||||
// Defaults to true.
|
||||
//
|
||||
// It's possible to pass in a custom function as the 'selector'
|
||||
// option, if you prefer to write your own autocompletion logic.
|
||||
// In that case, the other options above will not apply unless
|
||||
// you support them.
|
||||
|
||||
Autocompleter.Local = Class.create();
|
||||
Autocompleter.Local.prototype = Object.extend(new Autocompleter.Base(), {
|
||||
initialize: function(element, update, array, options) {
|
||||
this.baseInitialize(element, update, options);
|
||||
this.options.array = array;
|
||||
},
|
||||
|
||||
getUpdatedChoices: function() {
|
||||
this.updateChoices(this.options.selector(this));
|
||||
},
|
||||
|
||||
setOptions: function(options) {
|
||||
this.options = Object.extend({
|
||||
choices: 10,
|
||||
partialSearch: true,
|
||||
partialChars: 2,
|
||||
ignoreCase: true,
|
||||
fullSearch: false,
|
||||
selector: function(instance) {
|
||||
var ret = []; // Beginning matches
|
||||
var partial = []; // Inside matches
|
||||
var entry = instance.getToken();
|
||||
var count = 0;
|
||||
|
||||
for (var i = 0; i < instance.options.array.length &&
|
||||
ret.length < instance.options.choices ; i++) {
|
||||
|
||||
var elem = instance.options.array[i];
|
||||
var foundPos = instance.options.ignoreCase ?
|
||||
elem.toLowerCase().indexOf(entry.toLowerCase()) :
|
||||
elem.indexOf(entry);
|
||||
|
||||
while (foundPos != -1) {
|
||||
if (foundPos == 0 && elem.length != entry.length) {
|
||||
ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" +
|
||||
elem.substr(entry.length) + "</li>");
|
||||
break;
|
||||
} else if (entry.length >= instance.options.partialChars &&
|
||||
instance.options.partialSearch && foundPos != -1) {
|
||||
if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
|
||||
partial.push("<li>" + elem.substr(0, foundPos) + "<strong>" +
|
||||
elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(
|
||||
foundPos + entry.length) + "</li>");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foundPos = instance.options.ignoreCase ?
|
||||
elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) :
|
||||
elem.indexOf(entry, foundPos + 1);
|
||||
|
||||
}
|
||||
}
|
||||
if (partial.length)
|
||||
ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
|
||||
return "<ul>" + ret.join('') + "</ul>";
|
||||
}
|
||||
}, options || {});
|
||||
}
|
||||
});
|
||||
|
||||
// AJAX in-place editor
|
||||
//
|
||||
// see documentation on http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor
|
||||
|
||||
// Use this if you notice weird scrolling problems on some browsers,
|
||||
// the DOM might be a bit confused when this gets called so do this
|
||||
// waits 1 ms (with setTimeout) until it does the activation
|
||||
Field.scrollFreeActivate = function(field) {
|
||||
setTimeout(function() {
|
||||
Field.activate(field);
|
||||
}, 1);
|
||||
}
|
||||
|
||||
Ajax.InPlaceEditor = Class.create();
|
||||
Ajax.InPlaceEditor.defaultHighlightColor = "#FFFF99";
|
||||
Ajax.InPlaceEditor.prototype = {
|
||||
initialize: function(element, url, options) {
|
||||
this.url = url;
|
||||
this.element = $(element);
|
||||
|
||||
this.options = Object.extend({
|
||||
paramName: "value",
|
||||
okButton: true,
|
||||
okText: "ok",
|
||||
cancelLink: true,
|
||||
cancelText: "cancel",
|
||||
savingText: "Saving...",
|
||||
clickToEditText: "Click to edit",
|
||||
okText: "ok",
|
||||
rows: 1,
|
||||
onComplete: function(transport, element) {
|
||||
new Effect.Highlight(element, {startcolor: this.options.highlightcolor});
|
||||
},
|
||||
onFailure: function(transport) {
|
||||
alert("Error communicating with the server: " + transport.responseText.stripTags());
|
||||
},
|
||||
callback: function(form) {
|
||||
return Form.serialize(form);
|
||||
},
|
||||
handleLineBreaks: true,
|
||||
loadingText: 'Loading...',
|
||||
savingClassName: 'inplaceeditor-saving',
|
||||
loadingClassName: 'inplaceeditor-loading',
|
||||
formClassName: 'inplaceeditor-form',
|
||||
highlightcolor: Ajax.InPlaceEditor.defaultHighlightColor,
|
||||
highlightendcolor: "#FFFFFF",
|
||||
externalControl: null,
|
||||
submitOnBlur: false,
|
||||
ajaxOptions: {},
|
||||
evalScripts: false
|
||||
}, options || {});
|
||||
|
||||
if(!this.options.formId && this.element.id) {
|
||||
this.options.formId = this.element.id + "-inplaceeditor";
|
||||
if ($(this.options.formId)) {
|
||||
// there's already a form with that name, don't specify an id
|
||||
this.options.formId = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.externalControl) {
|
||||
this.options.externalControl = $(this.options.externalControl);
|
||||
}
|
||||
|
||||
this.originalBackground = Element.getStyle(this.element, 'background-color');
|
||||
if (!this.originalBackground) {
|
||||
this.originalBackground = "transparent";
|
||||
}
|
||||
|
||||
this.element.title = this.options.clickToEditText;
|
||||
|
||||
this.onclickListener = this.enterEditMode.bindAsEventListener(this);
|
||||
this.mouseoverListener = this.enterHover.bindAsEventListener(this);
|
||||
this.mouseoutListener = this.leaveHover.bindAsEventListener(this);
|
||||
Event.observe(this.element, 'click', this.onclickListener);
|
||||
Event.observe(this.element, 'mouseover', this.mouseoverListener);
|
||||
Event.observe(this.element, 'mouseout', this.mouseoutListener);
|
||||
if (this.options.externalControl) {
|
||||
Event.observe(this.options.externalControl, 'click', this.onclickListener);
|
||||
Event.observe(this.options.externalControl, 'mouseover', this.mouseoverListener);
|
||||
Event.observe(this.options.externalControl, 'mouseout', this.mouseoutListener);
|
||||
}
|
||||
},
|
||||
enterEditMode: function(evt) {
|
||||
if (this.saving) return;
|
||||
if (this.editing) return;
|
||||
this.editing = true;
|
||||
this.onEnterEditMode();
|
||||
if (this.options.externalControl) {
|
||||
Element.hide(this.options.externalControl);
|
||||
}
|
||||
Element.hide(this.element);
|
||||
this.createForm();
|
||||
this.element.parentNode.insertBefore(this.form, this.element);
|
||||
if (!this.options.loadTextURL) Field.scrollFreeActivate(this.editField);
|
||||
// stop the event to avoid a page refresh in Safari
|
||||
if (evt) {
|
||||
Event.stop(evt);
|
||||
}
|
||||
return false;
|
||||
},
|
||||
createForm: function() {
|
||||
this.form = document.createElement("form");
|
||||
this.form.id = this.options.formId;
|
||||
Element.addClassName(this.form, this.options.formClassName)
|
||||
this.form.onsubmit = this.onSubmit.bind(this);
|
||||
|
||||
this.createEditField();
|
||||
|
||||
if (this.options.textarea) {
|
||||
var br = document.createElement("br");
|
||||
this.form.appendChild(br);
|
||||
}
|
||||
|
||||
if (this.options.okButton) {
|
||||
okButton = document.createElement("input");
|
||||
okButton.type = "submit";
|
||||
okButton.value = this.options.okText;
|
||||
okButton.className = 'editor_ok_button';
|
||||
this.form.appendChild(okButton);
|
||||
}
|
||||
|
||||
if (this.options.cancelLink) {
|
||||
cancelLink = document.createElement("a");
|
||||
cancelLink.href = "#";
|
||||
cancelLink.appendChild(document.createTextNode(this.options.cancelText));
|
||||
cancelLink.onclick = this.onclickCancel.bind(this);
|
||||
cancelLink.className = 'editor_cancel';
|
||||
this.form.appendChild(cancelLink);
|
||||
}
|
||||
},
|
||||
hasHTMLLineBreaks: function(string) {
|
||||
if (!this.options.handleLineBreaks) return false;
|
||||
return string.match(/<br/i) || string.match(/<p>/i);
|
||||
},
|
||||
convertHTMLLineBreaks: function(string) {
|
||||
return string.replace(/<br>/gi, "\n").replace(/<br\/>/gi, "\n").replace(/<\/p>/gi, "\n").replace(/<p>/gi, "");
|
||||
},
|
||||
createEditField: function() {
|
||||
var text;
|
||||
if(this.options.loadTextURL) {
|
||||
text = this.options.loadingText;
|
||||
} else {
|
||||
text = this.getText();
|
||||
}
|
||||
|
||||
var obj = this;
|
||||
|
||||
if (this.options.rows == 1 && !this.hasHTMLLineBreaks(text)) {
|
||||
this.options.textarea = false;
|
||||
var textField = document.createElement("input");
|
||||
textField.obj = this;
|
||||
textField.type = "text";
|
||||
textField.name = this.options.paramName;
|
||||
textField.value = text;
|
||||
textField.style.backgroundColor = this.options.highlightcolor;
|
||||
textField.className = 'editor_field';
|
||||
var size = this.options.size || this.options.cols || 0;
|
||||
if (size != 0) textField.size = size;
|
||||
if (this.options.submitOnBlur)
|
||||
textField.onblur = this.onSubmit.bind(this);
|
||||
this.editField = textField;
|
||||
} else {
|
||||
this.options.textarea = true;
|
||||
var textArea = document.createElement("textarea");
|
||||
textArea.obj = this;
|
||||
textArea.name = this.options.paramName;
|
||||
textArea.value = this.convertHTMLLineBreaks(text);
|
||||
textArea.rows = this.options.rows;
|
||||
textArea.cols = this.options.cols || 40;
|
||||
textArea.className = 'editor_field';
|
||||
if (this.options.submitOnBlur)
|
||||
textArea.onblur = this.onSubmit.bind(this);
|
||||
this.editField = textArea;
|
||||
}
|
||||
|
||||
if(this.options.loadTextURL) {
|
||||
this.loadExternalText();
|
||||
}
|
||||
this.form.appendChild(this.editField);
|
||||
},
|
||||
getText: function() {
|
||||
return this.element.innerHTML;
|
||||
},
|
||||
loadExternalText: function() {
|
||||
Element.addClassName(this.form, this.options.loadingClassName);
|
||||
this.editField.disabled = true;
|
||||
new Ajax.Request(
|
||||
this.options.loadTextURL,
|
||||
Object.extend({
|
||||
asynchronous: true,
|
||||
onComplete: this.onLoadedExternalText.bind(this)
|
||||
}, this.options.ajaxOptions)
|
||||
);
|
||||
},
|
||||
onLoadedExternalText: function(transport) {
|
||||
Element.removeClassName(this.form, this.options.loadingClassName);
|
||||
this.editField.disabled = false;
|
||||
this.editField.value = transport.responseText.stripTags();
|
||||
Field.scrollFreeActivate(this.editField);
|
||||
},
|
||||
onclickCancel: function() {
|
||||
this.onComplete();
|
||||
this.leaveEditMode();
|
||||
return false;
|
||||
},
|
||||
onFailure: function(transport) {
|
||||
this.options.onFailure(transport);
|
||||
if (this.oldInnerHTML) {
|
||||
this.element.innerHTML = this.oldInnerHTML;
|
||||
this.oldInnerHTML = null;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
onSubmit: function() {
|
||||
// onLoading resets these so we need to save them away for the Ajax call
|
||||
var form = this.form;
|
||||
var value = this.editField.value;
|
||||
|
||||
// do this first, sometimes the ajax call returns before we get a chance to switch on Saving...
|
||||
// which means this will actually switch on Saving... *after* we've left edit mode causing Saving...
|
||||
// to be displayed indefinitely
|
||||
this.onLoading();
|
||||
|
||||
if (this.options.evalScripts) {
|
||||
new Ajax.Request(
|
||||
this.url, Object.extend({
|
||||
parameters: this.options.callback(form, value),
|
||||
onComplete: this.onComplete.bind(this),
|
||||
onFailure: this.onFailure.bind(this),
|
||||
asynchronous:true,
|
||||
evalScripts:true
|
||||
}, this.options.ajaxOptions));
|
||||
} else {
|
||||
new Ajax.Updater(
|
||||
{ success: this.element,
|
||||
// don't update on failure (this could be an option)
|
||||
failure: null },
|
||||
this.url, Object.extend({
|
||||
parameters: this.options.callback(form, value),
|
||||
onComplete: this.onComplete.bind(this),
|
||||
onFailure: this.onFailure.bind(this)
|
||||
}, this.options.ajaxOptions));
|
||||
}
|
||||
// stop the event to avoid a page refresh in Safari
|
||||
if (arguments.length > 1) {
|
||||
Event.stop(arguments[0]);
|
||||
}
|
||||
return false;
|
||||
},
|
||||
onLoading: function() {
|
||||
this.saving = true;
|
||||
this.removeForm();
|
||||
this.leaveHover();
|
||||
this.showSaving();
|
||||
},
|
||||
showSaving: function() {
|
||||
this.oldInnerHTML = this.element.innerHTML;
|
||||
this.element.innerHTML = this.options.savingText;
|
||||
Element.addClassName(this.element, this.options.savingClassName);
|
||||
this.element.style.backgroundColor = this.originalBackground;
|
||||
Element.show(this.element);
|
||||
},
|
||||
removeForm: function() {
|
||||
if(this.form) {
|
||||
if (this.form.parentNode) Element.remove(this.form);
|
||||
this.form = null;
|
||||
}
|
||||
},
|
||||
enterHover: function() {
|
||||
if (this.saving) return;
|
||||
this.element.style.backgroundColor = this.options.highlightcolor;
|
||||
if (this.effect) {
|
||||
this.effect.cancel();
|
||||
}
|
||||
Element.addClassName(this.element, this.options.hoverClassName)
|
||||
},
|
||||
leaveHover: function() {
|
||||
if (this.options.backgroundColor) {
|
||||
this.element.style.backgroundColor = this.oldBackground;
|
||||
}
|
||||
Element.removeClassName(this.element, this.options.hoverClassName)
|
||||
if (this.saving) return;
|
||||
this.effect = new Effect.Highlight(this.element, {
|
||||
startcolor: this.options.highlightcolor,
|
||||
endcolor: this.options.highlightendcolor,
|
||||
restorecolor: this.originalBackground
|
||||
});
|
||||
},
|
||||
leaveEditMode: function() {
|
||||
Element.removeClassName(this.element, this.options.savingClassName);
|
||||
this.removeForm();
|
||||
this.leaveHover();
|
||||
this.element.style.backgroundColor = this.originalBackground;
|
||||
Element.show(this.element);
|
||||
if (this.options.externalControl) {
|
||||
Element.show(this.options.externalControl);
|
||||
}
|
||||
this.editing = false;
|
||||
this.saving = false;
|
||||
this.oldInnerHTML = null;
|
||||
this.onLeaveEditMode();
|
||||
},
|
||||
onComplete: function(transport) {
|
||||
this.leaveEditMode();
|
||||
this.options.onComplete.bind(this)(transport, this.element);
|
||||
},
|
||||
onEnterEditMode: function() {},
|
||||
onLeaveEditMode: function() {},
|
||||
dispose: function() {
|
||||
if (this.oldInnerHTML) {
|
||||
this.element.innerHTML = this.oldInnerHTML;
|
||||
}
|
||||
this.leaveEditMode();
|
||||
Event.stopObserving(this.element, 'click', this.onclickListener);
|
||||
Event.stopObserving(this.element, 'mouseover', this.mouseoverListener);
|
||||
Event.stopObserving(this.element, 'mouseout', this.mouseoutListener);
|
||||
if (this.options.externalControl) {
|
||||
Event.stopObserving(this.options.externalControl, 'click', this.onclickListener);
|
||||
Event.stopObserving(this.options.externalControl, 'mouseover', this.mouseoverListener);
|
||||
Event.stopObserving(this.options.externalControl, 'mouseout', this.mouseoutListener);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ajax.InPlaceCollectionEditor = Class.create();
|
||||
Object.extend(Ajax.InPlaceCollectionEditor.prototype, Ajax.InPlaceEditor.prototype);
|
||||
Object.extend(Ajax.InPlaceCollectionEditor.prototype, {
|
||||
createEditField: function() {
|
||||
if (!this.cached_selectTag) {
|
||||
var selectTag = document.createElement("select");
|
||||
var collection = this.options.collection || [];
|
||||
var optionTag;
|
||||
collection.each(function(e,i) {
|
||||
optionTag = document.createElement("option");
|
||||
optionTag.value = (e instanceof Array) ? e[0] : e;
|
||||
if((typeof this.options.value == 'undefined') &&
|
||||
((e instanceof Array) ? this.element.innerHTML == e[1] : e == optionTag.value)) optionTag.selected = true;
|
||||
if(this.options.value==optionTag.value) optionTag.selected = true;
|
||||
optionTag.appendChild(document.createTextNode((e instanceof Array) ? e[1] : e));
|
||||
selectTag.appendChild(optionTag);
|
||||
}.bind(this));
|
||||
this.cached_selectTag = selectTag;
|
||||
}
|
||||
|
||||
this.editField = this.cached_selectTag;
|
||||
if(this.options.loadTextURL) this.loadExternalText();
|
||||
this.form.appendChild(this.editField);
|
||||
this.options.callback = function(form, value) {
|
||||
return "value=" + encodeURIComponent(value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Delayed observer, like Form.Element.Observer,
|
||||
// but waits for delay after last key input
|
||||
// Ideal for live-search fields
|
||||
|
||||
Form.Element.DelayedObserver = Class.create();
|
||||
Form.Element.DelayedObserver.prototype = {
|
||||
initialize: function(element, delay, callback) {
|
||||
this.delay = delay || 0.5;
|
||||
this.element = $(element);
|
||||
this.callback = callback;
|
||||
this.timer = null;
|
||||
this.lastValue = $F(this.element);
|
||||
Event.observe(this.element,'keyup',this.delayedListener.bindAsEventListener(this));
|
||||
},
|
||||
delayedListener: function(event) {
|
||||
if(this.lastValue == $F(this.element)) return;
|
||||
if(this.timer) clearTimeout(this.timer);
|
||||
this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000);
|
||||
this.lastValue = $F(this.element);
|
||||
},
|
||||
onTimerEvent: function() {
|
||||
this.timer = null;
|
||||
this.callback(this.element, $F(this.element));
|
||||
}
|
||||
};
|
||||
942
vendor/plugins/shoulda/test/rails_root/public/javascripts/dragdrop.js
vendored
Normal file
942
vendor/plugins/shoulda/test/rails_root/public/javascripts/dragdrop.js
vendored
Normal file
@@ -0,0 +1,942 @@
|
||||
// Copyright (c) 2005, 2006 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
|
||||
// (c) 2005, 2006 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz)
|
||||
//
|
||||
// script.aculo.us is freely distributable under the terms of an MIT-style license.
|
||||
// For details, see the script.aculo.us web site: http://script.aculo.us/
|
||||
|
||||
if(typeof Effect == 'undefined')
|
||||
throw("dragdrop.js requires including script.aculo.us' effects.js library");
|
||||
|
||||
var Droppables = {
|
||||
drops: [],
|
||||
|
||||
remove: function(element) {
|
||||
this.drops = this.drops.reject(function(d) { return d.element==$(element) });
|
||||
},
|
||||
|
||||
add: function(element) {
|
||||
element = $(element);
|
||||
var options = Object.extend({
|
||||
greedy: true,
|
||||
hoverclass: null,
|
||||
tree: false
|
||||
}, arguments[1] || {});
|
||||
|
||||
// cache containers
|
||||
if(options.containment) {
|
||||
options._containers = [];
|
||||
var containment = options.containment;
|
||||
if((typeof containment == 'object') &&
|
||||
(containment.constructor == Array)) {
|
||||
containment.each( function(c) { options._containers.push($(c)) });
|
||||
} else {
|
||||
options._containers.push($(containment));
|
||||
}
|
||||
}
|
||||
|
||||
if(options.accept) options.accept = [options.accept].flatten();
|
||||
|
||||
Element.makePositioned(element); // fix IE
|
||||
options.element = element;
|
||||
|
||||
this.drops.push(options);
|
||||
},
|
||||
|
||||
findDeepestChild: function(drops) {
|
||||
deepest = drops[0];
|
||||
|
||||
for (i = 1; i < drops.length; ++i)
|
||||
if (Element.isParent(drops[i].element, deepest.element))
|
||||
deepest = drops[i];
|
||||
|
||||
return deepest;
|
||||
},
|
||||
|
||||
isContained: function(element, drop) {
|
||||
var containmentNode;
|
||||
if(drop.tree) {
|
||||
containmentNode = element.treeNode;
|
||||
} else {
|
||||
containmentNode = element.parentNode;
|
||||
}
|
||||
return drop._containers.detect(function(c) { return containmentNode == c });
|
||||
},
|
||||
|
||||
isAffected: function(point, element, drop) {
|
||||
return (
|
||||
(drop.element!=element) &&
|
||||
((!drop._containers) ||
|
||||
this.isContained(element, drop)) &&
|
||||
((!drop.accept) ||
|
||||
(Element.classNames(element).detect(
|
||||
function(v) { return drop.accept.include(v) } ) )) &&
|
||||
Position.within(drop.element, point[0], point[1]) );
|
||||
},
|
||||
|
||||
deactivate: function(drop) {
|
||||
if(drop.hoverclass)
|
||||
Element.removeClassName(drop.element, drop.hoverclass);
|
||||
this.last_active = null;
|
||||
},
|
||||
|
||||
activate: function(drop) {
|
||||
if(drop.hoverclass)
|
||||
Element.addClassName(drop.element, drop.hoverclass);
|
||||
this.last_active = drop;
|
||||
},
|
||||
|
||||
show: function(point, element) {
|
||||
if(!this.drops.length) return;
|
||||
var affected = [];
|
||||
|
||||
if(this.last_active) this.deactivate(this.last_active);
|
||||
this.drops.each( function(drop) {
|
||||
if(Droppables.isAffected(point, element, drop))
|
||||
affected.push(drop);
|
||||
});
|
||||
|
||||
if(affected.length>0) {
|
||||
drop = Droppables.findDeepestChild(affected);
|
||||
Position.within(drop.element, point[0], point[1]);
|
||||
if(drop.onHover)
|
||||
drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element));
|
||||
|
||||
Droppables.activate(drop);
|
||||
}
|
||||
},
|
||||
|
||||
fire: function(event, element) {
|
||||
if(!this.last_active) return;
|
||||
Position.prepare();
|
||||
|
||||
if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active))
|
||||
if (this.last_active.onDrop)
|
||||
this.last_active.onDrop(element, this.last_active.element, event);
|
||||
},
|
||||
|
||||
reset: function() {
|
||||
if(this.last_active)
|
||||
this.deactivate(this.last_active);
|
||||
}
|
||||
}
|
||||
|
||||
var Draggables = {
|
||||
drags: [],
|
||||
observers: [],
|
||||
|
||||
register: function(draggable) {
|
||||
if(this.drags.length == 0) {
|
||||
this.eventMouseUp = this.endDrag.bindAsEventListener(this);
|
||||
this.eventMouseMove = this.updateDrag.bindAsEventListener(this);
|
||||
this.eventKeypress = this.keyPress.bindAsEventListener(this);
|
||||
|
||||
Event.observe(document, "mouseup", this.eventMouseUp);
|
||||
Event.observe(document, "mousemove", this.eventMouseMove);
|
||||
Event.observe(document, "keypress", this.eventKeypress);
|
||||
}
|
||||
this.drags.push(draggable);
|
||||
},
|
||||
|
||||
unregister: function(draggable) {
|
||||
this.drags = this.drags.reject(function(d) { return d==draggable });
|
||||
if(this.drags.length == 0) {
|
||||
Event.stopObserving(document, "mouseup", this.eventMouseUp);
|
||||
Event.stopObserving(document, "mousemove", this.eventMouseMove);
|
||||
Event.stopObserving(document, "keypress", this.eventKeypress);
|
||||
}
|
||||
},
|
||||
|
||||
activate: function(draggable) {
|
||||
if(draggable.options.delay) {
|
||||
this._timeout = setTimeout(function() {
|
||||
Draggables._timeout = null;
|
||||
window.focus();
|
||||
Draggables.activeDraggable = draggable;
|
||||
}.bind(this), draggable.options.delay);
|
||||
} else {
|
||||
window.focus(); // allows keypress events if window isn't currently focused, fails for Safari
|
||||
this.activeDraggable = draggable;
|
||||
}
|
||||
},
|
||||
|
||||
deactivate: function() {
|
||||
this.activeDraggable = null;
|
||||
},
|
||||
|
||||
updateDrag: function(event) {
|
||||
if(!this.activeDraggable) return;
|
||||
var pointer = [Event.pointerX(event), Event.pointerY(event)];
|
||||
// Mozilla-based browsers fire successive mousemove events with
|
||||
// the same coordinates, prevent needless redrawing (moz bug?)
|
||||
if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return;
|
||||
this._lastPointer = pointer;
|
||||
|
||||
this.activeDraggable.updateDrag(event, pointer);
|
||||
},
|
||||
|
||||
endDrag: function(event) {
|
||||
if(this._timeout) {
|
||||
clearTimeout(this._timeout);
|
||||
this._timeout = null;
|
||||
}
|
||||
if(!this.activeDraggable) return;
|
||||
this._lastPointer = null;
|
||||
this.activeDraggable.endDrag(event);
|
||||
this.activeDraggable = null;
|
||||
},
|
||||
|
||||
keyPress: function(event) {
|
||||
if(this.activeDraggable)
|
||||
this.activeDraggable.keyPress(event);
|
||||
},
|
||||
|
||||
addObserver: function(observer) {
|
||||
this.observers.push(observer);
|
||||
this._cacheObserverCallbacks();
|
||||
},
|
||||
|
||||
removeObserver: function(element) { // element instead of observer fixes mem leaks
|
||||
this.observers = this.observers.reject( function(o) { return o.element==element });
|
||||
this._cacheObserverCallbacks();
|
||||
},
|
||||
|
||||
notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag'
|
||||
if(this[eventName+'Count'] > 0)
|
||||
this.observers.each( function(o) {
|
||||
if(o[eventName]) o[eventName](eventName, draggable, event);
|
||||
});
|
||||
if(draggable.options[eventName]) draggable.options[eventName](draggable, event);
|
||||
},
|
||||
|
||||
_cacheObserverCallbacks: function() {
|
||||
['onStart','onEnd','onDrag'].each( function(eventName) {
|
||||
Draggables[eventName+'Count'] = Draggables.observers.select(
|
||||
function(o) { return o[eventName]; }
|
||||
).length;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
var Draggable = Class.create();
|
||||
Draggable._dragging = {};
|
||||
|
||||
Draggable.prototype = {
|
||||
initialize: function(element) {
|
||||
var defaults = {
|
||||
handle: false,
|
||||
reverteffect: function(element, top_offset, left_offset) {
|
||||
var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02;
|
||||
new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur,
|
||||
queue: {scope:'_draggable', position:'end'}
|
||||
});
|
||||
},
|
||||
endeffect: function(element) {
|
||||
var toOpacity = typeof element._opacity == 'number' ? element._opacity : 1.0;
|
||||
new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity,
|
||||
queue: {scope:'_draggable', position:'end'},
|
||||
afterFinish: function(){
|
||||
Draggable._dragging[element] = false
|
||||
}
|
||||
});
|
||||
},
|
||||
zindex: 1000,
|
||||
revert: false,
|
||||
scroll: false,
|
||||
scrollSensitivity: 20,
|
||||
scrollSpeed: 15,
|
||||
snap: false, // false, or xy or [x,y] or function(x,y){ return [x,y] }
|
||||
delay: 0
|
||||
};
|
||||
|
||||
if(!arguments[1] || typeof arguments[1].endeffect == 'undefined')
|
||||
Object.extend(defaults, {
|
||||
starteffect: function(element) {
|
||||
element._opacity = Element.getOpacity(element);
|
||||
Draggable._dragging[element] = true;
|
||||
new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7});
|
||||
}
|
||||
});
|
||||
|
||||
var options = Object.extend(defaults, arguments[1] || {});
|
||||
|
||||
this.element = $(element);
|
||||
|
||||
if(options.handle && (typeof options.handle == 'string'))
|
||||
this.handle = this.element.down('.'+options.handle, 0);
|
||||
|
||||
if(!this.handle) this.handle = $(options.handle);
|
||||
if(!this.handle) this.handle = this.element;
|
||||
|
||||
if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) {
|
||||
options.scroll = $(options.scroll);
|
||||
this._isScrollChild = Element.childOf(this.element, options.scroll);
|
||||
}
|
||||
|
||||
Element.makePositioned(this.element); // fix IE
|
||||
|
||||
this.delta = this.currentDelta();
|
||||
this.options = options;
|
||||
this.dragging = false;
|
||||
|
||||
this.eventMouseDown = this.initDrag.bindAsEventListener(this);
|
||||
Event.observe(this.handle, "mousedown", this.eventMouseDown);
|
||||
|
||||
Draggables.register(this);
|
||||
},
|
||||
|
||||
destroy: function() {
|
||||
Event.stopObserving(this.handle, "mousedown", this.eventMouseDown);
|
||||
Draggables.unregister(this);
|
||||
},
|
||||
|
||||
currentDelta: function() {
|
||||
return([
|
||||
parseInt(Element.getStyle(this.element,'left') || '0'),
|
||||
parseInt(Element.getStyle(this.element,'top') || '0')]);
|
||||
},
|
||||
|
||||
initDrag: function(event) {
|
||||
if(typeof Draggable._dragging[this.element] != 'undefined' &&
|
||||
Draggable._dragging[this.element]) return;
|
||||
if(Event.isLeftClick(event)) {
|
||||
// abort on form elements, fixes a Firefox issue
|
||||
var src = Event.element(event);
|
||||
if(src.tagName && (
|
||||
src.tagName=='INPUT' ||
|
||||
src.tagName=='SELECT' ||
|
||||
src.tagName=='OPTION' ||
|
||||
src.tagName=='BUTTON' ||
|
||||
src.tagName=='TEXTAREA')) return;
|
||||
|
||||
var pointer = [Event.pointerX(event), Event.pointerY(event)];
|
||||
var pos = Position.cumulativeOffset(this.element);
|
||||
this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) });
|
||||
|
||||
Draggables.activate(this);
|
||||
Event.stop(event);
|
||||
}
|
||||
},
|
||||
|
||||
startDrag: function(event) {
|
||||
this.dragging = true;
|
||||
|
||||
if(this.options.zindex) {
|
||||
this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0);
|
||||
this.element.style.zIndex = this.options.zindex;
|
||||
}
|
||||
|
||||
if(this.options.ghosting) {
|
||||
this._clone = this.element.cloneNode(true);
|
||||
Position.absolutize(this.element);
|
||||
this.element.parentNode.insertBefore(this._clone, this.element);
|
||||
}
|
||||
|
||||
if(this.options.scroll) {
|
||||
if (this.options.scroll == window) {
|
||||
var where = this._getWindowScroll(this.options.scroll);
|
||||
this.originalScrollLeft = where.left;
|
||||
this.originalScrollTop = where.top;
|
||||
} else {
|
||||
this.originalScrollLeft = this.options.scroll.scrollLeft;
|
||||
this.originalScrollTop = this.options.scroll.scrollTop;
|
||||
}
|
||||
}
|
||||
|
||||
Draggables.notify('onStart', this, event);
|
||||
|
||||
if(this.options.starteffect) this.options.starteffect(this.element);
|
||||
},
|
||||
|
||||
updateDrag: function(event, pointer) {
|
||||
if(!this.dragging) this.startDrag(event);
|
||||
Position.prepare();
|
||||
Droppables.show(pointer, this.element);
|
||||
Draggables.notify('onDrag', this, event);
|
||||
|
||||
this.draw(pointer);
|
||||
if(this.options.change) this.options.change(this);
|
||||
|
||||
if(this.options.scroll) {
|
||||
this.stopScrolling();
|
||||
|
||||
var p;
|
||||
if (this.options.scroll == window) {
|
||||
with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; }
|
||||
} else {
|
||||
p = Position.page(this.options.scroll);
|
||||
p[0] += this.options.scroll.scrollLeft + Position.deltaX;
|
||||
p[1] += this.options.scroll.scrollTop + Position.deltaY;
|
||||
p.push(p[0]+this.options.scroll.offsetWidth);
|
||||
p.push(p[1]+this.options.scroll.offsetHeight);
|
||||
}
|
||||
var speed = [0,0];
|
||||
if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity);
|
||||
if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity);
|
||||
if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity);
|
||||
if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity);
|
||||
this.startScrolling(speed);
|
||||
}
|
||||
|
||||
// fix AppleWebKit rendering
|
||||
if(navigator.appVersion.indexOf('AppleWebKit')>0) window.scrollBy(0,0);
|
||||
|
||||
Event.stop(event);
|
||||
},
|
||||
|
||||
finishDrag: function(event, success) {
|
||||
this.dragging = false;
|
||||
|
||||
if(this.options.ghosting) {
|
||||
Position.relativize(this.element);
|
||||
Element.remove(this._clone);
|
||||
this._clone = null;
|
||||
}
|
||||
|
||||
if(success) Droppables.fire(event, this.element);
|
||||
Draggables.notify('onEnd', this, event);
|
||||
|
||||
var revert = this.options.revert;
|
||||
if(revert && typeof revert == 'function') revert = revert(this.element);
|
||||
|
||||
var d = this.currentDelta();
|
||||
if(revert && this.options.reverteffect) {
|
||||
this.options.reverteffect(this.element,
|
||||
d[1]-this.delta[1], d[0]-this.delta[0]);
|
||||
} else {
|
||||
this.delta = d;
|
||||
}
|
||||
|
||||
if(this.options.zindex)
|
||||
this.element.style.zIndex = this.originalZ;
|
||||
|
||||
if(this.options.endeffect)
|
||||
this.options.endeffect(this.element);
|
||||
|
||||
Draggables.deactivate(this);
|
||||
Droppables.reset();
|
||||
},
|
||||
|
||||
keyPress: function(event) {
|
||||
if(event.keyCode!=Event.KEY_ESC) return;
|
||||
this.finishDrag(event, false);
|
||||
Event.stop(event);
|
||||
},
|
||||
|
||||
endDrag: function(event) {
|
||||
if(!this.dragging) return;
|
||||
this.stopScrolling();
|
||||
this.finishDrag(event, true);
|
||||
Event.stop(event);
|
||||
},
|
||||
|
||||
draw: function(point) {
|
||||
var pos = Position.cumulativeOffset(this.element);
|
||||
if(this.options.ghosting) {
|
||||
var r = Position.realOffset(this.element);
|
||||
pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY;
|
||||
}
|
||||
|
||||
var d = this.currentDelta();
|
||||
pos[0] -= d[0]; pos[1] -= d[1];
|
||||
|
||||
if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) {
|
||||
pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft;
|
||||
pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop;
|
||||
}
|
||||
|
||||
var p = [0,1].map(function(i){
|
||||
return (point[i]-pos[i]-this.offset[i])
|
||||
}.bind(this));
|
||||
|
||||
if(this.options.snap) {
|
||||
if(typeof this.options.snap == 'function') {
|
||||
p = this.options.snap(p[0],p[1],this);
|
||||
} else {
|
||||
if(this.options.snap instanceof Array) {
|
||||
p = p.map( function(v, i) {
|
||||
return Math.round(v/this.options.snap[i])*this.options.snap[i] }.bind(this))
|
||||
} else {
|
||||
p = p.map( function(v) {
|
||||
return Math.round(v/this.options.snap)*this.options.snap }.bind(this))
|
||||
}
|
||||
}}
|
||||
|
||||
var style = this.element.style;
|
||||
if((!this.options.constraint) || (this.options.constraint=='horizontal'))
|
||||
style.left = p[0] + "px";
|
||||
if((!this.options.constraint) || (this.options.constraint=='vertical'))
|
||||
style.top = p[1] + "px";
|
||||
|
||||
if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering
|
||||
},
|
||||
|
||||
stopScrolling: function() {
|
||||
if(this.scrollInterval) {
|
||||
clearInterval(this.scrollInterval);
|
||||
this.scrollInterval = null;
|
||||
Draggables._lastScrollPointer = null;
|
||||
}
|
||||
},
|
||||
|
||||
startScrolling: function(speed) {
|
||||
if(!(speed[0] || speed[1])) return;
|
||||
this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed];
|
||||
this.lastScrolled = new Date();
|
||||
this.scrollInterval = setInterval(this.scroll.bind(this), 10);
|
||||
},
|
||||
|
||||
scroll: function() {
|
||||
var current = new Date();
|
||||
var delta = current - this.lastScrolled;
|
||||
this.lastScrolled = current;
|
||||
if(this.options.scroll == window) {
|
||||
with (this._getWindowScroll(this.options.scroll)) {
|
||||
if (this.scrollSpeed[0] || this.scrollSpeed[1]) {
|
||||
var d = delta / 1000;
|
||||
this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000;
|
||||
this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000;
|
||||
}
|
||||
|
||||
Position.prepare();
|
||||
Droppables.show(Draggables._lastPointer, this.element);
|
||||
Draggables.notify('onDrag', this);
|
||||
if (this._isScrollChild) {
|
||||
Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer);
|
||||
Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000;
|
||||
Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000;
|
||||
if (Draggables._lastScrollPointer[0] < 0)
|
||||
Draggables._lastScrollPointer[0] = 0;
|
||||
if (Draggables._lastScrollPointer[1] < 0)
|
||||
Draggables._lastScrollPointer[1] = 0;
|
||||
this.draw(Draggables._lastScrollPointer);
|
||||
}
|
||||
|
||||
if(this.options.change) this.options.change(this);
|
||||
},
|
||||
|
||||
_getWindowScroll: function(w) {
|
||||
var T, L, W, H;
|
||||
with (w.document) {
|
||||
if (w.document.documentElement && documentElement.scrollTop) {
|
||||
T = documentElement.scrollTop;
|
||||
L = documentElement.scrollLeft;
|
||||
} else if (w.document.body) {
|
||||
T = body.scrollTop;
|
||||
L = body.scrollLeft;
|
||||
}
|
||||
if (w.innerWidth) {
|
||||
W = w.innerWidth;
|
||||
H = w.innerHeight;
|
||||
} else if (w.document.documentElement && documentElement.clientWidth) {
|
||||
W = documentElement.clientWidth;
|
||||
H = documentElement.clientHeight;
|
||||
} else {
|
||||
W = body.offsetWidth;
|
||||
H = body.offsetHeight
|
||||
}
|
||||
}
|
||||
return { top: T, left: L, width: W, height: H };
|
||||
}
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
var SortableObserver = Class.create();
|
||||
SortableObserver.prototype = {
|
||||
initialize: function(element, observer) {
|
||||
this.element = $(element);
|
||||
this.observer = observer;
|
||||
this.lastValue = Sortable.serialize(this.element);
|
||||
},
|
||||
|
||||
onStart: function() {
|
||||
this.lastValue = Sortable.serialize(this.element);
|
||||
},
|
||||
|
||||
onEnd: function() {
|
||||
Sortable.unmark();
|
||||
if(this.lastValue != Sortable.serialize(this.element))
|
||||
this.observer(this.element)
|
||||
}
|
||||
}
|
||||
|
||||
var Sortable = {
|
||||
SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/,
|
||||
|
||||
sortables: {},
|
||||
|
||||
_findRootElement: function(element) {
|
||||
while (element.tagName != "BODY") {
|
||||
if(element.id && Sortable.sortables[element.id]) return element;
|
||||
element = element.parentNode;
|
||||
}
|
||||
},
|
||||
|
||||
options: function(element) {
|
||||
element = Sortable._findRootElement($(element));
|
||||
if(!element) return;
|
||||
return Sortable.sortables[element.id];
|
||||
},
|
||||
|
||||
destroy: function(element){
|
||||
var s = Sortable.options(element);
|
||||
|
||||
if(s) {
|
||||
Draggables.removeObserver(s.element);
|
||||
s.droppables.each(function(d){ Droppables.remove(d) });
|
||||
s.draggables.invoke('destroy');
|
||||
|
||||
delete Sortable.sortables[s.element.id];
|
||||
}
|
||||
},
|
||||
|
||||
create: function(element) {
|
||||
element = $(element);
|
||||
var options = Object.extend({
|
||||
element: element,
|
||||
tag: 'li', // assumes li children, override with tag: 'tagname'
|
||||
dropOnEmpty: false,
|
||||
tree: false,
|
||||
treeTag: 'ul',
|
||||
overlap: 'vertical', // one of 'vertical', 'horizontal'
|
||||
constraint: 'vertical', // one of 'vertical', 'horizontal', false
|
||||
containment: element, // also takes array of elements (or id's); or false
|
||||
handle: false, // or a CSS class
|
||||
only: false,
|
||||
delay: 0,
|
||||
hoverclass: null,
|
||||
ghosting: false,
|
||||
scroll: false,
|
||||
scrollSensitivity: 20,
|
||||
scrollSpeed: 15,
|
||||
format: this.SERIALIZE_RULE,
|
||||
onChange: Prototype.emptyFunction,
|
||||
onUpdate: Prototype.emptyFunction
|
||||
}, arguments[1] || {});
|
||||
|
||||
// clear any old sortable with same element
|
||||
this.destroy(element);
|
||||
|
||||
// build options for the draggables
|
||||
var options_for_draggable = {
|
||||
revert: true,
|
||||
scroll: options.scroll,
|
||||
scrollSpeed: options.scrollSpeed,
|
||||
scrollSensitivity: options.scrollSensitivity,
|
||||
delay: options.delay,
|
||||
ghosting: options.ghosting,
|
||||
constraint: options.constraint,
|
||||
handle: options.handle };
|
||||
|
||||
if(options.starteffect)
|
||||
options_for_draggable.starteffect = options.starteffect;
|
||||
|
||||
if(options.reverteffect)
|
||||
options_for_draggable.reverteffect = options.reverteffect;
|
||||
else
|
||||
if(options.ghosting) options_for_draggable.reverteffect = function(element) {
|
||||
element.style.top = 0;
|
||||
element.style.left = 0;
|
||||
};
|
||||
|
||||
if(options.endeffect)
|
||||
options_for_draggable.endeffect = options.endeffect;
|
||||
|
||||
if(options.zindex)
|
||||
options_for_draggable.zindex = options.zindex;
|
||||
|
||||
// build options for the droppables
|
||||
var options_for_droppable = {
|
||||
overlap: options.overlap,
|
||||
containment: options.containment,
|
||||
tree: options.tree,
|
||||
hoverclass: options.hoverclass,
|
||||
onHover: Sortable.onHover
|
||||
}
|
||||
|
||||
var options_for_tree = {
|
||||
onHover: Sortable.onEmptyHover,
|
||||
overlap: options.overlap,
|
||||
containment: options.containment,
|
||||
hoverclass: options.hoverclass
|
||||
}
|
||||
|
||||
// fix for gecko engine
|
||||
Element.cleanWhitespace(element);
|
||||
|
||||
options.draggables = [];
|
||||
options.droppables = [];
|
||||
|
||||
// drop on empty handling
|
||||
if(options.dropOnEmpty || options.tree) {
|
||||
Droppables.add(element, options_for_tree);
|
||||
options.droppables.push(element);
|
||||
}
|
||||
|
||||
(this.findElements(element, options) || []).each( function(e) {
|
||||
// handles are per-draggable
|
||||
var handle = options.handle ?
|
||||
$(e).down('.'+options.handle,0) : e;
|
||||
options.draggables.push(
|
||||
new Draggable(e, Object.extend(options_for_draggable, { handle: handle })));
|
||||
Droppables.add(e, options_for_droppable);
|
||||
if(options.tree) e.treeNode = element;
|
||||
options.droppables.push(e);
|
||||
});
|
||||
|
||||
if(options.tree) {
|
||||
(Sortable.findTreeElements(element, options) || []).each( function(e) {
|
||||
Droppables.add(e, options_for_tree);
|
||||
e.treeNode = element;
|
||||
options.droppables.push(e);
|
||||
});
|
||||
}
|
||||
|
||||
// keep reference
|
||||
this.sortables[element.id] = options;
|
||||
|
||||
// for onupdate
|
||||
Draggables.addObserver(new SortableObserver(element, options.onUpdate));
|
||||
|
||||
},
|
||||
|
||||
// return all suitable-for-sortable elements in a guaranteed order
|
||||
findElements: function(element, options) {
|
||||
return Element.findChildren(
|
||||
element, options.only, options.tree ? true : false, options.tag);
|
||||
},
|
||||
|
||||
findTreeElements: function(element, options) {
|
||||
return Element.findChildren(
|
||||
element, options.only, options.tree ? true : false, options.treeTag);
|
||||
},
|
||||
|
||||
onHover: function(element, dropon, overlap) {
|
||||
if(Element.isParent(dropon, element)) return;
|
||||
|
||||
if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) {
|
||||
return;
|
||||
} else if(overlap>0.5) {
|
||||
Sortable.mark(dropon, 'before');
|
||||
if(dropon.previousSibling != element) {
|
||||
var oldParentNode = element.parentNode;
|
||||
element.style.visibility = "hidden"; // fix gecko rendering
|
||||
dropon.parentNode.insertBefore(element, dropon);
|
||||
if(dropon.parentNode!=oldParentNode)
|
||||
Sortable.options(oldParentNode).onChange(element);
|
||||
Sortable.options(dropon.parentNode).onChange(element);
|
||||
}
|
||||
} else {
|
||||
Sortable.mark(dropon, 'after');
|
||||
var nextElement = dropon.nextSibling || null;
|
||||
if(nextElement != element) {
|
||||
var oldParentNode = element.parentNode;
|
||||
element.style.visibility = "hidden"; // fix gecko rendering
|
||||
dropon.parentNode.insertBefore(element, nextElement);
|
||||
if(dropon.parentNode!=oldParentNode)
|
||||
Sortable.options(oldParentNode).onChange(element);
|
||||
Sortable.options(dropon.parentNode).onChange(element);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
onEmptyHover: function(element, dropon, overlap) {
|
||||
var oldParentNode = element.parentNode;
|
||||
var droponOptions = Sortable.options(dropon);
|
||||
|
||||
if(!Element.isParent(dropon, element)) {
|
||||
var index;
|
||||
|
||||
var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only});
|
||||
var child = null;
|
||||
|
||||
if(children) {
|
||||
var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap);
|
||||
|
||||
for (index = 0; index < children.length; index += 1) {
|
||||
if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) {
|
||||
offset -= Element.offsetSize (children[index], droponOptions.overlap);
|
||||
} else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) {
|
||||
child = index + 1 < children.length ? children[index + 1] : null;
|
||||
break;
|
||||
} else {
|
||||
child = children[index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dropon.insertBefore(element, child);
|
||||
|
||||
Sortable.options(oldParentNode).onChange(element);
|
||||
droponOptions.onChange(element);
|
||||
}
|
||||
},
|
||||
|
||||
unmark: function() {
|
||||
if(Sortable._marker) Sortable._marker.hide();
|
||||
},
|
||||
|
||||
mark: function(dropon, position) {
|
||||
// mark on ghosting only
|
||||
var sortable = Sortable.options(dropon.parentNode);
|
||||
if(sortable && !sortable.ghosting) return;
|
||||
|
||||
if(!Sortable._marker) {
|
||||
Sortable._marker =
|
||||
($('dropmarker') || Element.extend(document.createElement('DIV'))).
|
||||
hide().addClassName('dropmarker').setStyle({position:'absolute'});
|
||||
document.getElementsByTagName("body").item(0).appendChild(Sortable._marker);
|
||||
}
|
||||
var offsets = Position.cumulativeOffset(dropon);
|
||||
Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'});
|
||||
|
||||
if(position=='after')
|
||||
if(sortable.overlap == 'horizontal')
|
||||
Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'});
|
||||
else
|
||||
Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'});
|
||||
|
||||
Sortable._marker.show();
|
||||
},
|
||||
|
||||
_tree: function(element, options, parent) {
|
||||
var children = Sortable.findElements(element, options) || [];
|
||||
|
||||
for (var i = 0; i < children.length; ++i) {
|
||||
var match = children[i].id.match(options.format);
|
||||
|
||||
if (!match) continue;
|
||||
|
||||
var child = {
|
||||
id: encodeURIComponent(match ? match[1] : null),
|
||||
element: element,
|
||||
parent: parent,
|
||||
children: [],
|
||||
position: parent.children.length,
|
||||
container: $(children[i]).down(options.treeTag)
|
||||
}
|
||||
|
||||
/* Get the element containing the children and recurse over it */
|
||||
if (child.container)
|
||||
this._tree(child.container, options, child)
|
||||
|
||||
parent.children.push (child);
|
||||
}
|
||||
|
||||
return parent;
|
||||
},
|
||||
|
||||
tree: function(element) {
|
||||
element = $(element);
|
||||
var sortableOptions = this.options(element);
|
||||
var options = Object.extend({
|
||||
tag: sortableOptions.tag,
|
||||
treeTag: sortableOptions.treeTag,
|
||||
only: sortableOptions.only,
|
||||
name: element.id,
|
||||
format: sortableOptions.format
|
||||
}, arguments[1] || {});
|
||||
|
||||
var root = {
|
||||
id: null,
|
||||
parent: null,
|
||||
children: [],
|
||||
container: element,
|
||||
position: 0
|
||||
}
|
||||
|
||||
return Sortable._tree(element, options, root);
|
||||
},
|
||||
|
||||
/* Construct a [i] index for a particular node */
|
||||
_constructIndex: function(node) {
|
||||
var index = '';
|
||||
do {
|
||||
if (node.id) index = '[' + node.position + ']' + index;
|
||||
} while ((node = node.parent) != null);
|
||||
return index;
|
||||
},
|
||||
|
||||
sequence: function(element) {
|
||||
element = $(element);
|
||||
var options = Object.extend(this.options(element), arguments[1] || {});
|
||||
|
||||
return $(this.findElements(element, options) || []).map( function(item) {
|
||||
return item.id.match(options.format) ? item.id.match(options.format)[1] : '';
|
||||
});
|
||||
},
|
||||
|
||||
setSequence: function(element, new_sequence) {
|
||||
element = $(element);
|
||||
var options = Object.extend(this.options(element), arguments[2] || {});
|
||||
|
||||
var nodeMap = {};
|
||||
this.findElements(element, options).each( function(n) {
|
||||
if (n.id.match(options.format))
|
||||
nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode];
|
||||
n.parentNode.removeChild(n);
|
||||
});
|
||||
|
||||
new_sequence.each(function(ident) {
|
||||
var n = nodeMap[ident];
|
||||
if (n) {
|
||||
n[1].appendChild(n[0]);
|
||||
delete nodeMap[ident];
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
serialize: function(element) {
|
||||
element = $(element);
|
||||
var options = Object.extend(Sortable.options(element), arguments[1] || {});
|
||||
var name = encodeURIComponent(
|
||||
(arguments[1] && arguments[1].name) ? arguments[1].name : element.id);
|
||||
|
||||
if (options.tree) {
|
||||
return Sortable.tree(element, arguments[1]).children.map( function (item) {
|
||||
return [name + Sortable._constructIndex(item) + "[id]=" +
|
||||
encodeURIComponent(item.id)].concat(item.children.map(arguments.callee));
|
||||
}).flatten().join('&');
|
||||
} else {
|
||||
return Sortable.sequence(element, arguments[1]).map( function(item) {
|
||||
return name + "[]=" + encodeURIComponent(item);
|
||||
}).join('&');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if child is contained within element
|
||||
Element.isParent = function(child, element) {
|
||||
if (!child.parentNode || child == element) return false;
|
||||
if (child.parentNode == element) return true;
|
||||
return Element.isParent(child.parentNode, element);
|
||||
}
|
||||
|
||||
Element.findChildren = function(element, only, recursive, tagName) {
|
||||
if(!element.hasChildNodes()) return null;
|
||||
tagName = tagName.toUpperCase();
|
||||
if(only) only = [only].flatten();
|
||||
var elements = [];
|
||||
$A(element.childNodes).each( function(e) {
|
||||
if(e.tagName && e.tagName.toUpperCase()==tagName &&
|
||||
(!only || (Element.classNames(e).detect(function(v) { return only.include(v) }))))
|
||||
elements.push(e);
|
||||
if(recursive) {
|
||||
var grandchildren = Element.findChildren(e, only, recursive, tagName);
|
||||
if(grandchildren) elements.push(grandchildren);
|
||||
}
|
||||
});
|
||||
|
||||
return (elements.length>0 ? elements.flatten() : []);
|
||||
}
|
||||
|
||||
Element.offsetSize = function (element, type) {
|
||||
return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')];
|
||||
}
|
||||
1088
vendor/plugins/shoulda/test/rails_root/public/javascripts/effects.js
vendored
Normal file
1088
vendor/plugins/shoulda/test/rails_root/public/javascripts/effects.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
2515
vendor/plugins/shoulda/test/rails_root/public/javascripts/prototype.js
vendored
Normal file
2515
vendor/plugins/shoulda/test/rails_root/public/javascripts/prototype.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1
vendor/plugins/shoulda/test/rails_root/public/robots.txt
vendored
Normal file
1
vendor/plugins/shoulda/test/rails_root/public/robots.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
# See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
|
||||
74
vendor/plugins/shoulda/test/rails_root/public/stylesheets/scaffold.css
vendored
Normal file
74
vendor/plugins/shoulda/test/rails_root/public/stylesheets/scaffold.css
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
body { background-color: #fff; color: #333; }
|
||||
|
||||
body, p, ol, ul, td {
|
||||
font-family: verdana, arial, helvetica, sans-serif;
|
||||
font-size: 13px;
|
||||
line-height: 18px;
|
||||
}
|
||||
|
||||
pre {
|
||||
background-color: #eee;
|
||||
padding: 10px;
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
a { color: #000; }
|
||||
a:visited { color: #666; }
|
||||
a:hover { color: #fff; background-color:#000; }
|
||||
|
||||
.fieldWithErrors {
|
||||
padding: 2px;
|
||||
background-color: red;
|
||||
display: table;
|
||||
}
|
||||
|
||||
#errorExplanation {
|
||||
width: 400px;
|
||||
border: 2px solid red;
|
||||
padding: 7px;
|
||||
padding-bottom: 12px;
|
||||
margin-bottom: 20px;
|
||||
background-color: #f0f0f0;
|
||||
}
|
||||
|
||||
#errorExplanation h2 {
|
||||
text-align: left;
|
||||
font-weight: bold;
|
||||
padding: 5px 5px 5px 15px;
|
||||
font-size: 12px;
|
||||
margin: -7px;
|
||||
background-color: #c00;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
#errorExplanation p {
|
||||
color: #333;
|
||||
margin-bottom: 0;
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
#errorExplanation ul li {
|
||||
font-size: 12px;
|
||||
list-style: square;
|
||||
}
|
||||
|
||||
div.uploadStatus {
|
||||
margin: 5px;
|
||||
}
|
||||
|
||||
div.progressBar {
|
||||
margin: 5px;
|
||||
}
|
||||
|
||||
div.progressBar div.border {
|
||||
background-color: #fff;
|
||||
border: 1px solid grey;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
div.progressBar div.background {
|
||||
background-color: #333;
|
||||
height: 18px;
|
||||
width: 0%;
|
||||
}
|
||||
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/about
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/about
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../config/boot'
|
||||
require 'commands/about'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/breakpointer
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/breakpointer
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../config/boot'
|
||||
require 'commands/breakpointer'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/console
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/console
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../config/boot'
|
||||
require 'commands/console'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/destroy
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/destroy
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../config/boot'
|
||||
require 'commands/destroy'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/generate
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/generate
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../config/boot'
|
||||
require 'commands/generate'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/performance/benchmarker
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/performance/benchmarker
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../../config/boot'
|
||||
require 'commands/performance/benchmarker'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/performance/profiler
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/performance/profiler
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../../config/boot'
|
||||
require 'commands/performance/profiler'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/plugin
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/plugin
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../config/boot'
|
||||
require 'commands/plugin'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/process/inspector
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/process/inspector
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../../config/boot'
|
||||
require 'commands/process/inspector'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/process/reaper
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/process/reaper
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../../config/boot'
|
||||
require 'commands/process/reaper'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/process/spawner
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/process/spawner
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../../config/boot'
|
||||
require 'commands/process/spawner'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/runner
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/runner
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../config/boot'
|
||||
require 'commands/runner'
|
||||
3
vendor/plugins/shoulda/test/rails_root/script/server
vendored
Executable file
3
vendor/plugins/shoulda/test/rails_root/script/server
vendored
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
require File.dirname(__FILE__) + '/../config/boot'
|
||||
require 'commands/server'
|
||||
35
vendor/plugins/shoulda/test/test_helper.rb
vendored
Normal file
35
vendor/plugins/shoulda/test/test_helper.rb
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
require 'fileutils'
|
||||
# Load the environment
|
||||
ENV['RAILS_ENV'] = 'sqlite3'
|
||||
|
||||
# ln rails_root/vendor/plugins/shoulda => ../../../../
|
||||
rails_root = File.dirname(__FILE__) + '/rails_root'
|
||||
|
||||
FileUtils.ln_s('../../../../', "#{rails_root}/vendor/plugins/shoulda") unless File.exists?("#{rails_root}/vendor/plugins/shoulda")
|
||||
|
||||
require "#{rails_root}/config/environment.rb"
|
||||
|
||||
# Load the testing framework
|
||||
require 'test_help'
|
||||
silence_warnings { RAILS_ENV = ENV['RAILS_ENV'] }
|
||||
|
||||
# Run the migrations
|
||||
ActiveRecord::Migration.verbose = false
|
||||
ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
|
||||
|
||||
# Setup the fixtures path
|
||||
Test::Unit::TestCase.fixture_path = File.join(File.dirname(__FILE__), "fixtures")
|
||||
# $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
|
||||
|
||||
class Test::Unit::TestCase #:nodoc:
|
||||
def create_fixtures(*table_names)
|
||||
if block_given?
|
||||
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
|
||||
else
|
||||
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
|
||||
end
|
||||
end
|
||||
|
||||
self.use_transactional_fixtures = false
|
||||
self.use_instantiated_fixtures = false
|
||||
end
|
||||
6
vendor/plugins/shoulda/test/unit/dog_test.rb
vendored
Normal file
6
vendor/plugins/shoulda/test/unit/dog_test.rb
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class DogTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
should_belong_to :user
|
||||
end
|
||||
14
vendor/plugins/shoulda/test/unit/post_test.rb
vendored
Normal file
14
vendor/plugins/shoulda/test/unit/post_test.rb
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class PostTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
|
||||
should_belong_to :user
|
||||
should_belong_to :owner
|
||||
should_have_many :tags, :through => :taggings
|
||||
|
||||
should_require_unique_attributes :title
|
||||
should_require_attributes :body, :message => /wtf/
|
||||
should_require_attributes :title
|
||||
should_only_allow_numeric_values_for :user_id
|
||||
end
|
||||
8
vendor/plugins/shoulda/test/unit/tag_test.rb
vendored
Normal file
8
vendor/plugins/shoulda/test/unit/tag_test.rb
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class TagTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
|
||||
should_have_many :taggings
|
||||
should_have_many :posts
|
||||
end
|
||||
8
vendor/plugins/shoulda/test/unit/tagging_test.rb
vendored
Normal file
8
vendor/plugins/shoulda/test/unit/tagging_test.rb
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class TaggingTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
|
||||
should_belong_to :post
|
||||
should_belong_to :tag
|
||||
end
|
||||
20
vendor/plugins/shoulda/test/unit/user_test.rb
vendored
Normal file
20
vendor/plugins/shoulda/test/unit/user_test.rb
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class UserTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
|
||||
should_have_many :posts
|
||||
should_have_many :dogs
|
||||
|
||||
should_not_allow_values_for :email, "blah", "b lah"
|
||||
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
|
||||
should_ensure_length_in_range :email, 1..100
|
||||
should_ensure_value_in_range :age, 1..100
|
||||
should_protect_attributes :password
|
||||
should_have_class_methods :find, :destroy
|
||||
should_have_instance_methods :email, :age, :email=, :valid?
|
||||
should_have_db_columns :name, :email, :age
|
||||
should_have_db_column :id, :type => "integer", :primary => true
|
||||
should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
|
||||
:null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
|
||||
end
|
||||
Reference in New Issue
Block a user