Forum: Ruby on Rails Changes to core methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Keynan P. (Guest)
on 2007-05-17 03:47
##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 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 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
Jason R. (Guest)
on 2007-05-17 16:50
(Received via mailing list)
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
This topic is locked and can not be replied to.