Latest approach (ar-literal)
http://s3.amazonaws.com/drawohara.com.data/ar-literals.tgz
# passing pure sql through to active-record is simply not supported,
but
# soometimes it's needed badly such as when you need to initialize a
value
# with a database function or provide a default as defined by the db.
this
# small hack, saved in RAILS_ROOT/lib/ar-sql.rb will provide a simple
# mechanism to pass sql value through unaltered to active-record
#
# the basic concept is simple, define a class that will be:
#
# . be ignored by type-casting
# . passed through verbetim by any quoting mechanism
# . easy to mark object with
#
# here's the code
#
# let rails do it's thing to load this modules/classes
#
ActiveRecord
ActiveRecord::Base
ActiveRecord::ConnectionAdapters
ActiveRecord::ConnectionAdapters::Column
# the following two sections are actuall all the needed code
#
module ActiveRecord
class Literal < ::String
def quoted_id
self
end
def inspect
"#{ self.class.name }(#{ super })"
end
end
def ActiveRecord.Literal(*args, &block)
Literal.new(*args, &block)
end
end
module ActiveRecord
class Base
def Base.Literal(*args, &block)
ActiveRecord.Literal(*args, &block)
end
def Literal(*args, &block)
Base.Literal(*args, &block)
end
end
end
# this code wouldn't actually be used if the above were incorporated
into core
#
module ActiveRecord
module ConnectionAdapters
class Column
@@type_cast = instance_method :type_cast
def type_cast value
Literal === value ? value : @@type_cast.bind(self).call
(value)
end
end
end
end
# and now we can do this like this
#
=begin
class Model < ActiveRecord::Base
def before_save
self['foo'] ||= Literal("current_timestamp")
end
end
=end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---