From d6f7b7d35337b87cc1c419e47fae52bbfc3f371e Mon Sep 17 00:00:00 2001 From: Tim Connor Date: Mon, 27 Sep 2010 11:56:18 +1300 Subject: Fix remove_index issue when provided :name is a symbol Signed-off-by: Santiago Pastorino --- .../lib/active_record/connection_adapters/abstract/schema_statements.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 310423bb20..6c29e67e17 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -404,7 +404,7 @@ module ActiveRecord # as there's no way to determine the correct answer in that case. def index_name_exists?(table_name, index_name, default) return default unless respond_to?(:indexes) - indexes(table_name).detect { |i| i.name == index_name } + indexes(table_name).detect { |i| i.name == index_name.to_s } end # Returns a string of CREATE TABLE SQL statement(s) for recreating the -- cgit v1.2.3 From f8c7f4cc5370b98ba93faeab49dcf9c37656920e Mon Sep 17 00:00:00 2001 From: Nic Benders Date: Sun, 26 Sep 2010 11:25:10 -0700 Subject: db:structure:dump should list current Rails.env adapter in errors, not always the test adapter [#5710 state:committed] Signed-off-by: Santiago Pastorino --- activerecord/lib/active_record/railties/databases.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 12bfe3c738..389a5d5884 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -390,7 +390,7 @@ namespace :db do db_string = firebird_db_string(abcs[Rails.env]) sh "isql -a #{db_string} > #{Rails.root}/db/#{Rails.env}_structure.sql" else - raise "Task not supported by '#{abcs["test"]["adapter"]}'" + raise "Task not supported by '#{abcs[Rails.env]["adapter"]}'" end if ActiveRecord::Base.connection.supports_migrations? -- cgit v1.2.3 From 7918a5c96604b6c2d8a60542b7afc9e445c43fba Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sun, 26 Sep 2010 18:25:13 -0700 Subject: third parameter is not used --- .../active_record/connection_adapters/abstract/database_statements.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index 25432e9985..646a78622c 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -35,7 +35,7 @@ module ActiveRecord undef_method :select_rows # Executes the SQL statement in the context of this connection. - def execute(sql, name = nil, skip_logging = false) + def execute(sql, name = nil) end undef_method :execute -- cgit v1.2.3 From eff68d86adce12f8c850fcd810f18a81ef53c0eb Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 22 Sep 2010 15:03:59 -0300 Subject: Prevent shadowing outer local variable. --- activerecord/lib/active_record/association_preload.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 715c868598..e6b367790b 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -321,14 +321,14 @@ module ActiveRecord klasses_and_ids[reflection.klass.name] = id_map unless id_map.empty? end - klasses_and_ids.each do |klass_name, id_map| + klasses_and_ids.each do |klass_name, _id_map| klass = klass_name.constantize table_name = klass.quoted_table_name primary_key = reflection.options[:primary_key] || klass.primary_key column_type = klass.columns.detect{|c| c.name == primary_key}.type - ids = id_map.keys.map do |id| + ids = _id_map.keys.map do |id| if column_type == :integer id.to_i elsif column_type == :float @@ -343,7 +343,7 @@ module ActiveRecord associated_records = klass.unscoped.where([conditions, ids]).apply_finder_options(options.slice(:include, :select, :joins, :order)).to_a - set_association_single_records(id_map, reflection.name, associated_records, primary_key) + set_association_single_records(_id_map, reflection.name, associated_records, primary_key) end end -- cgit v1.2.3 From fbd1d306b95cc2efb6422e12d26d5818a3a42343 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Thu, 9 Sep 2010 16:49:44 -0400 Subject: Three performance improvements: * for simple cases like User.last and User.order('name desc').last no need to perform Array#join operation. * Instead of performing String#blank? do Array#empty? * no need to create variable relation --- .../lib/active_record/relation/query_methods.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 6a33edeb97..c97b1a24d2 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -135,14 +135,13 @@ module ActiveRecord end def reverse_order - order_clause = arel.order_clauses.join(', ') - relation = except(:order) + order_clause = arel.order_clauses - order = order_clause.blank? ? + order = order_clause.empty? ? "#{@klass.table_name}.#{@klass.primary_key} DESC" : - reverse_sql_order(order_clause) + reverse_sql_order(order_clause).join(', ') - relation.order(Arel::SqlLiteral.new(order)) + except(:order).order(Arel::SqlLiteral.new(order)) end def arel @@ -283,15 +282,15 @@ module ActiveRecord end def reverse_sql_order(order_query) - order_query.split(',').each { |s| + order_query.join(', ').split(',').collect { |s| if s.match(/\s(asc|ASC)$/) - s.gsub!(/\s(asc|ASC)$/, ' DESC') + s.gsub(/\s(asc|ASC)$/, ' DESC') elsif s.match(/\s(desc|DESC)$/) - s.gsub!(/\s(desc|DESC)$/, ' ASC') + s.gsub(/\s(desc|DESC)$/, ' ASC') else - s.concat(' DESC') + s + ' DESC' end - }.join(',') + } end def array_of_strings?(o) -- cgit v1.2.3 From 7f743233c4e276d1799451478e6718589d270cbd Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sun, 26 Sep 2010 00:35:39 -0400 Subject: Fix for nested_attributes with has_many association fails when a single record is being updated. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#5705 state:resolved] Signed-off-by: José Valim --- activerecord/lib/active_record/nested_attributes.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 7372ab3278..bdd940f3ee 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -377,7 +377,12 @@ module ActiveRecord end if attributes_collection.is_a? Hash - attributes_collection = attributes_collection.sort_by { |index, _| index.to_i }.map { |_, attributes| attributes } + keys = attributes_collection.keys + attributes_collection = if keys.include?('id') || keys.include?(:id) + Array.wrap(attributes_collection) + else + attributes_collection.sort_by { |i, _| i.to_i }.map { |_, attributes| attributes } + end end association = send(association_name) -- cgit v1.2.3 From f22b40a8f223e0b2f5194b6f3ce24cafd5cd3a05 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 14:29:00 -0700 Subject: make sure we use the engine assigned to the table when quoting --- activerecord/lib/active_record/relation/query_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index c97b1a24d2..b8cfdb54e5 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -182,7 +182,7 @@ module ActiveRecord when Arel::SqlLiteral arel = arel.where(where) else - sql = where.is_a?(String) ? where : where.to_sql + sql = where.is_a?(String) ? where : where.to_sql(table.engine) arel = arel.where(Arel::SqlLiteral.new("(#{sql})")) end end -- cgit v1.2.3 From 9e652b65c2c481f2b4ae0cda9df42d4bb56b9f3c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 16:06:55 -0700 Subject: reduce method calls in the dynamic finder matcher --- activerecord/lib/active_record/dynamic_finder_match.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/dynamic_finder_match.rb b/activerecord/lib/active_record/dynamic_finder_match.rb index 533bc331ae..e8f3f8b560 100644 --- a/activerecord/lib/active_record/dynamic_finder_match.rb +++ b/activerecord/lib/active_record/dynamic_finder_match.rb @@ -6,8 +6,8 @@ module ActiveRecord # class DynamicFinderMatch def self.match(method) - df_match = self.new(method) - df_match.finder ? df_match : nil + df_match = new(method) + df_match.finder && df_match end def initialize(method) @@ -35,11 +35,11 @@ module ActiveRecord attr_reader :finder, :attribute_names, :instantiator def finder? - !@finder.nil? && @instantiator.nil? + @finder && !@instantiator end def instantiator? - @finder == :first && !@instantiator.nil? + @finder == :first && @instantiator end def creator? -- cgit v1.2.3 From ff5b3f5c3aa36e19f2aad5e9d64f0710e81a555d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 16:08:52 -0700 Subject: DRY up our regular expression --- activerecord/lib/active_record/dynamic_finder_match.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/dynamic_finder_match.rb b/activerecord/lib/active_record/dynamic_finder_match.rb index e8f3f8b560..9493272483 100644 --- a/activerecord/lib/active_record/dynamic_finder_match.rb +++ b/activerecord/lib/active_record/dynamic_finder_match.rb @@ -16,9 +16,9 @@ module ActiveRecord @instantiator = nil case method.to_s - when /^find_(all_by|last_by|by)_([_a-zA-Z]\w*)$/ - @finder = :last if $1 == 'last_by' - @finder = :all if $1 == 'all_by' + when /^find_(all_|last_)?by_([_a-zA-Z]\w*)$/ + @finder = :last if $1 == 'last_' + @finder = :all if $1 == 'all_' names = $2 when /^find_by_([_a-zA-Z]\w*)\!$/ @bang = true -- cgit v1.2.3 From f6ef4d383edd0403e5c2bb8390c9fec9f0843722 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 16:19:14 -0700 Subject: do not need intermediate variable, avoid lasgn --- activerecord/lib/active_record/base.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f5291b180e..a9361f96f0 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1014,8 +1014,9 @@ module ActiveRecord #:nodoc: end def all_attributes_exists?(attribute_names) - attribute_names = expand_attribute_names_for_aggregates(attribute_names) - attribute_names.all? { |name| column_methods_hash.include?(name.to_sym) } + expand_attribute_names_for_aggregates(attribute_names).all? { |name| + column_methods_hash.include?(name.to_sym) + } end protected -- cgit v1.2.3 From b1f5e90839a760c926403046ef43dd60af9fcf07 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 16:20:32 -0700 Subject: no need for a case / when statement --- activerecord/lib/active_record/dynamic_scope_match.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/dynamic_scope_match.rb b/activerecord/lib/active_record/dynamic_scope_match.rb index 61c3ea0e7f..4e51df9e71 100644 --- a/activerecord/lib/active_record/dynamic_scope_match.rb +++ b/activerecord/lib/active_record/dynamic_scope_match.rb @@ -8,25 +8,21 @@ module ActiveRecord # scope except that it's dynamic. class DynamicScopeMatch def self.match(method) - ds_match = self.new(method) - ds_match.scope ? ds_match : nil + ds_match = new(method) + ds_match.scope && ds_match end def initialize(method) - @scope = true - case method.to_s - when /^scoped_by_([_a-zA-Z]\w*)$/ + @scope = nil + if method.to_s =~ /^scoped_by_([_a-zA-Z]\w*)$/ names = $1 - else - @scope = nil + @scope = true end + @attribute_names = names && names.split('_and_') end attr_reader :scope, :attribute_names - - def scope? - !@scope.nil? - end + alias :scope? :scope end end -- cgit v1.2.3 From 133742d185c2abf0fb443b694a305a4b68259bcb Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 16:51:12 -0700 Subject: @klass also uses DynamicFinderMatch, so no need for it on the relation --- activerecord/lib/active_record/relation.rb | 9 --------- 1 file changed, 9 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 478f1e8ef1..dfc66cdd09 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -358,15 +358,6 @@ module ActiveRecord scoping { @klass.send(method, *args, &block) } elsif arel.respond_to?(method) arel.send(method, *args, &block) - elsif match = DynamicFinderMatch.match(method) - attributes = match.attribute_names - super unless @klass.send(:all_attributes_exists?, attributes) - - if match.finder? - find_by_attributes(match, attributes, *args) - elsif match.instantiator? - find_or_instantiator_by_attributes(match, attributes, *args, &block) - end else super end -- cgit v1.2.3 From 65d74312c86c09ab59af7ff9414db901aa6164f0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 17:38:49 -0700 Subject: constructor should not do so much work; avoid allocating object if possible --- activerecord/lib/active_record/dynamic_scope_match.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/dynamic_scope_match.rb b/activerecord/lib/active_record/dynamic_scope_match.rb index 4e51df9e71..c832e927d6 100644 --- a/activerecord/lib/active_record/dynamic_scope_match.rb +++ b/activerecord/lib/active_record/dynamic_scope_match.rb @@ -8,18 +8,13 @@ module ActiveRecord # scope except that it's dynamic. class DynamicScopeMatch def self.match(method) - ds_match = new(method) - ds_match.scope && ds_match + return unless method.to_s =~ /^scoped_by_([_a-zA-Z]\w*)$/ + new(true, $1 && $1.split('_and_')) end - def initialize(method) - @scope = nil - if method.to_s =~ /^scoped_by_([_a-zA-Z]\w*)$/ - names = $1 - @scope = true - end - - @attribute_names = names && names.split('_and_') + def initialize(scope, attribute_names) + @scope = scope + @attribute_names = attribute_names end attr_reader :scope, :attribute_names -- cgit v1.2.3 From 7752b2fa629d55e870342b1bafa57d6805007497 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 18:07:35 -0700 Subject: be kind to the garbage collector: only instantiate objects when absolutely necessary --- .../lib/active_record/dynamic_finder_match.rb | 31 ++++++++++++---------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/dynamic_finder_match.rb b/activerecord/lib/active_record/dynamic_finder_match.rb index 9493272483..b309df9b1b 100644 --- a/activerecord/lib/active_record/dynamic_finder_match.rb +++ b/activerecord/lib/active_record/dynamic_finder_match.rb @@ -6,30 +6,33 @@ module ActiveRecord # class DynamicFinderMatch def self.match(method) - df_match = new(method) - df_match.finder && df_match - end - - def initialize(method) - @finder = :first - @bang = false - @instantiator = nil + finder = :first + bang = false + instantiator = nil case method.to_s when /^find_(all_|last_)?by_([_a-zA-Z]\w*)$/ - @finder = :last if $1 == 'last_' - @finder = :all if $1 == 'all_' + finder = :last if $1 == 'last_' + finder = :all if $1 == 'all_' names = $2 when /^find_by_([_a-zA-Z]\w*)\!$/ - @bang = true + bang = true names = $1 when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/ - @instantiator = $1 == 'initialize' ? :new : :create + instantiator = $1 == 'initialize' ? :new : :create names = $2 else - @finder = nil + return nil end - @attribute_names = names && names.split('_and_') + + new(finder, instantiator, bang, names.split('_and_')) + end + + def initialize(finder, instantiator, bang, attribute_names) + @finder = finder + @instantiator = instantiator + @bang = bang + @attribute_names = attribute_names end attr_reader :finder, :attribute_names, :instantiator -- cgit v1.2.3 From b7934afe326f8bba9cdcacdfac93062dff155efe Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Sep 2010 18:55:28 -0700 Subject: =?UTF-8?q?use=20new=20skool=20Ruby=20instead=20of=20Ruby=20Classi?= =?UTF-8?q?c=E2=84=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- activerecord/lib/active_record/fixtures.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 826031a3e3..6fb723f2f5 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -704,11 +704,9 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash) end def read_yaml_fixture_files - yaml_string = "" - Dir["#{@fixture_path}/**/*.yml"].select { |f| test(?f, f) }.each do |subfixture_path| - yaml_string << IO.read(subfixture_path) - end - yaml_string << IO.read(yaml_file_path) + yaml_string = (Dir["#{@fixture_path}/**/*.yml"].select { |f| + File.file?(f) + } + [yaml_file_path]).map { |file_path| IO.read(file_path) }.join if yaml = parse_yaml_string(yaml_string) # If the file is an ordered map, extract its children. -- cgit v1.2.3 From 9eca11a4a564f44675cca951216e917b8f610eab Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 10:04:33 -0700 Subject: porting 066518295032a8e3f3468737337b8c8299442867 to master. Thanks Marcelo Giorgi --- activerecord/lib/active_record/association_preload.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index e6b367790b..456cd75ab1 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -279,7 +279,7 @@ module ActiveRecord end else options = {} - options[:include] = reflection.options[:include] || reflection.options[:source] if reflection.options[:conditions] + options[:include] = reflection.options[:include] || reflection.options[:source] if reflection.options[:conditions] || reflection.options[:order] options[:order] = reflection.options[:order] options[:conditions] = reflection.options[:conditions] records.first.class.preload_associations(records, through_association, options) -- cgit v1.2.3 From 8b8730e1390963a8bb3a231ed1a324862d3dc4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Mon, 27 Sep 2010 20:59:45 +0200 Subject: Test add_index and remove_index with a symbol name #4891 --- .../connection_adapters/abstract/schema_statements.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 6c29e67e17..ce6782aac7 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -327,14 +327,12 @@ module ActiveRecord # # Note: SQLite doesn't support index length def add_index(table_name, column_name, options = {}) - options[:name] = options[:name].to_s if options.key?(:name) - column_names = Array.wrap(column_name) index_name = index_name(table_name, :column => column_names) if Hash === options # legacy support, since this param was a string index_type = options[:unique] ? "UNIQUE" : "" - index_name = options[:name] || index_name + index_name = options[:name].to_s if options.key?(:name) else index_type = options end @@ -404,7 +402,8 @@ module ActiveRecord # as there's no way to determine the correct answer in that case. def index_name_exists?(table_name, index_name, default) return default unless respond_to?(:indexes) - indexes(table_name).detect { |i| i.name == index_name.to_s } + index_name = index_name.to_s + indexes(table_name).detect { |i| i.name == index_name } end # Returns a string of CREATE TABLE SQL statement(s) for recreating the -- cgit v1.2.3 From bf2223d0e0a5562fb2e946abca4289953134cb77 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 10:30:42 -0700 Subject: removing an inject + merge in favor of Hash#[] --- activerecord/lib/active_record/relation/calculations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 12a2c6aec3..63689ed925 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -222,7 +222,7 @@ module ActiveRecord if association key_ids = calculated_data.collect { |row| row[group_alias] } key_records = association.klass.base_class.find(key_ids) - key_records = key_records.inject({}) { |hsh, r| hsh.merge(r.id => r) } + key_records = Hash[key_records.map { |r| [r.id, r] }] end ActiveSupport::OrderedHash[calculated_data.map do |row| -- cgit v1.2.3 From 9b561ab029f56f3bea61700ffcc7c5c0dd763d25 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 10:37:35 -0700 Subject: avoid calling to_sql when we can --- activerecord/lib/active_record/relation/calculations.rb | 6 +++--- activerecord/lib/active_record/relation/query_methods.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 63689ed925..86b89c741d 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -208,12 +208,12 @@ module ActiveRecord aggregate_alias = column_alias_for(operation, column_name) select_statement = if operation == 'count' && column_name == :all - "COUNT(*) AS count_all" + ["COUNT(*) AS count_all"] else - Arel::Attribute.new(@klass.unscoped.table, column_name).send(operation).as(aggregate_alias).to_sql + [Arel::Attribute.new(@klass.unscoped.table, column_name).send(operation).as(aggregate_alias)] end - select_statement << ", #{group_field} AS #{group_alias}" + select_statement << "#{group_field} AS #{group_alias}" relation = except(:group).select(select_statement).group(group) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index b8cfdb54e5..b9957c41fa 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -36,7 +36,7 @@ module ActiveRecord to_a.select {|*block_args| value.call(*block_args) } else relation = clone - relation.select_values += [value] + relation.select_values += Array.wrap(value) relation end end -- cgit v1.2.3 From e001ea09400af589ff629a895a1ae946c6d5ef69 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 10:40:43 -0700 Subject: no need for splat and flatten --- activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 0571e0cd14..5b2a202b73 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -360,7 +360,7 @@ module ActiveRecord end def copy_table_contents(from, to, columns, rename = {}) #:nodoc: - column_mappings = Hash[*columns.map {|name| [name, name]}.flatten] + column_mappings = Hash[columns.map {|name| [name, name]}] rename.inject(column_mappings) {|map, a| map[a.last] = a.first; map} from_columns = columns(from).collect {|col| col.name} columns = columns.find_all{|col| from_columns.include?(column_mappings[col])} -- cgit v1.2.3 From 2fc5c6333b64f01194abd45150c2891f7cc755fe Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 10:43:18 -0700 Subject: each works well too --- activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 5b2a202b73..c0cc7ba20d 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -361,7 +361,7 @@ module ActiveRecord def copy_table_contents(from, to, columns, rename = {}) #:nodoc: column_mappings = Hash[columns.map {|name| [name, name]}] - rename.inject(column_mappings) {|map, a| map[a.last] = a.first; map} + rename.each { |a| column_mappings[a.last] = a.first } from_columns = columns(from).collect {|col| col.name} columns = columns.find_all{|col| from_columns.include?(column_mappings[col])} quoted_columns = columns.map { |col| quote_column_name(col) } * ',' -- cgit v1.2.3 From cdfd013dd7953fc87038c73ecddbaa8cfe29a301 Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Wed, 15 Sep 2010 12:26:54 -0300 Subject: Set attributes properly for model built from association with conditions [#5562 state:resolved] Signed-off-by: Santiago Pastorino --- activerecord/lib/active_record/base.rb | 17 +++++++++-------- activerecord/lib/active_record/relation.rb | 6 +++++- 2 files changed, 14 insertions(+), 9 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index a9361f96f0..2157a0aded 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1384,10 +1384,7 @@ MSG ensure_proper_type - if scope = self.class.send(:current_scoped_methods) - create_with = scope.scope_for_create - create_with.each { |att,value| self.send("#{att}=", value) } if create_with - end + populate_with_current_scope_attributes self.attributes = attributes unless attributes.nil? result = yield self if block_given? @@ -1416,10 +1413,7 @@ MSG @new_record = true ensure_proper_type - if scope = self.class.send(:current_scoped_methods) - create_with = scope.scope_for_create - create_with.each { |att,value| self.send("#{att}=", value) } if create_with - end + populate_with_current_scope_attributes end # Returns a String, which Action Pack uses for constructing an URL to this @@ -1808,6 +1802,13 @@ MSG return string unless string.is_a?(String) && string =~ /^---/ YAML::load(string) rescue string end + + def populate_with_current_scope_attributes + if scope = self.class.send(:current_scoped_methods) + create_with = scope.scope_for_create + create_with.each { |att,value| self.respond_to?(:"#{att}=") && self.send("#{att}=", value) } if create_with + end + end end Base.class_eval do diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index dfc66cdd09..062293eea4 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -326,7 +326,11 @@ module ActiveRecord def scope_for_create @scope_for_create ||= begin - @create_with_value || where_values_hash + if @create_with_value + @create_with_value.reverse_merge(where_values_hash || {}) + else + where_values_hash + end end end -- cgit v1.2.3 From d0fb0d770f6253bbab75e32e9190c4e89b084f26 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 15:32:12 -0700 Subject: fisting the postgresql tests --- activerecord/lib/active_record/relation/calculations.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 86b89c741d..0b3d1ed3ff 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -215,7 +215,8 @@ module ActiveRecord select_statement << "#{group_field} AS #{group_alias}" - relation = except(:group).select(select_statement).group(group) + relation = except(:group).group(group) + relation.select_values = select_statement calculated_data = @klass.connection.select_all(relation.to_sql) -- cgit v1.2.3 From 10041e2d4542f347e9ce85dc44ba41ea3ab09644 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 15:39:24 -0700 Subject: removing a conditional that is not used --- activerecord/lib/active_record/relation/query_methods.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index b9957c41fa..b25388c0e8 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -264,8 +264,6 @@ module ActiveRecord case select = selects.last when Arel::Expression, Arel::SqlLiteral arel.project(select) - when /^COUNT\(/ - arel.project(Arel::SqlLiteral.new(select)) else arel.project(*selects) end -- cgit v1.2.3 From 16f5d297558671eb840a4e10aad6c769aa604c20 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 15:51:00 -0700 Subject: Revert "porting 066518295032a8e3f3468737337b8c8299442867 to master. Thanks Marcelo Giorgi" This reverts commit 9eca11a4a564f44675cca951216e917b8f610eab. --- activerecord/lib/active_record/association_preload.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 456cd75ab1..e6b367790b 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -279,7 +279,7 @@ module ActiveRecord end else options = {} - options[:include] = reflection.options[:include] || reflection.options[:source] if reflection.options[:conditions] || reflection.options[:order] + options[:include] = reflection.options[:include] || reflection.options[:source] if reflection.options[:conditions] options[:order] = reflection.options[:order] options[:conditions] = reflection.options[:conditions] records.first.class.preload_associations(records, through_association, options) -- cgit v1.2.3 From 3f16103daf330a067454dd2ae324d8d238ac5ba3 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 28 Sep 2010 14:32:59 -0400 Subject: performance improvement based on discussion at http://github.com/rails/rails/commit/fbd1d306b95cc2efb6422e12d26d5818a3a42343 Credit goes to all the participants in the discussion --- activerecord/lib/active_record/relation/query_methods.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index b25388c0e8..e2b1330c24 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -280,15 +280,9 @@ module ActiveRecord end def reverse_sql_order(order_query) - order_query.join(', ').split(',').collect { |s| - if s.match(/\s(asc|ASC)$/) - s.gsub(/\s(asc|ASC)$/, ' DESC') - elsif s.match(/\s(desc|DESC)$/) - s.gsub(/\s(desc|DESC)$/, ' ASC') - else - s + ' DESC' - end - } + order_query.join(', ').split(',').collect do |s| + s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC') + end end def array_of_strings?(o) -- cgit v1.2.3 From c2cad2d97e5feb872ac9222d8f4e393201f4ad18 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 15:56:08 -0700 Subject: where_values_hash always returns a hash --- activerecord/lib/active_record/relation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 062293eea4..04ba5b291e 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -327,7 +327,7 @@ module ActiveRecord def scope_for_create @scope_for_create ||= begin if @create_with_value - @create_with_value.reverse_merge(where_values_hash || {}) + @create_with_value.reverse_merge(where_values_hash) else where_values_hash end -- cgit v1.2.3 From e6ca7e7197f46df0e538f50cea236cb923e4b47f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 16:14:39 -0700 Subject: refactoring to remove crazy logic --- activerecord/lib/active_record/relation/calculations.rb | 6 +++++- activerecord/lib/active_record/relation/query_methods.rb | 9 +-------- 2 files changed, 6 insertions(+), 9 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 0b3d1ed3ff..03862c78e4 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -191,7 +191,11 @@ module ActiveRecord end # Postgresql doesn't like ORDER BY when there are no GROUP BY - relation = except(:order).select(operation == 'count' ? column.count(distinct) : column.send(operation)) + relation = except(:order) + select_value = operation == 'count' ? column.count(distinct) : column.send(operation) + + relation.select_values = [select_value] + type_cast_calculated_value(@klass.connection.select_value(relation.to_sql), column_for(column_name), operation) end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index e2b1330c24..bdd171135c 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -259,14 +259,7 @@ module ActiveRecord def build_select(arel, selects) unless selects.empty? @implicit_readonly = false - # TODO: fix this ugly hack, we should refactor the callers to get an Arel compatible array. - # Before this change we were passing to Arel the last element only, and Arel is capable of handling an array - case select = selects.last - when Arel::Expression, Arel::SqlLiteral - arel.project(select) - else - arel.project(*selects) - end + arel.project(*selects) else arel.project(Arel::SqlLiteral.new(@klass.quoted_table_name + '.*')) end -- cgit v1.2.3 From ef2392f60bb1dae27f418e506eca56c9422e8f46 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 16:59:38 -0700 Subject: removing unused lasgns --- activerecord/lib/active_record/relation/query_methods.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index bdd171135c..502a220f5b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -149,7 +149,7 @@ module ActiveRecord end def custom_join_sql(*joins) - arel = table + arel = table.select_manager joins.each do |join| next if join.blank? @@ -160,12 +160,12 @@ module ActiveRecord when Hash, Array, Symbol if array_of_strings?(join) join_string = join.join(' ') - arel = arel.join(Arel::SqlLiteral.new(join_string)) + arel.join(Arel::SqlLiteral.new(join_string)) end when String - arel = arel.join(Arel::SqlLiteral.new(join)) + arel.join(Arel::SqlLiteral.new(join)) else - arel = arel.join(join) + arel.join(join) end end -- cgit v1.2.3 From 957e6fbe2a313eceed9d3d8f6fa7e65f78756fe8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 17:03:36 -0700 Subject: dry up calls to arel.join() --- activerecord/lib/active_record/relation/query_methods.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 502a220f5b..b7c15fb808 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -159,14 +159,13 @@ module ActiveRecord case join when Hash, Array, Symbol if array_of_strings?(join) - join_string = join.join(' ') - arel.join(Arel::SqlLiteral.new(join_string)) + join = Arel::SqlLiteral.new(join.join(' ')) end when String - arel.join(Arel::SqlLiteral.new(join)) - else - arel.join(join) + join = Arel::SqlLiteral.new(join) end + + arel.join(join) end arel.joins(arel) -- cgit v1.2.3 From 39d98e5c0925f6ba00e8a6d0058b3230ee76f6d2 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 17:05:32 -0700 Subject: we only care about arrays and strings --- activerecord/lib/active_record/relation/query_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index b7c15fb808..e99009a111 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -157,7 +157,7 @@ module ActiveRecord @implicit_readonly = true case join - when Hash, Array, Symbol + when Array if array_of_strings?(join) join = Arel::SqlLiteral.new(join.join(' ')) end -- cgit v1.2.3 From b50b1ef9e8ea33c8fb3fa1cac915baca64edfbea Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 17:12:02 -0700 Subject: shorten up or sql literal creation statements --- activerecord/lib/active_record/relation/query_methods.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index e99009a111..762ae8e2a4 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -158,11 +158,9 @@ module ActiveRecord case join when Array - if array_of_strings?(join) - join = Arel::SqlLiteral.new(join.join(' ')) - end + join = Arel.sql(join.join(' ')) if array_of_strings?(join) when String - join = Arel::SqlLiteral.new(join) + join = Arel.sql(join) end arel.join(join) -- cgit v1.2.3 From 8d9f7b5b9987cccd392442b92c3945fc20a12fff Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 17:31:31 -0700 Subject: SqlLiteral is a string, so we can dry up these conditionals --- activerecord/lib/active_record/relation/query_methods.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 762ae8e2a4..182c293d30 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -176,11 +176,10 @@ module ActiveRecord (@where_values - ['']).uniq.each do |where| case where - when Arel::SqlLiteral - arel = arel.where(where) + when String + arel = arel.where(Arel.sql("(#{where})")) else - sql = where.is_a?(String) ? where : where.to_sql(table.engine) - arel = arel.where(Arel::SqlLiteral.new("(#{sql})")) + arel = arel.where(Arel::Nodes::Grouping.new(where)) end end -- cgit v1.2.3 From dec3a759d189c3acb812bef3aebcc5a2e061c28a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 28 Sep 2010 17:32:38 -0700 Subject: removing more useless code! yay! --- activerecord/lib/active_record/relation/query_methods.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 182c293d30..2e0a2effc2 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -175,12 +175,8 @@ module ActiveRecord arel = build_joins(arel, @joins_values) unless @joins_values.empty? (@where_values - ['']).uniq.each do |where| - case where - when String - arel = arel.where(Arel.sql("(#{where})")) - else - arel = arel.where(Arel::Nodes::Grouping.new(where)) - end + where = Arel.sql(where) if String === where + arel = arel.where(Arel::Nodes::Grouping.new(where)) end arel = arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty? -- cgit v1.2.3 From b3c7766df71938ca05e026d1cb78b12158fd49dc Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 29 Sep 2010 09:42:19 -0700 Subject: just require sqlite3 when the database adapter is required --- .../lib/active_record/connection_adapters/sqlite3_adapter.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index e5e92f2b1c..2b08fdae57 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -1,4 +1,5 @@ require 'active_record/connection_adapters/sqlite_adapter' +require 'sqlite3' module ActiveRecord class Base @@ -20,10 +21,6 @@ module ActiveRecord raise ArgumentError, 'adapter name should be "sqlite3"' end - unless self.class.const_defined?(:SQLite3) - require_library_or_gem(config[:adapter]) - end - db = SQLite3::Database.new( config[:database], :results_as_hash => true -- cgit v1.2.3 From 995ad3b3781c7a310daa11f728f2c5585c45c05d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 29 Sep 2010 09:45:05 -0700 Subject: just use if rather than nil? --- activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 2b08fdae57..5ca1923d89 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -26,7 +26,7 @@ module ActiveRecord :results_as_hash => true ) - db.busy_timeout(config[:timeout]) unless config[:timeout].nil? + db.busy_timeout(config[:timeout]) if config[:timeout] ConnectionAdapters::SQLite3Adapter.new(db, logger, config) end -- cgit v1.2.3 From b2979117e0adad6f86370074237f57d96cc7c1c7 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 29 Sep 2010 10:15:18 -0700 Subject: use inheritence to deal with custom methods --- .../connection_adapters/abstract/schema_statements.rb | 13 +++++++++---- .../connection_adapters/postgresql_adapter.rb | 17 +++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index ce6782aac7..4e770c37da 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -151,10 +151,10 @@ module ActiveRecord # # See also TableDefinition#column for details on how to create columns. def create_table(table_name, options = {}) - table_definition = TableDefinition.new(self) - table_definition.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false + td = table_definition + td.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false - yield table_definition if block_given? + yield td if block_given? if options[:force] && table_exists?(table_name) drop_table(table_name, options) @@ -162,7 +162,7 @@ module ActiveRecord create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE " create_sql << "#{quote_table_name(table_name)} (" - create_sql << table_definition.to_sql + create_sql << td.to_sql create_sql << ") #{options[:options]}" execute create_sql end @@ -534,6 +534,11 @@ module ActiveRecord def options_include_default?(options) options.include?(:default) && !(options[:null] == false && options[:default].nil?) end + + private + def table_definition + TableDefinition.new(self) + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 0a2bacdb84..5078495ed8 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -27,12 +27,6 @@ module ActiveRecord end module ConnectionAdapters - class TableDefinition - def xml(*args) - options = args.extract_options! - column(args[0], 'xml', options) - end - end # PostgreSQL-specific extensions to column definitions in a table. class PostgreSQLColumn < Column #:nodoc: # Instantiates a new PostgreSQL column definition in a table. @@ -192,6 +186,13 @@ module ActiveRecord # * :allow_concurrency - If true, use async query methods so Ruby threads don't deadlock; # otherwise, use blocking query methods. class PostgreSQLAdapter < AbstractAdapter + class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition + def xml(*args) + options = args.extract_options! + column(args[0], 'xml', options) + end + end + ADAPTER_NAME = 'PostgreSQL'.freeze NATIVE_DATABASE_TYPES = { @@ -1024,6 +1025,10 @@ module ActiveRecord [match_data[1], (rest.length > 0 ? rest : nil)] end end + + def table_definition + TableDefinition.new(self) + end end end end -- cgit v1.2.3 From b5b7f2e442081e7270de2aadbe5eeec1759e7a0f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 29 Sep 2010 10:18:27 -0700 Subject: only reopen ConnectionAdapters once --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 5078495ed8..7adc2c2276 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -164,9 +164,7 @@ module ActiveRecord end end end - end - module ConnectionAdapters # The PostgreSQL adapter works both with the native C (http://ruby.scripting.ca/postgres/) and the pure # Ruby (available both as gem and from http://rubyforge.org/frs/?group_id=234&release_id=1944) drivers. # -- cgit v1.2.3 From df9abfab6f213440d3c4dd75973a1ffa8a21b34b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 29 Sep 2010 10:20:44 -0700 Subject: require pg when this adapter is loaded --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 7adc2c2276..91e323e672 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -1,20 +1,19 @@ require 'active_record/connection_adapters/abstract_adapter' require 'active_support/core_ext/kernel/requires' require 'active_support/core_ext/object/blank' +require 'pg' module ActiveRecord class Base # Establishes a connection to the database that's used by all Active Record objects def self.postgresql_connection(config) # :nodoc: - require 'pg' - config = config.symbolize_keys host = config[:host] port = config[:port] || 5432 username = config[:username].to_s if config[:username] password = config[:password].to_s if config[:password] - if config.has_key?(:database) + if config.key?(:database) database = config[:database] else raise ArgumentError, "No database specified. Missing argument: database." @@ -191,10 +190,10 @@ module ActiveRecord end end - ADAPTER_NAME = 'PostgreSQL'.freeze + ADAPTER_NAME = 'PostgreSQL' NATIVE_DATABASE_TYPES = { - :primary_key => "serial primary key".freeze, + :primary_key => "serial primary key", :string => { :name => "character varying", :limit => 255 }, :text => { :name => "text" }, :integer => { :name => "integer" }, -- cgit v1.2.3 From 5e77872ac9da3007d6bc0b86428dca9ac8881936 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 29 Sep 2010 10:23:03 -0700 Subject: use consistent method calls for column inspection --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 91e323e672..c9911af9ff 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -315,7 +315,7 @@ module ActiveRecord def quote(value, column = nil) #:nodoc: return super unless column - if value.kind_of?(String) && column.type == :binary + if value.kind_of?(String) && column.sql_type == 'bytea' "'#{escape_bytea(value)}'" elsif value.kind_of?(String) && column.sql_type == 'xml' "xml '#{quote_string(value)}'" -- cgit v1.2.3 From 4350a5c73f0822264ba333576b09caf871b12919 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 29 Sep 2010 10:23:26 -0700 Subject: fixing where clause indentation --- .../lib/active_record/connection_adapters/postgresql_adapter.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index c9911af9ff..a45e637dda 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -324,10 +324,10 @@ module ActiveRecord "'#{value}'" elsif value.kind_of?(String) && column.sql_type =~ /^bit/ case value - when /^[01]*$/ - "B'#{value}'" # Bit-string notation - when /^[0-9A-F]*$/i - "X'#{value}'" # Hexadecimal notation + when /^[01]*$/ + "B'#{value}'" # Bit-string notation + when /^[0-9A-F]*$/i + "X'#{value}'" # Hexadecimal notation end else super -- cgit v1.2.3 From b9215273a9440f92859b84cca86dfd03ef2de88f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 29 Sep 2010 10:31:26 -0700 Subject: DRY up postgresql quote logic --- .../connection_adapters/postgresql_adapter.rb | 25 ++++++++++++---------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index a45e637dda..194842a9a0 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -315,19 +315,22 @@ module ActiveRecord def quote(value, column = nil) #:nodoc: return super unless column - if value.kind_of?(String) && column.sql_type == 'bytea' - "'#{escape_bytea(value)}'" - elsif value.kind_of?(String) && column.sql_type == 'xml' - "xml '#{quote_string(value)}'" - elsif value.kind_of?(Numeric) && column.sql_type == 'money' + case value + when Numeric + return super unless column.sql_type == 'money' # Not truly string input, so doesn't require (or allow) escape string syntax. "'#{value}'" - elsif value.kind_of?(String) && column.sql_type =~ /^bit/ - case value - when /^[01]*$/ - "B'#{value}'" # Bit-string notation - when /^[0-9A-F]*$/i - "X'#{value}'" # Hexadecimal notation + when String + case column.sql_type + when 'bytea' then "'#{escape_bytea(value)}'" + when 'xml' then "xml '#{quote_string(value)}'" + when /^bit/ + case value + when /^[01]*$/ then "B'#{value}'" # Bit-string notation + when /^[0-9A-F]*$/i then "X'#{value}'" # Hexadecimal notation + end + else + super end else super -- cgit v1.2.3 From 5793d5e0023257962ed9a8ef980062cddd30ce19 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 29 Sep 2010 11:18:43 -0700 Subject: eliminating method_missing on TableDefinition --- .../abstract/schema_definitions.rb | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 84fc4c03f9..6480aeb171 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -318,21 +318,13 @@ module ActiveRecord @base = base end - #Handles non supported datatypes - e.g. XML - def method_missing(symbol, *args) - if symbol.to_s == 'xml' - xml_column_fallback(args) - else - super - end - end + def xml(*args) + raise NotImplementedError unless %w{ + sqlite mysql mysql2 + }.include? @base.adapter_name.downcase - def xml_column_fallback(*args) - case @base.adapter_name.downcase - when 'sqlite', 'mysql' - options = args.extract_options! - column(args[0], :text, options) - end + options = args.extract_options! + column(args[0], :text, options) end # Appends a primary key definition to the table definition. -- cgit v1.2.3 From 299e9f692798f502bd74d16655b611bccf520620 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 29 Sep 2010 14:17:59 -0400 Subject: no need of nil check --- .../active_record/associations/belongs_to_polymorphic_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb index 38454ec242..e429806b0c 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -39,7 +39,7 @@ module ActiveRecord def set_inverse_instance(record, instance) return if record.nil? || !we_can_set_the_inverse_on_this?(record) inverse_relationship = @reflection.polymorphic_inverse_of(record.class) - unless inverse_relationship.nil? + if inverse_relationship record.send(:"set_#{inverse_relationship.name}_target", instance) end end -- cgit v1.2.3 From 396f3a28f175dcdae6fdcd5139b9dd5defde18de Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Thu, 30 Sep 2010 02:30:33 +0800 Subject: double negative is not good --- activerecord/lib/active_record/serialization.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb index ad3f7afd6f..398eb1534a 100644 --- a/activerecord/lib/active_record/serialization.rb +++ b/activerecord/lib/active_record/serialization.rb @@ -45,7 +45,7 @@ module ActiveRecord #:nodoc: send(association) end - unless records.nil? + if records association_options = include_has_options ? include_associations[association] : base_only_or_except opts = options.merge(association_options) yield(association, records, opts) -- cgit v1.2.3 From f851352318c7468db310e5fbea0d86dc8b731c6d Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 29 Sep 2010 17:41:30 +0200 Subject: Added config.app_generators to allow configuring application's generators from railties. With config.generators becomes a way to configure generators for current instance only. For example: module Blog class Engine < Rails::Engine config.generators do |g| g.orm :active_record end config.app_generators do |g| g.test_framework :rspec end end end such definition sets :active_record as orm for engine and :rspec as test_framework for application. The values set with app_generators can be overwritten in application using config.generators as you would normally do: module MyApp class Application < Rails::Application config.generators do |g| g.test_framework :test_unit end end end --- activerecord/lib/active_record/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 94dda4e413..868fd6c3ff 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -13,7 +13,7 @@ module ActiveRecord class Railtie < Rails::Railtie config.active_record = ActiveSupport::OrderedOptions.new - config.generators.orm :active_record, :migration => true, + config.app_generators.orm :active_record, :migration => true, :timestamps => true config.app_middleware.insert_after "::ActionDispatch::Callbacks", -- cgit v1.2.3 From 91deff08c940f16ed149e7628694faff0393fe0a Mon Sep 17 00:00:00 2001 From: yalab Date: Fri, 1 Oct 2010 01:26:22 +0900 Subject: Fix 'rake db:create' is ignore encoding when using postgres [#5717 state:resolved] Signed-off-by: Santiago Pastorino --- activerecord/lib/active_record/railties/databases.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 389a5d5884..58c705c8b2 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -108,7 +108,7 @@ namespace :db do end end when 'postgresql' - @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8' + @encoding = config['encoding'] || ENV['CHARSET'] || 'utf8' begin ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public')) ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding)) -- cgit v1.2.3