如何解决如何重写activerecord-import gem?
我试图覆盖activerecord-import gem,但我不知道该怎么做。
https://github.com/zdennis/activerecord-import
我要覆盖的方法是add_special_rails_stamps
。
但是,此方法位于class ActiveRecord::Base
中。
https://github.com/zdennis/activerecord-import/blob/master/lib/activerecord-import/import.rb#L995
如何覆盖此方法?
config / initializers / mockey_patches.rb
Dir[Rails.root.join('lib/monkey_patches/**/*.rb')].sort.each do |file|
require file
end
lib / monkey_patches / activerecord-import.rb
class ActiveRecord::Base
def class << self
def add_special_rails_stamps( column_names,array_of_attributes,options )
Rails.logger.debug("my debugger")
timestamp_columns = {}
timestamps = {}
if respond_to?(:all_timestamp_attributes_in_model,true) # Rails 5.1 and higher
timestamp_columns[:create] = timestamp_attributes_for_create_in_model
timestamp_columns[:update] = timestamp_attributes_for_update_in_model
else
instance = allocate
timestamp_columns[:create] = instance.send(:timestamp_attributes_for_create_in_model)
timestamp_columns[:update] = instance.send(:timestamp_attributes_for_update_in_model)
end
# use tz as set in ActiveRecord::Base
timestamp = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
[:create,:update].each do |action|
timestamp_columns[action].each do |column|
column = column.to_s
timestamps[column] = timestamp
index = column_names.index(column) || column_names.index(column.to_sym)
if index
# replace every instance of the array of attributes with our value
array_of_attributes.each { |arr| arr[index] = timestamp if arr[index].nil? }
else
column_names << column
array_of_attributes.each { |arr| arr << timestamp }
end
if supports_on_duplicate_key_update? && action == :update
connection.add_column_for_on_duplicate_key_update(column,options)
end
end
end
timestamps
end
end
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。