Guest User

Untitled

a guest
Mar 1st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. require 'rubygems'
  2. require 'activerecord'
  3.  
  4. class ActiveRecord::Base
  5.  
  6. alias_method '__initialize__', 'initialize'
  7.  
  8. def initialize options = nil, &block
  9. returning( __initialize__(options, &block) ) do
  10. options ||= {}
  11. options.to_options!
  12. defaults = self.class.defaults || self.defaults || Hash.new
  13. (defaults.keys - options.keys).each do |key|
  14. value = defaults[key]
  15. case value
  16. when Proc
  17. value = instance_eval &value
  18. when Symbol
  19. value = send value
  20. end
  21. send "#{ key }=", value
  22. end
  23. end
  24. end
  25.  
  26. def self.defaults *argv
  27. @defaults = argv.shift.to_hash if argv.first
  28. return @defaults if defined? @defaults
  29. end
  30.  
  31. def defaults *argv
  32. @defaults = argv.shift.to_hash if argv.first
  33. return @defaults if defined? @defaults
  34. end
  35.  
  36. module UnQuoted
  37. def quoted_id()
  38. STDERR.puts "CALLED quoted_id"
  39. self
  40. end ### hackity hack hack
  41. end
  42.  
  43. unless defined? DEFAULT
  44. STDERR.puts "CREATING DEFAULT"
  45. DEFAULT = 'DEFAULT'
  46. DEFAULT.extend UnQuoted
  47. DEFAULT.freeze
  48. end
  49.  
  50. before_create :force_default_where_necessary
  51. # force the sql keyword DEFAULT into those columns where there must be a
  52. # default value, that is, columns with a default that are not nullable, those
  53. # should have the DEFAULT keyword set⋅
  54. def force_default_where_necessary
  55. STDERR.puts "force default_where_necessary"
  56. @attributes.each_pair do |name, value|
  57. STDERR.puts "Checking on #{self.class.name}::#{name} with value #{value.inspect}"
  58. unless value
  59. column = column_for_attribute(name)
  60. STDERR.puts "checking column type -> #{column.type.inspect}, sql_type -> #{column.sql_type.inspect}, default -> #{column.default.inspect}"
  61. STDERR.puts "checking column has_default? -> #{column.has_default?.inspect}, null -> #{column.null.inspect}"
  62. if (name == 'created_time') or (column.has_default? and !column.null) then
  63. STDERR.puts "Setting #{self.class.name}##{name.inspect} to #{DEFAULT}"
  64. send "#{name}=", DEFAULT
  65. end
  66. end
  67. end
  68. end
  69. end
  70.  
  71.  
  72. class Datetest < ActiveRecord::Base
  73. defaults 'created_time' => DEFAULT
  74. end
  75.  
  76. DbConfig = { :username => 'postgres', :password => "", :database => 'mcore', :adapter => "postgresql" }
  77.  
  78. ActiveRecord::Base.establish_connection( DbConfig )
  79. ActiveRecord::Base.connection.execute(<<-table)
  80. CREATE TABLE DateTests(
  81. id serial not null primary key ,
  82. created_time timestamp not null default current_timestamp
  83. )
  84. table
  85.  
  86. dt = Datetest.create!
  87.  
  88. ActiveRecord::Base.connection.execute("DROP TABLE DateTests")
Add Comment
Please, Sign In to add comment