# Changes to core methods

**URL:** <https://www.ruby-forum.com/t/changes-to-core-methods/104354>\
**Category:** Rails\
**Created:** [May 17, 2007, 1:47am UTC](https://www.ruby-forum.com/t/changes-to-core-methods/104354 "2007-05-17T01:47:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![keynan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@keynan](https://www.ruby-forum.com/u/keynan)\
**Post date:** [May 17, 2007, 1:47am UTC](https://www.ruby-forum.com/t/changes-to-core-methods/104354/1 "2007-05-17T01:47:09Z")

</div>

##I am trying to get these put into the next version of rails

## any help or suggestions would be great thanks

## from prototype helper in action view

def remote\_funtion(options)

function = []  
if options[:requests] and options[:requests].is\_a?Array  
options[:requests].each{ |request| function \<\<  
make\_ajax\_object(request) }  
else  
function \<\< make\_ajax\_object(options)  
end  
function = function.join(‘;’)  
function = “if (#{options[:condition]}) { #{function}; }” if  
options[:condition]  
function = “if (confirm(‘#{escape\_javascript(options[:confirm])}’)) {  
#{function}; }” if options[:confirm]

return function  
end

def make\_ajax\_object(options)

javascript\_options = options\_for\_ajax(options)

update = ‘’  
if options[:update] && options[:update].is\_a?(Hash)  
update = []  
update \<\< “success:‘#{options[:update][:success]}’” if  
options[:update][:success]  
update \<\< “failure:‘#{options[:update][:failure]}’” if  
options[:update][:failure]  
update = ‘{’ + update.join(‘,’) + ‘}’  
elsif options[:update]  
update \<\< “‘#{options[:update]}’”  
end

function = update.empty? ?  
[“new Ajax.Request(”] :  
["new Ajax.Updater(#{update}, "]

url\_options = options[:url]  
url\_options = url\_options.merge(:escape =\> false) if  
url\_options.is\_a?(Hash)  
function \<\< “‘#{url\_for(url\_options)}’”  
function \<\< “, #{javascript\_options})”

function \<\< “#{options[:before]}; #{function}” if options[:before]  
function \<\< “#{function}; #{options[:after]}” if options[:after]

return function.join

end

## Callbacks modual in active record callbacks

CALLBACKS = %w(  
after\_find after\_initialize before\_save after\_save before\_create  
after\_create before\_update after\_update before\_validation  
after\_validation before\_validation\_on\_create  
after\_validation\_on\_create before\_validation\_on\_update  
after\_validation\_on\_update before\_destroy after\_destroy  
before\_destroy\_dependencies after\_destroy\_dependencies  
)

def destroy\_dependents\_with\_callbacks(&block)  
callback(:before\_destroy\_dependencies)  
block.call  
callback(:after\_destroy\_dependencies)  
end

## In active recored associations

def configure\_dependency\_for\_has\_many(reflection)  
if reflection.options[:dependent] == true  
::ActiveSupport::Deprecation.warn(“The :dependent =\> true option is  
deprecated and will be removed from Rails 2.0. Please use :dependent =\>  
:destroy instead. See [Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.](http://www.rubyonrails.org/deprecation) for  
details.”, caller)  
end

if reflection.options[:dependent] &&  
reflection.options[:exclusively\_dependent]  
raise ArgumentError, ‘:dependent and :exclusively\_dependent are  
mutually exclusive options. You may specify one or the other.’  
end

if reflection.options[:exclusively\_dependent]  
reflection.options[:dependent] = :delete\_all  
::ActiveSupport::Deprecation.warn(“The :exclusively\_dependent option  
is deprecated and will be removed from Rails 2.0. Please use :dependent  
=\> :delete\_all instead. See [Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.](http://www.rubyonrails.org/deprecation) for  
details.”, caller)  
end

# See HasManyAssociation#delete\_records. Dependent associations

# delete children, otherwise foreign key is set to NULL.

# Add polymorphic type if the :as option is present

dependent\_conditions = %(#{reflection.primary\_key\_name} =  
#{record.quoted\_id})  
if reflection.options[:as]  
dependent\_conditions += " AND #{reflection.options[:as]}\_type =  
‘#{base\_class.name}’"  
end

case reflection.options[:dependent]  
when :destroy, true  
module\_eval “before\_destroy  
‘destroy\_dependents\_with\_callbacks{#{reflection.name}.each { |o|  
o.destroy }}’”  
when :delete\_all  
module\_eval “before\_destroy { |record|  
destroy\_dependents\_with\_callbacks{#{reflection.class\_name}.delete\_all(%(#{dependent\_conditions}))  
}}”  
when :nullify  
module\_eval “before\_destroy { |record|  
destroy\_dependents\_with\_callbacks{#{reflection.class\_name}.update\_all(%(#{reflection.primary\_key\_name}  
= NULL), %(#{dependent\_conditions})) }}”  
when nil, false  
# pass  
else  
raise ArgumentError, ‘The :dependent option expects either :destroy,  
:delete\_all, or :nullify’  
end  
end

def configure\_dependency\_for\_has\_one(reflection)  
case reflection.options[:dependent]  
when :destroy, true  
module\_eval “before\_destroy  
‘destroy\_dependents\_with\_callbacks{#{reflection.name}.destroy unless  
#{reflection.name}.nil?}’”  
when :delete  
module\_eval “before\_destroy  
‘destroy\_dependents\_with\_callbacks{#{reflection.class\_name}.delete(#{reflection.name}.id)  
unless #{reflection.name}.nil?}’”  
when :nullify  
module\_eval “before\_destroy  
‘destroy\_dependents\_with\_callbacks{#{reflection.name}.update\_attribute("#{reflection.primary\_key\_name}",  
nil) unless #{reflection.name}.nil?}’”  
when nil, false  
# pass  
else  
raise ArgumentError, “The :dependent option expects either :destroy,  
:delete or :nullify.”  
end  
end

## MySQL Connector

def change\_column\_default(table\_name, column\_name, default) #:nodoc:  
current\_type = select\_one(“SHOW COLUMNS FROM #{table\_name} LIKE  
‘#{column\_name}’”)[“Type”]  
default = (default == :null)? ‘NULL’ : quote(default);  
execute(“ALTER TABLE #{table\_name} CHANGE #{column\_name}  
#{column\_name} #{current\_type} DEFAULT #{default}”)  
end

---

<div class="post-metadata">

**Author:** ![keynan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/8dc957/32.png) [@keynan](https://www.ruby-forum.com/u/keynan)\
**Post date:** [May 17, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/changes-to-core-methods/104354/2 "2007-05-17T14:50:18Z")

</div>

And the point of this post is…?

Rails already has remote\_function, and I have no idea what you’re trying  
to  
do with this “configure\_dependency\_for” stuff.

Jason
