1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Moved support into Active Support

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@277 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2004-12-29 21:14:58 +00:00
parent 84d5519b74
commit 4290267959
9 changed files with 57 additions and 885 deletions

View file

@ -36,19 +36,31 @@ module ActiveRecord
configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
if configuration[:scope].is_a?(Symbol)
scope_condition_method = %(
def scope_condition
if #{configuration[:scope].to_s}.nil?
"#{configuration[:scope].to_s} IS NULL"
else
"#{configuration[:scope].to_s} = \#{#{configuration[:scope].to_s}}"
end
end
)
else
scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end"
end
class_eval <<-EOV
include ActiveRecord::Acts::List::InstanceMethods
def position_column
'#{configuration[:column]}'
end
def scope_condition
"#{configuration[:scope].is_a?(Symbol) ? configuration[:scope].to_s + " = \#{" + configuration[:scope].to_s + "}" : configuration[:scope]}"
end
#{scope_condition_method}
before_destroy :remove_from_list
before_create :add_to_list_bottom
before_create :add_to_list_bottom
EOV
end
end
@ -123,55 +135,54 @@ module ActiveRecord
end
private
def add_to_list_top
increment_positions_on_all_items
end
def add_to_list_top
increment_positions_on_all_items
end
def add_to_list_bottom
write_attribute(position_column, bottom_position_in_list.to_i + 1)
end
def add_to_list_bottom
write_attribute(position_column, bottom_position_in_list.to_i + 1)
end
# Overwrite this method to define the scope of the list changes
def scope_condition() "1" end
# Overwrite this method to define the scope of the list changes
def scope_condition() "1" end
def bottom_position_in_list
item = bottom_item
item ? item.send(position_column) : 0
end
def bottom_position_in_list
item = bottom_item
item ? item.send(position_column) : 0
end
def bottom_item
self.class.find_first(
"#{scope_condition} ",
"#{position_column} DESC"
)
end
def bottom_item
self.class.find_first(
"#{scope_condition} ",
"#{position_column} DESC"
)
end
def assume_bottom_position
update_attribute position_column, bottom_position_in_list.to_i + 1
end
def assume_bottom_position
update_attribute position_column, bottom_position_in_list.to_i + 1
end
def assume_top_position
update_attribute position_column, 1
end
def assume_top_position
update_attribute position_column, 1
end
def decrement_positions_on_lower_items
self.class.update_all(
"#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}"
)
end
def decrement_positions_on_lower_items
self.class.update_all(
"#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}"
)
end
def increment_positions_on_higher_items
self.class.update_all(
"#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column).to_i}"
)
end
def increment_positions_on_higher_items
self.class.update_all(
"#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column).to_i}"
)
end
def increment_positions_on_all_items
self.class.update_all(
"#{position_column} = (#{position_column} + 1)", "#{scope_condition}"
)
end
def increment_positions_on_all_items
self.class.update_all(
"#{position_column} = (#{position_column} + 1)", "#{scope_condition}"
)
end
end
end
end

View file

@ -3,30 +3,6 @@ require 'active_record/associations/has_many_association'
require 'active_record/associations/has_and_belongs_to_many_association'
require 'active_record/deprecated_associations'
unless Object.respond_to?(:require_association)
Object.send(:define_method, :require_association) { |file_name| ActiveRecord::Base.require_association(file_name) }
end
class Object
class << self
# Use const_missing to autoload associations so we don't have to
# require_association when using single-table inheritance.
unless respond_to?(:pre_association_const_missing)
alias_method :pre_association_const_missing, :const_missing
def const_missing(class_id)
begin
require_association(Inflector.underscore(Inflector.demodulize(class_id.to_s)))
return Object.const_get(class_id) if Object.const_defined?(class_id) && Object.const_get(class_id).ancestors.include?(ActiveRecord::Base)
rescue LoadError
pre_association_const_missing(class_id)
end
end
end
end
end
module ActiveRecord
module Associations # :nodoc:
def self.append_features(base)
@ -478,31 +454,6 @@ module ActiveRecord
deprecated_has_collection_method(association_name)
end
# Loads the <tt>file_name</tt> if reload_associations is true or requires if it's false.
def require_association(file_name)
if !associations_loaded.include?(file_name)
associations_loaded << file_name
reload_associations ? silence_warnings { load("#{file_name}.rb") } : require(file_name)
end
end
# Resets the list of dependencies loaded (typically to be called by the end of a request), so when require_association is
# called for that dependency it'll be loaded anew.
def reset_associations_loaded
self.associations_loaded = []
end
# Reload all the associations that have already been loaded once.
def reload_associations_loaded
associations_loaded.each do |file_name|
begin
silence_warnings { load("#{file_name}.rb") }
rescue LoadError
# The association didn't reside in its own file, so we assume it was required by other means
end
end
end
private
# Raises an exception if an invalid option has been specified to prevent misspellings from slipping through
def validate_options(valid_option_keys, supplied_option_keys)
@ -619,13 +570,7 @@ module ActiveRecord
end
def require_association_class(class_name)
return unless class_name
begin
require_association(Inflector.underscore(class_name))
rescue LoadError
# Failed to load the associated class -- let's hope the developer is doing the requiring himself.
end
require_association(Inflector.underscore(class_name)) if class_name
end
end
end

View file

@ -1,81 +0,0 @@
begin
require 'simplecc'
rescue LoadError
def Continuation.create(*args, &block)
cc = nil; result = callcc {|c| cc = c; block.call(cc) if block and args.empty?}
result ||= args
return *[cc, *result]
end
end
# This method returns the binding of the method that called your
# method. It will raise an Exception when you're not inside a method.
#
# It's used like this:
# def inc_counter(amount = 1)
# Binding.of_caller do |binding|
# # Create a lambda that will increase the variable 'counter'
# # in the caller of this method when called.
# inc = eval("lambda { |arg| counter += arg }", binding)
# # We can refer to amount from inside this block safely.
# inc.call(amount)
# end
# # No other statements can go here. Put them inside the block.
# end
# counter = 0
# 2.times { inc_counter }
# counter # => 2
#
# Binding.of_caller must be the last statement in the method.
# This means that you will have to put everything you want to
# do after the call to Binding.of_caller into the block of it.
# This should be no problem however, because Ruby has closures.
# If you don't do this an Exception will be raised. Because of
# the way that Binding.of_caller is implemented it has to be
# done this way.
def Binding.of_caller(&block)
old_critical = Thread.critical
Thread.critical = true
count = 0
cc, result, error, extra_data = Continuation.create(nil, nil)
error.call if error
tracer = lambda do |*args|
type, context, extra_data = args[0], args[4], args
if type == "return"
count += 1
# First this method and then calling one will return --
# the trace event of the second event gets the context
# of the method which called the method that called this
# method.
if count == 2
# It would be nice if we could restore the trace_func
# that was set before we swapped in our own one, but
# this is impossible without overloading set_trace_func
# in current Ruby.
set_trace_func(nil)
cc.call(eval("binding", context), nil, extra_data)
end
elsif type == "line" then
nil
elsif type == "c-return" and extra_data[3] == :set_trace_func then
nil
else
set_trace_func(nil)
error_msg = "Binding.of_caller used in non-method context or " +
"trailing statements of method using it aren't in the block."
cc.call(nil, lambda { raise(ArgumentError, error_msg) }, nil)
end
end
unless result
set_trace_func(tracer)
return nil
else
Thread.critical = old_critical
case block.arity
when 1 then yield(result)
else yield(result, extra_data)
end
end
end

View file

@ -1,525 +0,0 @@
# The Breakpoint library provides the convenience of
# being able to inspect and modify state, diagnose
# bugs all via IRB by simply setting breakpoints in
# your applications by the call of a method.
#
# This library was written and is supported by me,
# Florian Gross. I can be reached at flgr@ccan.de
# and enjoy getting feedback about my libraries.
#
# The whole library (including breakpoint_client.rb
# and binding_of_caller.rb) is licensed under the
# same license that Ruby uses. (Which is currently
# either the GNU General Public License or a custom
# one that allows for commercial usage.) If you for
# some good reason need to use this under another
# license please contact me.
require 'irb'
# require 'binding_of_caller' <- Needs this
require 'drb'
require 'drb/acl'
module Breakpoint
extend self
# This will pop up an interactive ruby session at a
# pre-defined break point in a Ruby application. In
# this session you can examine the environment of
# the break point.
#
# You can get a list of variables in the context using
# local_variables via +local_variables+. You can then
# examine their values by typing their names.
#
# You can have a look at the call stack via +caller+.
#
# The source code around the location where the breakpoint
# was executed can be examined via +source_lines+. Its
# argument specifies how much lines of context to display.
# The default amount of context is 5 lines. Note that
# the call to +source_lines+ can raise an exception when
# it isn't able to read in the source code.
#
# breakpoints can also return a value. They will execute
# a supplied block for getting a default return value.
# A custom value can be returned from the session by doing
# +throw(:debug_return, value)+.
#
# You can also give names to break points which will be
# used in the message that is displayed upon execution
# of them.
#
# Here's a sample of how breakpoints should be placed:
#
# class Person
# def initialize(name, age)
# @name, @age = name, age
# breakpoint("Person#initialize")
# end
#
# attr_reader :age
# def name
# breakpoint("Person#name") { @name }
# end
# end
#
# person = Person.new("Random Person", 23)
# puts "Name: #{person.name}"
#
# And here is a sample debug session:
#
# Executing break point "Person#initialize" at file.rb:4 in `initialize'
# irb(#<Person:0x292fbe8>):001:0> local_variables
# => ["name", "age", "_", "__"]
# irb(#<Person:0x292fbe8>):002:0> [name, age]
# => ["Random Person", 23]
# irb(#<Person:0x292fbe8>):003:0> [@name, @age]
# => ["Random Person", 23]
# irb(#<Person:0x292fbe8>):004:0> self
# => #<Person:0x292fbe8 @age=23, @name="Random Person">
# irb(#<Person:0x292fbe8>):005:0> @age += 1; self
# => #<Person:0x292fbe8 @age=24, @name="Random Person">
# irb(#<Person:0x292fbe8>):006:0> exit
# Executing break point "Person#name" at file.rb:9 in `name'
# irb(#<Person:0x292fbe8>):001:0> throw(:debug_return, "Overriden name")
# Name: Overriden name
#
# Breakpoint sessions will automatically have a few
# convenience methods available. See Breakpoint::CommandBundle
# for a list of them.
#
# Breakpoints can also be used remotely over sockets.
# This is implemented by running part of the IRB session
# in the application and part of it in a special client.
# You have to call Breakpoint.activate_drb to enable
# support for remote breakpoints and then run
# breakpoint_client.rb which is distributed with this
# library. See the documentation of Breakpoint.activate_drb
# for details.
def breakpoint(id = nil, context = nil, &block)
callstack = caller
callstack.slice!(0, 3) if callstack.first["breakpoint"]
file, line, method = *callstack.first.match(/^(.+?):(\d+)(?::in `(.*?)')?/).captures
message = "Executing break point " + (id ? "#{id.inspect} " : "") +
"at #{file}:#{line}" + (method ? " in `#{method}'" : "")
if context then
return handle_breakpoint(context, message, file, line, &block)
end
Binding.of_caller do |binding_context|
handle_breakpoint(binding_context, message, file, line, &block)
end
end
module CommandBundle #:nodoc:
# Proxy to a Breakpoint client. Lets you directly execute code
# in the context of the client.
class Client#:nodoc:
def initialize(eval_handler) # :nodoc:
@eval_handler = eval_handler
end
instance_methods.each do |method|
next if method[/^__.+__$/]
undef_method method
end
# Executes the specified code at the client.
def eval(code)
@eval_handler.call(code)
end
# Will execute the specified statement at the client.
def method_missing(method, *args)
if args.empty?
result = eval("#{method}")
else
result = eval("#{method}(*Marshal.load(#{Marshal.dump(args).inspect}))")
end
unless [true, false, nil].include?(result)
result.extend(DRbUndumped) if result
end
return result
end
end
# Returns the source code surrounding the location where the
# breakpoint was issued.
def source_lines(context = 5, return_line_numbers = false)
lines = File.readlines(@__bp_file).map { |line| line.chomp }
break_line = @__bp_line
start_line = [break_line - context, 1].max
end_line = break_line + context
result = lines[(start_line - 1) .. (end_line - 1)]
if return_line_numbers then
return [start_line, break_line, result]
else
return result
end
end
# Lets an object that will forward method calls to the breakpoint
# client. This is useful for outputting longer things at the client
# and so on. You can for example do these things:
#
# client.puts "Hello" # outputs "Hello" at client console
# # outputs "Hello" into the file temp.txt at the client
# client.File.open("temp.txt", "w") { |f| f.puts "Hello" }
def client()
if Breakpoint.use_drb? then
Client.new(Breakpoint.drb_service.eval_handler)
else
Client.new(lambda { |code| eval(code, TOPLEVEL_BINDING) })
end
end
end
def handle_breakpoint(context, message, file = "", line = "", &block) # :nodoc:
catch(:debug_return) do |value|
eval(%{
@__bp_file = #{file.inspect}
@__bp_line = #{line}
extend Breakpoint::CommandBundle
extend DRbUndumped if self
}, context) rescue nil
if not use_drb? then
puts message
IRB.start(nil, IRB::WorkSpace.new(context))
else
@drb_service.add_breakpoint(context, message)
end
block.call if block
end
end
# These exceptions will be raised on failed asserts
# if Breakpoint.asserts_cause_exceptions is set to
# true.
class FailedAssertError < RuntimeError#:nodoc:
end
# This asserts that the block evaluates to true.
# If it doesn't evaluate to true a breakpoint will
# automatically be created at that execution point.
#
# You can disable assert checking in production
# code by setting Breakpoint.optimize_asserts to
# true. (It will still be enabled when Ruby is run
# via the -d argument.)
#
# Example:
# person_name = "Foobar"
# assert { not person_name.nil? }
#
# Note: If you want to use this method from an
# unit test, you will have to call it by its full
# name, Breakpoint.assert.
def assert(context = nil, &condition)
return if Breakpoint.optimize_asserts and not $DEBUG
return if yield
callstack = caller
callstack.slice!(0, 3) if callstack.first["assert"]
file, line, method = *callstack.first.match(/^(.+?):(\d+)(?::in `(.*?)')?/).captures
message = "Assert failed at #{file}:#{line}#{" in `#{method}'" if method}."
if Breakpoint.asserts_cause_exceptions and not $DEBUG then
raise(Breakpoint::FailedAssertError, message)
end
message += " Executing implicit breakpoint."
if context then
return handle_breakpoint(context, message, file, line)
end
Binding.of_caller do |context|
handle_breakpoint(context, message, file, line)
end
end
# Whether asserts should be ignored if not in debug mode.
# Debug mode can be enabled by running ruby with the -d
# switch or by setting $DEBUG to true.
attr_accessor :optimize_asserts
self.optimize_asserts = false
# Whether an Exception should be raised on failed asserts
# in non-$DEBUG code or not. By default this is disabled.
attr_accessor :asserts_cause_exceptions
self.asserts_cause_exceptions = false
@use_drb = false
attr_reader :drb_service # :nodoc:
class DRbService # :nodoc:
include DRbUndumped
def initialize
@handler = @eval_handler = @collision_handler = nil
IRB.instance_eval { @CONF[:RC] = true }
IRB.run_config
end
def collision
sleep(0.5) until @collision_handler
@collision_handler.call
end
def ping; end
def add_breakpoint(context, message)
workspace = IRB::WorkSpace.new(context)
workspace.extend(DRbUndumped)
sleep(0.5) until @handler
@handler.call(workspace, message)
end
def register_handler(&block)
@handler = block
end
def unregister_handler
@handler = nil
end
attr_reader :eval_handler
def register_eval_handler(&block)
@eval_handler = block
end
def unregister_eval_handler
@eval_handler = lambda { }
end
def register_collision_handler(&block)
@collision_handler = block
end
def unregister_collision_handler
@collision_handler = lambda { }
end
end
# Will run Breakpoint in DRb mode. This will spawn a server
# that can be attached to via the breakpoint-client command
# whenever a breakpoint is executed. This is useful when you
# are debugging CGI applications or other applications where
# you can't access debug sessions via the standard input and
# output of your application.
#
# You can specify an URI where the DRb server will run at.
# This way you can specify the port the server runs on. The
# default URI is druby://localhost:42531.
#
# Please note that breakpoints will be skipped silently in
# case the DRb server can not spawned. (This can happen if
# the port is already used by another instance of your
# application on CGI or another application.)
#
# Also note that by default this will only allow access
# from localhost. You can however specify a list of
# allowed hosts or nil (to allow access from everywhere).
# But that will still not protect you from somebody
# reading the data as it goes through the net.
#
# A good approach for getting security and remote access
# is setting up an SSH tunnel between the DRb service
# and the client. This is usually done like this:
#
# $ ssh -L20000:127.0.0.1:20000 -R10000:127.0.0.1:10000 example.com
# (This will connect port 20000 at the client side to port
# 20000 at the server side, and port 10000 at the server
# side to port 10000 at the client side.)
#
# After that do this on the server side: (the code being debugged)
# Breakpoint.activate_drb("druby://127.0.0.1:20000", "localhost")
#
# And at the client side:
# ruby breakpoint_client.rb -c druby://127.0.0.1:10000 -s druby://127.0.0.1:20000
#
# Running through such a SSH proxy will also let you use
# breakpoint.rb in case you are behind a firewall.
#
# Detailed information about running DRb through firewalls is
# available at http://www.rubygarden.org/ruby?DrbTutorial
def activate_drb(uri = nil, allowed_hosts = ['localhost', '127.0.0.1', '::1'], ignore_collisions = false) #:nodoc:
return false if @use_drb
uri ||= 'druby://localhost:42531'
if allowed_hosts then
acl = ["deny", "all"]
Array(allowed_hosts).each do |host|
acl += ["allow", host]
end
DRb.install_acl(ACL.new(acl))
end
@use_drb = true
@drb_service = DRbService.new
did_collision = false
begin
@service = DRb.start_service(uri, @drb_service)
rescue Errno::EADDRINUSE
if ignore_collisions then
nil
else
# The port is already occupied by another
# Breakpoint service. We will try to tell
# the old service that we want its port.
# It will then forward that request to the
# user and retry.
unless did_collision then
DRbObject.new(nil, uri).collision
did_collision = true
end
sleep(10)
retry
end
end
return true
end
# Deactivates a running Breakpoint service.
def deactivate_drb #:nodoc:
@service.stop_service unless @service.nil?
@service = nil
@use_drb = false
@drb_service = nil
end
# Returns true when Breakpoints are used over DRb.
# Breakpoint.activate_drb causes this to be true.
def use_drb? #:nodoc:
@use_drb == true
end
end
module IRB # :nodoc:
class << self; remove_method :start; end
def self.start(ap_path = nil, main_context = nil, workspace = nil)
$0 = File::basename(ap_path, ".rb") if ap_path
# suppress some warnings about redefined constants
old_verbose, $VERBOSE = $VERBOSE, nil
IRB.setup(ap_path)
$VERBOSE = old_verbose
if @CONF[:SCRIPT] then
irb = Irb.new(main_context, @CONF[:SCRIPT])
else
irb = Irb.new(main_context)
end
if workspace then
irb.context.workspace = workspace
end
@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
@CONF[:MAIN_CONTEXT] = irb.context
old_sigint = trap("SIGINT") do
irb.signal_handle
end
catch(:IRB_EXIT) do
irb.eval_input
end
ensure
trap("SIGINT", old_sigint)
end
class << self
alias :old_CurrentContext :CurrentContext
remove_method :CurrentContext
end
def IRB.CurrentContext
if old_CurrentContext.nil? and Breakpoint.use_drb? then
result = Object.new
def result.last_value; end
return result
else
old_CurrentContext
end
end
class Context#:nodoc:
alias :old_evaluate :evaluate
def evaluate(line, line_no)
if line.chomp == "exit" then
exit
else
old_evaluate(line, line_no)
end
end
end
class WorkSpace#:nodoc:
alias :old_evaluate :evaluate
def evaluate(*args)
if Breakpoint.use_drb? then
result = old_evaluate(*args)
if args[0] != :no_proxy and
not [true, false, nil].include?(result)
then
result.extend(DRbUndumped) rescue nil
end
return result
else
old_evaluate(*args)
end
end
end
module InputCompletor#:nodoc:
def self.eval(code, context, *more)
# Big hack, this assumes that InputCompletor
# will only call eval() when it wants code
# to be executed in the IRB context.
IRB.conf[:MAIN_CONTEXT].workspace.evaluate(:no_proxy, code, *more)
end
end
end
module DRb # :nodoc:
class DRbObject#:nodoc:
undef :inspect
undef :clone
end
end
# See Breakpoint.breakpoint
def breakpoint(id = nil, &block)
Binding.of_caller do |context|
Breakpoint.breakpoint(id, context, &block)
end
end
# See Breakpoint.assert
def assert(&block)
Binding.of_caller do |context|
Breakpoint.assert(context, &block)
end
end

View file

@ -1,43 +0,0 @@
# attr_* style accessors for class-variables that can accessed both on an instance and class level.
class Class #:nodoc:
def cattr_reader(*syms)
syms.each do |sym|
class_eval <<-EOS
if ! defined? @@#{sym.id2name}
@@#{sym.id2name} = nil
end
def self.#{sym.id2name}
@@#{sym}
end
def #{sym.id2name}
self.class.#{sym.id2name}
end
EOS
end
end
def cattr_writer(*syms)
syms.each do |sym|
class_eval <<-EOS
if ! defined? @@#{sym.id2name}
@@#{sym.id2name} = nil
end
def self.#{sym.id2name}=(obj)
@@#{sym.id2name} = obj
end
def #{sym.id2name}=(obj)
self.class.#{sym.id2name}=(obj)
end
EOS
end
end
def cattr_accessor(*syms)
cattr_reader(*syms)
cattr_writer(*syms)
end
end

View file

@ -1,40 +0,0 @@
# Allows attributes to be shared within an inheritance hierarchy, but where each descentent gets a copy of
# their parents' attributes, instead of just a pointer to the same. This means that the child can add elements
# to, for example, an array without those additions being shared with either their parent, siblings, or
# children, which is unlike the regular class-level attributes that are shared across the entire hierarchy.
module ClassInheritableAttributes # :nodoc:
def self.append_features(base)
super
base.extend(ClassMethods)
end
module ClassMethods # :nodoc:
@@classes ||= {}
def inheritable_attributes
@@classes[self] ||= {}
end
def write_inheritable_attribute(key, value)
inheritable_attributes[key] = value
end
def write_inheritable_array(key, elements)
write_inheritable_attribute(key, []) if read_inheritable_attribute(key).nil?
write_inheritable_attribute(key, read_inheritable_attribute(key) + elements)
end
def read_inheritable_attribute(key)
inheritable_attributes[key]
end
def reset_inheritable_attributes
inheritable_attributes.clear
end
private
def inherited(child)
@@classes[child] = inheritable_attributes.dup
end
end
end

View file

@ -1,10 +0,0 @@
require 'logger'
class Logger #:nodoc:
private
remove_const "Format"
Format = "%s\n"
def format_message(severity, timestamp, msg, progname)
Format % [msg]
end
end

View file

@ -1,79 +0,0 @@
# The Inflector transforms words from singular to plural, class names to table names, modulized class names to ones without,
# and class names to foreign keys.
module Inflector
extend self
def pluralize(word)
result = word.dup
plural_rules.each do |(rule, replacement)|
break if result.gsub!(rule, replacement)
end
return result
end
def singularize(word)
result = word.dup
singular_rules.each do |(rule, replacement)|
break if result.gsub!(rule, replacement)
end
return result
end
def camelize(lower_case_and_underscored_word)
lower_case_and_underscored_word.gsub(/(^|_)(.)/){$2.upcase}
end
def underscore(camel_cased_word)
camel_cased_word.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase
end
def demodulize(class_name_in_module)
class_name_in_module.gsub(/^.*::/, '')
end
def tableize(class_name)
pluralize(underscore(class_name))
end
def classify(table_name)
camelize(singularize(table_name))
end
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
Inflector.underscore(Inflector.demodulize(class_name)) +
(separate_class_name_and_id_with_underscore ? "_id" : "id")
end
private
def plural_rules #:doc:
[
[/(x|ch|ss)$/, '\1es'], # search, switch, fix, box, process, address
[/([^aeiouy]|qu)y$/, '\1ies'], # query, ability, agency
[/(?:([^f])fe|([lr])f)$/, '\1\2ves'], # half, safe, wife
[/sis$/, 'ses'], # basis, diagnosis
[/([ti])um$/, '\1a'], # datum, medium
[/person$/, 'people'], # person, salesperson
[/man$/, 'men'], # man, woman, spokesman
[/child$/, 'children'], # child
[/s$/, 's'], # no change (compatibility)
[/$/, 's']
]
end
def singular_rules #:doc:
[
[/(x|ch|ss)es$/, '\1'],
[/movies$/, 'movie'],
[/([^aeiouy]|qu)ies$/, '\1y'],
[/([lr])ves$/, '\1f'],
[/([^f])ves$/, '\1fe'],
[/(analy|ba|diagno|parenthe|progno|synop|the)ses$/, '\1sis'],
[/([ti])a$/, '\1um'],
[/people$/, 'person'],
[/men$/, 'man'],
[/status$/, 'status'],
[/children$/, 'child'],
[/s$/, '']
]
end
end

View file

@ -1,6 +0,0 @@
def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
result = yield
$VERBOSE = old_verbose
return result
end