Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial support for ActiveRecord 4. Dropping support for older versions.

  • Loading branch information...
commit 9369d15b77da24c6fd06928d90cbe755a1f6982b 1 parent c06c90b
@ebeigarts ebeigarts authored
View
4 Gemfile
@@ -1,7 +1,7 @@
source 'http://rubygems.org'
group :development do
- gem 'jeweler', '~> 1.5.1'
+ # gem 'jeweler', '~> 1.5.1'
gem 'rspec', '~> 2.4'
gem 'rdoc'
@@ -41,6 +41,8 @@ group :development do
else
gem "journey", :git => "git://github.com/rails/journey"
end
+
+ gem "active_record_deprecated_finders", :git => "git://github.com/rails/active_record_deprecated_finders"
end
gem 'ruby-plsql', '>=0.4.4'
View
41 lib/active_record/connection_adapters/oracle_enhanced_activerecord_patches.rb
@@ -1,41 +0,0 @@
-# ActiveRecord 2.3 patches
-if ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 3
- require "active_record/associations"
-
- ActiveRecord::Associations::ClassMethods.module_eval do
- private
- def tables_in_string(string)
- return [] if string.blank?
- if self.connection.adapter_name == "OracleEnhanced"
- # always convert table names to downcase as in Oracle quoted table names are in uppercase
- # ignore raw_sql_ that is used by Oracle adapter as alias for limit/offset subqueries
- string.scan(/([a-zA-Z_][\.\w]+).?\./).flatten.map(&:downcase).uniq - ['raw_sql_']
- else
- string.scan(/([\.a-zA-Z_]+).?\./).flatten
- end
- end
- end
-
- ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.class_eval do
- protected
- def aliased_table_name_for(name, suffix = nil)
- # always downcase quoted table name as Oracle quoted table names are in uppercase
- if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{active_record.connection.quote_table_name(name).downcase}\son}
- @join_dependency.table_aliases[name] += 1
- end
-
- unless @join_dependency.table_aliases[name].zero?
- # if the table name has been used, then use an alias
- name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}"
- table_index = @join_dependency.table_aliases[name]
- @join_dependency.table_aliases[name] += 1
- name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
- else
- @join_dependency.table_aliases[name] += 1
- end
-
- name
- end
- end
-
-end
View
62 lib/active_record/connection_adapters/oracle_enhanced_adapter.rb
@@ -29,18 +29,30 @@
# contribution.
# portions Copyright 2005 Graham Jenkins
-# ActiveRecord 2.2 does not load version file automatically
-require 'active_record/version' unless defined?(ActiveRecord::VERSION)
-
require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/oracle_enhanced_connection'
-require 'active_record/connection_adapters/oracle_enhanced_base_ext'
require 'active_record/connection_adapters/oracle_enhanced_column'
require 'digest/sha1'
module ActiveRecord
+ module ConnectionHandling #:nodoc:
+ # Establishes a connection to the database that's used by all Active Record objects.
+ def oracle_enhanced_connection(config) #:nodoc:
+ if config[:emulate_oracle_adapter] == true
+ # allows the enhanced adapter to look like the OracleAdapter. Useful to pick up
+ # conditionals in the rails activerecord test suite
+ require 'active_record/connection_adapters/emulation/oracle_adapter'
+ ConnectionAdapters::OracleAdapter.new(
+ ConnectionAdapters::OracleEnhancedConnection.create(config), logger, config)
+ else
+ ConnectionAdapters::OracleEnhancedAdapter.new(
+ ConnectionAdapters::OracleEnhancedConnection.create(config), logger, config)
+ end
+ end
+ end
+
module ConnectionAdapters #:nodoc:
# Oracle enhanced adapter will work with both
@@ -852,9 +864,9 @@ def insert_fixture(fixture, table_name) #:nodoc:
super
if ActiveRecord::Base.pluralize_table_names
- klass = table_name.singularize.camelize
+ klass = table_name.to_s.singularize.camelize
else
- klass = table_name.camelize
+ klass = table_name.to_s.camelize
end
klass = klass.constantize rescue nil
@@ -1337,18 +1349,7 @@ def log_dbms_output
end
end
-# Added LOB writing callback for sessions stored in database
-# Otherwise it is not working as Session class is defined before OracleAdapter is loaded in Rails 2.0
-if defined?(CGI::Session::ActiveRecordStore::Session)
- if !CGI::Session::ActiveRecordStore::Session.respond_to?(:after_save_callback_chain) ||
- CGI::Session::ActiveRecordStore::Session.after_save_callback_chain.detect{|cb| cb.method == :enhanced_write_lobs}.nil?
- #:stopdoc:
- class CGI::Session::ActiveRecordStore::Session
- after_save :enhanced_write_lobs
- end
- #:startdoc:
- end
-end
+require 'active_record/connection_adapters/oracle_enhanced_model'
# Implementation of standard schema definition statements and extensions for schema definition
require 'active_record/connection_adapters/oracle_enhanced_schema_statements'
@@ -1360,21 +1361,12 @@ class CGI::Session::ActiveRecordStore::Session
# Extensions for context index definition
require 'active_record/connection_adapters/oracle_enhanced_context_index'
-# Load custom create, update, delete methods functionality
-require 'active_record/connection_adapters/oracle_enhanced_procedures'
-
# Load additional methods for composite_primary_keys support
require 'active_record/connection_adapters/oracle_enhanced_cpk'
# Load patch for dirty tracking methods
require 'active_record/connection_adapters/oracle_enhanced_dirty'
-# Load rake tasks definitions
-begin
- require 'active_record/connection_adapters/oracle_enhanced_tasks'
-rescue LoadError
-end if defined?(Rails) || defined?(RAILS_ROOT)
-
# Patches and enhancements for schema dumper
require 'active_record/connection_adapters/oracle_enhanced_schema_dumper'
@@ -1384,6 +1376,18 @@ class CGI::Session::ActiveRecordStore::Session
# Add BigDecimal#to_d, Fixnum#to_d and Bignum#to_d methods if not already present
require 'active_record/connection_adapters/oracle_enhanced_core_ext'
-require 'active_record/connection_adapters/oracle_enhanced_activerecord_patches'
-
require 'active_record/connection_adapters/oracle_enhanced_version'
+
+module ActiveRecord
+ autoload :OracleEnhancedProcedures, 'active_record/connection_adapters/oracle_enhanced_procedures'
+
+ module Model
+ include OracleEnhancedModel
+ end
+
+ class Base
+ # Include ActiveRecord::OracleEnhancedModel also in ActiveRecord::Base, since we are too late -
+ # the ActiveRecord::Model has already been loaded into ActiveRecord::Base.
+ include OracleEnhancedModel
+ end
+end
View
106 lib/active_record/connection_adapters/oracle_enhanced_base_ext.rb
@@ -1,106 +0,0 @@
-module ActiveRecord
- class Base
- # Establishes a connection to the database that's used by all Active Record objects.
- def self.oracle_enhanced_connection(config) #:nodoc:
- if config[:emulate_oracle_adapter] == true
- # allows the enhanced adapter to look like the OracleAdapter. Useful to pick up
- # conditionals in the rails activerecord test suite
- require 'active_record/connection_adapters/emulation/oracle_adapter'
- ConnectionAdapters::OracleAdapter.new(
- ConnectionAdapters::OracleEnhancedConnection.create(config), logger, config)
- else
- ConnectionAdapters::OracleEnhancedAdapter.new(
- ConnectionAdapters::OracleEnhancedConnection.create(config), logger, config)
- end
- end
-
- # Specify table columns which should be ignored by ActiveRecord, e.g.:
- #
- # ignore_table_columns :attribute1, :attribute2
- def self.ignore_table_columns(*args)
- connection.ignore_table_columns(table_name,*args)
- end
-
- # Specify which table columns should be typecasted to Date (without time), e.g.:
- #
- # set_date_columns :created_on, :updated_on
- def self.set_date_columns(*args)
- connection.set_type_for_columns(table_name,:date,*args)
- end
-
- # Specify which table columns should be typecasted to Time (or DateTime), e.g.:
- #
- # set_datetime_columns :created_date, :updated_date
- def self.set_datetime_columns(*args)
- connection.set_type_for_columns(table_name,:datetime,*args)
- end
-
- # Specify which table columns should be typecasted to boolean values +true+ or +false+, e.g.:
- #
- # set_boolean_columns :is_valid, :is_completed
- def self.set_boolean_columns(*args)
- connection.set_type_for_columns(table_name,:boolean,*args)
- end
-
- # Specify which table columns should be typecasted to integer values.
- # Might be useful to force NUMBER(1) column to be integer and not boolean, or force NUMBER column without
- # scale to be retrieved as integer and not decimal. Example:
- #
- # set_integer_columns :version_number, :object_identifier
- def self.set_integer_columns(*args)
- connection.set_type_for_columns(table_name,:integer,*args)
- end
-
- # Specify which table columns should be typecasted to string values.
- # Might be useful to specify that columns should be string even if its name matches boolean column criteria.
- #
- # set_string_columns :active_flag
- def self.set_string_columns(*args)
- connection.set_type_for_columns(table_name,:string,*args)
- end
-
- # After setting large objects to empty, select the OCI8::LOB
- # and write back the data.
- if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR >= 1
- after_update :enhanced_write_lobs
- else
- after_save :enhanced_write_lobs
- end
- def enhanced_write_lobs #:nodoc:
- if connection.is_a?(ConnectionAdapters::OracleEnhancedAdapter) &&
- !(self.class.custom_create_method || self.class.custom_update_method)
- connection.write_lobs(self.class.table_name, self.class, attributes)
- end
- end
- private :enhanced_write_lobs
-
- # Get table comment from schema definition.
- def self.table_comment
- connection.table_comment(self.table_name)
- end
-
- def self.virtual_columns
- columns.select do |column|
- column.respond_to?(:virtual?) && column.virtual?
- end
- end
-
- if ActiveRecord::VERSION::MAJOR < 3
- def attributes_with_quotes_with_virtual_columns(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys)
- virtual_column_names = self.class.virtual_columns.map(&:name)
- attributes_with_quotes_without_virtual_columns(include_primary_key, include_readonly_attributes, attribute_names - virtual_column_names)
- end
-
- alias_method_chain :attributes_with_quotes, :virtual_columns
- else
- def arel_attributes_values_with_virtual_columns(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys)
- virtual_column_names = self.class.virtual_columns.map(&:name)
- arel_attributes_values_without_virtual_columns(include_primary_key, include_readonly_attributes, attribute_names - virtual_column_names)
- end
-
- alias_method_chain :arel_attributes_values, :virtual_columns
- end
-
- end
-
-end
View
17 lib/active_record/connection_adapters/oracle_enhanced_context_index.rb
@@ -298,19 +298,10 @@ def has_context_index
module ContextIndexClassMethods
# Add context index condition.
- case ::ActiveRecord::VERSION::MAJOR
- when 3
- def contains(column, query, options ={})
- score_label = options[:label].to_i || 1
- where("CONTAINS(#{connection.quote_column_name(column)}, ?, #{score_label}) > 0", query).
- order("SCORE(#{score_label}) DESC")
- end
- when 2
- def contains(column, query, options ={})
- score_label = options[:label].to_i || 1
- scoped(:conditions => ["CONTAINS(#{connection.quote_column_name(column)}, ?, #{score_label}) > 0", query],
- :order => "SCORE(#{score_label}) DESC")
- end
+ def contains(column, query, options ={})
+ score_label = options[:label].to_i || 1
+ where("CONTAINS(#{connection.quote_column_name(column)}, ?, #{score_label}) > 0", query).
+ order("SCORE(#{score_label}) DESC")
end
end
View
97 lib/active_record/connection_adapters/oracle_enhanced_model.rb
@@ -0,0 +1,97 @@
+module ActiveRecord
+ module OracleEnhancedModel
+ module ClassMethods
+ # TODO REMOVE THIS AND PATCH #5267 commit 2fdb521
+ def sequence_name
+ if base_class == self
+ reset_sequence_name
+ @sequence_name
+ else
+ (@sequence_name ||= nil) || base_class.sequence_name
+ end
+ end
+
+ # Specify table columns which should be ignored by ActiveRecord, e.g.:
+ #
+ # ignore_table_columns :attribute1, :attribute2
+ def ignore_table_columns(*args)
+ connection.ignore_table_columns(table_name,*args)
+ end
+
+ # Specify which table columns should be typecasted to Date (without time), e.g.:
+ #
+ # set_date_columns :created_on, :updated_on
+ def set_date_columns(*args)
+ connection.set_type_for_columns(table_name,:date,*args)
+ end
+
+ # Specify which table columns should be typecasted to Time (or DateTime), e.g.:
+ #
+ # set_datetime_columns :created_date, :updated_date
+ def set_datetime_columns(*args)
+ connection.set_type_for_columns(table_name,:datetime,*args)
+ end
+
+ # Specify which table columns should be typecasted to boolean values +true+ or +false+, e.g.:
+ #
+ # set_boolean_columns :is_valid, :is_completed
+ def set_boolean_columns(*args)
+ connection.set_type_for_columns(table_name,:boolean,*args)
+ end
+
+ # Specify which table columns should be typecasted to integer values.
+ # Might be useful to force NUMBER(1) column to be integer and not boolean, or force NUMBER column without
+ # scale to be retrieved as integer and not decimal. Example:
+ #
+ # set_integer_columns :version_number, :object_identifier
+ def set_integer_columns(*args)
+ connection.set_type_for_columns(table_name,:integer,*args)
+ end
+
+ # Specify which table columns should be typecasted to string values.
+ # Might be useful to specify that columns should be string even if its name matches boolean column criteria.
+ #
+ # set_string_columns :active_flag
+ def set_string_columns(*args)
+ connection.set_type_for_columns(table_name,:string,*args)
+ end
+
+ # Get table comment from schema definition.
+ def table_comment
+ connection.table_comment(self.table_name)
+ end
+
+ def virtual_columns
+ columns.select do |column|
+ column.respond_to?(:virtual?) && column.virtual?
+ end
+ end
+ end
+
+ def self.included(base)
+ base.class_eval do
+ extend ClassMethods
+ end
+ end
+
+ def arel_attributes_with_values(attribute_names)
+ virtual_column_names = self.class.virtual_columns.map(&:name)
+ super(attribute_names - virtual_column_names)
+ end
+
+ private
+
+ # After setting large objects to empty, select the OCI8::LOB
+ # and write back the data.
+ def update
+ super
+ if connection.is_a?(ConnectionAdapters::OracleEnhancedAdapter) &&
+ !(
+ (self.class.respond_to?(:custom_create_method) && self.class.custom_create_method) ||
+ (self.class.respond_to?(:custom_update_method) && self.class.custom_update_method)
+ )
+ connection.write_lobs(self.class.table_name, self.class, attributes)
+ end
+ end
+ end
+end
View
395 lib/active_record/connection_adapters/oracle_enhanced_procedures.rb
@@ -1,260 +1,191 @@
-# define accessors before requiring ruby-plsql as these accessors are used in clob writing callback and should be
-# available also if ruby-plsql could not be loaded
-ActiveRecord::Base.class_eval do
- if respond_to? :class_attribute
- class_attribute :custom_create_method, :custom_update_method, :custom_delete_method
- elsif respond_to? :class_inheritable_accessor
- class_inheritable_accessor :custom_create_method, :custom_update_method, :custom_delete_method
- end
-end
-
require 'active_support'
module ActiveRecord #:nodoc:
- module ConnectionAdapters #:nodoc:
- module OracleEnhancedProcedures #:nodoc:
-
- module ClassMethods
- # Specify custom create method which should be used instead of Rails generated INSERT statement.
- # Provided block should return ID of new record.
- # Example:
- # set_create_method do
- # plsql.employees_pkg.create_employee(
- # :p_first_name => first_name,
- # :p_last_name => last_name,
- # :p_employee_id => nil
- # )[:p_employee_id]
- # end
- def set_create_method(&block)
- include_with_custom_methods
- self.custom_create_method = block
- end
-
- # Specify custom update method which should be used instead of Rails generated UPDATE statement.
- # Example:
- # set_update_method do
- # plsql.employees_pkg.update_employee(
- # :p_employee_id => id,
- # :p_first_name => first_name,
- # :p_last_name => last_name
- # )
- # end
- def set_update_method(&block)
- include_with_custom_methods
- self.custom_update_method = block
- end
-
- # Specify custom delete method which should be used instead of Rails generated DELETE statement.
- # Example:
- # set_delete_method do
- # plsql.employees_pkg.delete_employee(
- # :p_employee_id => id
- # )
- # end
- def set_delete_method(&block)
- include_with_custom_methods
- self.custom_delete_method = block
- end
-
- if ActiveRecord::VERSION::MAJOR < 3
- def create_method_name_before_custom_methods #:nodoc:
- if private_method_defined?(:create_without_timestamps) && defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::STRING.to_f >= 2.3
- :create_without_timestamps
- elsif private_method_defined?(:create_without_callbacks)
- :create_without_callbacks
- else
- :create
- end
- end
-
- def update_method_name_before_custom_methods #:nodoc:
- if private_method_defined?(:update_without_dirty)
- :update_without_dirty
- elsif private_method_defined?(:update_without_timestamps) && defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::STRING.to_f >= 2.3
- :update_without_timestamps
- elsif private_method_defined?(:update_without_callbacks)
- :update_without_callbacks
- else
- :update
- end
- end
+ # Custom create, update, delete methods functionality.
+ #
+ # Example:
+ #
+ # class Employee < ActiveRecord::Base
+ # include ActiveRecord::OracleEnhancedProcedures
+ #
+ # set_create_method do
+ # plsql.employees_pkg.create_employee(
+ # :p_first_name => first_name,
+ # :p_last_name => last_name,
+ # :p_employee_id => nil
+ # )[:p_employee_id]
+ # end
+ #
+ # set_update_method do
+ # plsql.employees_pkg.update_employee(
+ # :p_employee_id => id,
+ # :p_first_name => first_name,
+ # :p_last_name => last_name
+ # )
+ # end
+ #
+ # set_delete_method do
+ # plsql.employees_pkg.delete_employee(
+ # :p_employee_id => id
+ # )
+ # end
+ # end
+ #
+ module OracleEnhancedProcedures #:nodoc:
+ module ClassMethods
+ # Specify custom create method which should be used instead of Rails generated INSERT statement.
+ # Provided block should return ID of new record.
+ # Example:
+ # set_create_method do
+ # plsql.employees_pkg.create_employee(
+ # :p_first_name => first_name,
+ # :p_last_name => last_name,
+ # :p_employee_id => nil
+ # )[:p_employee_id]
+ # end
+ def set_create_method(&block)
+ self.custom_create_method = block
+ end
- def destroy_method_name_before_custom_methods #:nodoc:
- if public_method_defined?(:destroy_without_callbacks)
- :destroy_without_callbacks
- else
- :destroy
- end
- end
- end
+ # Specify custom update method which should be used instead of Rails generated UPDATE statement.
+ # Example:
+ # set_update_method do
+ # plsql.employees_pkg.update_employee(
+ # :p_employee_id => id,
+ # :p_first_name => first_name,
+ # :p_last_name => last_name
+ # )
+ # end
+ def set_update_method(&block)
+ self.custom_update_method = block
+ end
- private
+ # Specify custom delete method which should be used instead of Rails generated DELETE statement.
+ # Example:
+ # set_delete_method do
+ # plsql.employees_pkg.delete_employee(
+ # :p_employee_id => id
+ # )
+ # end
+ def set_delete_method(&block)
+ self.custom_delete_method = block
+ end
+ end
- def include_with_custom_methods
- unless included_modules.include? InstanceMethods
- include InstanceMethods
- end
- end
+ def self.included(base)
+ base.class_eval do
+ extend ClassMethods
+ class_attribute :custom_create_method
+ class_attribute :custom_update_method
+ class_attribute :custom_delete_method
end
+ end
- module InstanceMethods #:nodoc:
- def self.included(base)
- # alias methods just for ActiveRecord 2.x
- # for ActiveRecord 3.0 will just redefine create, update, delete methods which call super
- if ActiveRecord::VERSION::MAJOR < 3
- base.instance_eval do
- alias_method :create_without_custom_method, create_method_name_before_custom_methods
- alias_method create_method_name_before_custom_methods, :create_with_custom_method
- alias_method :update_without_custom_method, update_method_name_before_custom_methods
- alias_method update_method_name_before_custom_methods, :update_with_custom_method
- alias_method :destroy_without_custom_method, destroy_method_name_before_custom_methods
- alias_method destroy_method_name_before_custom_methods, :destroy_with_custom_method
- private :create, :update
- public :destroy
- end
- end
+ def destroy #:nodoc:
+ # check if class has custom delete method
+ if self.class.custom_delete_method
+ # wrap destroy in transaction
+ with_transaction_returning_status do
+ # run before/after callbacks defined in model
+ run_callbacks(:destroy) { destroy_using_custom_method }
end
+ else
+ super
+ end
+ end
- if ActiveRecord::VERSION::MAJOR >= 3
- def destroy #:nodoc:
- # check if class has custom delete method
- if self.class.custom_delete_method
- # wrap destroy in transaction
- with_transaction_returning_status do
- # run before/after callbacks defined in model
- _run_destroy_callbacks { destroy_using_custom_method }
+ private
+
+ # Creates a record with custom create method
+ # and returns its id.
+ def create
+ # check if class has custom create method
+ if self.class.custom_create_method
+ # run before/after callbacks defined in model
+ run_callbacks(:create) do
+ # timestamp
+ if self.record_timestamps
+ current_time = current_time_from_proper_timezone
+
+ all_timestamp_attributes.each do |column|
+ if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil?
+ write_attribute(column.to_s, current_time)
end
- else
- super
- end
- end
- end
-
- private
-
- # Creates a record with custom create method
- # and returns its id.
- if ActiveRecord::VERSION::MAJOR < 3
- def create_with_custom_method
- # check if class has custom create method
- self.class.custom_create_method ? create_using_custom_method : create_without_custom_method
- end
- else # ActiveRecord 3.x
- def create
- # check if class has custom create method
- if self.class.custom_create_method
- set_timestamps_before_custom_create_method
- # run before/after callbacks defined in model
- _run_create_callbacks { create_using_custom_method }
- else
- super
end
end
+ # run
+ create_using_custom_method
end
+ else
+ super
+ end
+ end
- def create_using_custom_method
- self.class.connection.log_custom_method("custom create method", "#{self.class.name} Create") do
- self.id = instance_eval(&self.class.custom_create_method)
- end
- @new_record = false
- # Starting from ActiveRecord 3.0.3 @persisted is used instead of @new_record
- @persisted = true
- id
- end
+ def create_using_custom_method
+ log_custom_method("custom create method", "#{self.class.name} Create") do
+ self.id = instance_eval(&self.class.custom_create_method)
+ end
+ @new_record = false
+ # Starting from ActiveRecord 3.0.3 @persisted is used instead of @new_record
+ @persisted = true
+ id
+ end
- # Updates the associated record with custom update method
- # Returns the number of affected rows.
- if ActiveRecord::VERSION::MAJOR < 3
- def update_with_custom_method(attribute_names = @attributes.keys)
- # check if class has custom create method
- self.class.custom_update_method ? update_using_custom_method(attribute_names) : update_without_custom_method(attribute_names)
- end
- else # ActiveRecord 3.x
- def update(attribute_names = @attributes.keys)
- # check if class has custom update method
- if self.class.custom_update_method
- set_timestamps_before_custom_update_method
- # run before/after callbacks defined in model
- _run_update_callbacks do
- # update just dirty attributes
- if partial_updates?
- # Serialized attributes should always be written in case they've been
- # changed in place.
- update_using_custom_method(changed | (attributes.keys & self.class.serialized_attributes.keys))
- else
- update_using_custom_method(attribute_names)
- end
- end
- else
- super
+ # Updates the associated record with custom update method
+ # Returns the number of affected rows.
+ def update(attribute_names = @attributes.keys)
+ # check if class has custom update method
+ if self.class.custom_update_method
+ # run before/after callbacks defined in model
+ run_callbacks(:update) do
+ # timestamp
+ if should_record_timestamps?
+ current_time = current_time_from_proper_timezone
+
+ timestamp_attributes_for_update_in_model.each do |column|
+ column = column.to_s
+ next if attribute_changed?(column)
+ write_attribute(column, current_time)
end
end
- end
-
- def update_using_custom_method(attribute_names)
- return 0 if attribute_names.empty?
- self.class.connection.log_custom_method("custom update method with #{self.class.primary_key}=#{self.id}", "#{self.class.name} Update") do
- instance_eval(&self.class.custom_update_method)
- end
- 1
- end
-
- # Deletes the record in the database with custom delete method
- # and freezes this instance to reflect that no changes should
- # be made (since they can't be persisted).
- if ActiveRecord::VERSION::MAJOR < 3
- def destroy_with_custom_method
- # check if class has custom delete method
- self.class.custom_delete_method ? destroy_using_custom_method : destroy_without_custom_method
+ # update just dirty attributes
+ if partial_updates?
+ # Serialized attributes should always be written in case they've been
+ # changed in place.
+ update_using_custom_method(changed | (attributes.keys & self.class.serialized_attributes.keys))
+ else
+ update_using_custom_method(attribute_names)
end
end
+ else
+ super
+ end
+ end
- def destroy_using_custom_method
- unless new_record? || @destroyed
- self.class.connection.log_custom_method("custom delete method with #{self.class.primary_key}=#{self.id}", "#{self.class.name} Destroy") do
- instance_eval(&self.class.custom_delete_method)
- end
- end
-
- @destroyed = true
- freeze
- end
-
- if ActiveRecord::VERSION::MAJOR >= 3
- def set_timestamps_before_custom_create_method
- if record_timestamps
- current_time = current_time_from_proper_timezone
-
- write_attribute('created_at', current_time) if respond_to?(:created_at) && created_at.nil?
- write_attribute('created_on', current_time) if respond_to?(:created_on) && created_on.nil?
-
- write_attribute('updated_at', current_time) if respond_to?(:updated_at) && updated_at.nil?
- write_attribute('updated_on', current_time) if respond_to?(:updated_on) && updated_on.nil?
- end
- end
-
- def set_timestamps_before_custom_update_method
- if record_timestamps && (!partial_updates? || changed?)
- current_time = current_time_from_proper_timezone
+ def update_using_custom_method(attribute_names)
+ return 0 if attribute_names.empty?
+ log_custom_method("custom update method with #{self.class.primary_key}=#{self.id}", "#{self.class.name} Update") do
+ instance_eval(&self.class.custom_update_method)
+ end
+ 1
+ end
- write_attribute('updated_at', current_time) if respond_to?(:updated_at)
- write_attribute('updated_on', current_time) if respond_to?(:updated_on)
- end
- end
+ # Deletes the record in the database with custom delete method
+ # and freezes this instance to reflect that no changes should
+ # be made (since they can't be persisted).
+ def destroy_using_custom_method
+ unless new_record? || @destroyed
+ log_custom_method("custom delete method with #{self.class.primary_key}=#{self.id}", "#{self.class.name} Destroy") do
+ instance_eval(&self.class.custom_delete_method)
end
-
end
+ @destroyed = true
+ freeze
end
- end
-end
-ActiveRecord::Base.class_eval do
- extend ActiveRecord::ConnectionAdapters::OracleEnhancedProcedures::ClassMethods
-end
-
-ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.class_eval do
- # public alias to log method which could be used from other objects
- alias_method :log_custom_method, :log
- public :log_custom_method
+ def log_custom_method(*args)
+ self.class.connection.send(:log, *args) { yield }
+ end
+ end
end
View
17 lib/active_record/connection_adapters/oracle_enhanced_tasks.rb
@@ -1,17 +0,0 @@
-# Used just for Rails 2.x
-# In Rails 3.x rake tasks are loaded using railtie
-if ActiveRecord::VERSION::MAJOR == 2
-
- if defined?(Rake.application) && Rake.application &&
- ActiveRecord::Base.configurations[defined?(Rails.env) ? Rails.env : RAILS_ENV]['adapter'] == 'oracle_enhanced'
- oracle_enhanced_rakefile = File.dirname(__FILE__) + "/oracle_enhanced.rake"
- if Rake.application.lookup("environment")
- # rails tasks already defined; load the override tasks now
- load oracle_enhanced_rakefile
- else
- # rails tasks not loaded yet; load as an import
- Rake.application.add_import(oracle_enhanced_rakefile)
- end
- end
-
-end
View
2  spec/active_record/connection_adapters/oracle_enhanced_procedures_spec.rb
@@ -107,6 +107,8 @@
before(:each) do
class ::TestEmployee < ActiveRecord::Base
+ include ActiveRecord::OracleEnhancedProcedures
+
if self.respond_to?(:primary_key=)
self.primary_key = :employee_id
else
View
3  spec/active_record/connection_adapters/oracle_enhanced_schema_statements_spec.rb
@@ -757,7 +757,6 @@ class ::TestComment < ActiveRecord::Base
end
it "should add foreign key in change_table" do
- return pending("Not in this ActiveRecord version") unless ENV['RAILS_GEM_VERSION'] >= '2.1'
schema_define do
create_table :test_comments, :force => true do |t|
t.string :body, :limit => 4000
@@ -773,7 +772,6 @@ class ::TestComment < ActiveRecord::Base
end
it "should add foreign key in change_table references" do
- return pending("Not in this ActiveRecord version") unless ENV['RAILS_GEM_VERSION'] >= '2.1'
schema_define do
create_table :test_comments, :force => true do |t|
t.string :body, :limit => 4000
@@ -788,7 +786,6 @@ class ::TestComment < ActiveRecord::Base
end
it "should remove foreign key by table name" do
- return pending("Not in this ActiveRecord version") unless ENV['RAILS_GEM_VERSION'] >= '2.1'
schema_define do
create_table :test_comments, :force => true do |t|
t.string :body, :limit => 4000
View
74 spec/spec_helper.rb
@@ -13,35 +13,21 @@
puts "==> Running specs with JRuby version #{JRUBY_VERSION}"
end
-ENV['RAILS_GEM_VERSION'] ||= '3.2-master'
-NO_COMPOSITE_PRIMARY_KEYS = true if ENV['RAILS_GEM_VERSION'] >= '2.3.5' || ENV['RAILS_GEM_VERSION'] =~ /^2\.3\.1\d$/
+ENV['RAILS_GEM_VERSION'] ||= '4.0-master'
+NO_COMPOSITE_PRIMARY_KEYS = true
puts "==> Running specs with Rails version #{ENV['RAILS_GEM_VERSION']}"
require 'active_record'
-if ENV['RAILS_GEM_VERSION'] >= '3.0'
- require 'action_dispatch'
- require 'active_support/core_ext/module/attribute_accessors'
- require 'active_support/core_ext/class/attribute_accessors'
-
- if ENV['RAILS_GEM_VERSION'] =~ /^3.0.0.beta/
- require "rails/log_subscriber"
- require 'active_record/railties/log_subscriber'
- else
- require "active_support/log_subscriber"
- require 'active_record/log_subscriber'
- end
+require 'action_dispatch'
+require 'active_support/core_ext/module/attribute_accessors'
+require 'active_support/core_ext/class/attribute_accessors'
- require 'logger'
-elsif ENV['RAILS_GEM_VERSION'] =~ /^2.3/
- require 'action_pack'
- require 'action_controller/session/abstract_store'
- require 'active_record/session_store'
-elsif ENV['RAILS_GEM_VERSION'] <= '2.3'
- require 'action_pack'
- require 'action_controller/session/active_record_store'
-end
+require "active_support/log_subscriber"
+require 'active_record/log_subscriber'
+
+require 'logger'
require 'active_record/connection_adapters/oracle_enhanced_adapter'
require 'ruby-plsql'
@@ -51,30 +37,16 @@ def set_logger
@logger = MockLogger.new
@old_logger = ActiveRecord::Base.logger
- if ENV['RAILS_GEM_VERSION'] >= '3.0'
- @notifier = ActiveSupport::Notifications::Fanout.new
+ @notifier = ActiveSupport::Notifications::Fanout.new
- ActiveSupport::LogSubscriber.colorize_logging = false
+ ActiveSupport::LogSubscriber.colorize_logging = false
- ActiveRecord::Base.logger = @logger
- @old_notifier = ActiveSupport::Notifications.notifier
- ActiveSupport::Notifications.notifier = @notifier
-
- ActiveRecord::LogSubscriber.attach_to(:active_record)
- if ENV['RAILS_GEM_VERSION'] >= '3.2'
- ActiveSupport::Notifications.subscribe("sql.active_record", ActiveRecord::ExplainSubscriber.new)
- end
- else # ActiveRecord 2.x
- if ActiveRecord::Base.respond_to?(:connection_pool)
- ActiveRecord::Base.connection_pool.clear_reloadable_connections!
- else
- ActiveRecord::Base.clear_active_connections!
- end
- ActiveRecord::Base.logger = @logger
- ActiveRecord::Base.colorize_logging = false
- # ActiveRecord::Base.logger.level = Logger::DEBUG
- end
+ ActiveRecord::Base.logger = @logger
+ @old_notifier = ActiveSupport::Notifications.notifier
+ ActiveSupport::Notifications.notifier = @notifier
+ ActiveRecord::LogSubscriber.attach_to(:active_record)
+ ActiveSupport::Notifications.subscribe("sql.active_record", ActiveRecord::ExplainSubscriber.new)
end
class MockLogger
@@ -115,11 +87,8 @@ def clear_logger
ActiveRecord::Base.logger = @old_logger
@logger = nil
- if ENV['RAILS_GEM_VERSION'] >= '3.0'
- ActiveSupport::Notifications.notifier = @old_notifier
- @notifier = nil
- end
-
+ ActiveSupport::Notifications.notifier = @old_notifier
+ @notifier = nil
end
# Wait notifications to be published (for Rails 3.0)
@@ -177,11 +146,8 @@ def schema_define(&block)
DATABASE_NON_DEFAULT_TABLESPACE = ENV['DATABASE_NON_DEFAULT_TABLESPACE'] || "SYSTEM"
-# Set default $KCODE to UTF8
-if RUBY_VERSION < "1.9"
- $KCODE = "UTF8"
-end
-
# set default time zone in TZ environment variable
# which will be used to set session time zone
ENV['TZ'] ||= 'Europe/Riga'
+
+# ActiveRecord::Base.logger = Logger.new(STDOUT)
Please sign in to comment.
Something went wrong with that request. Please try again.