Skip to content

Commit

Permalink
Merge branch 'master' into asset-path-helper
Browse files Browse the repository at this point in the history
Conflicts:
	railties/test/application/configuration_test.rb
  • Loading branch information
josh committed Oct 15, 2012
2 parents 7dba159 + 46dc6e7 commit c800e27
Show file tree
Hide file tree
Showing 123 changed files with 681 additions and 626 deletions.
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gem 'arel', github: 'rails/arel', branch: 'master'

gem 'mocha', '>= 0.11.2', :require => false
gem 'rack-test', github: 'brynary/rack-test'
gem 'rack-cache', "~> 1.2"
gem 'rack-cache', '~> 1.2'
gem 'bcrypt-ruby', '~> 3.0.0'
gem 'jquery-rails', '~> 2.1.4', github: 'rails/jquery-rails'
gem 'turbolinks'
Expand Down Expand Up @@ -41,8 +41,8 @@ instance_eval File.read local_gemfile if File.exists? local_gemfile

platforms :mri do
group :test do
gem 'ruby-prof', '~> 0.11.2'
gem 'debugger' if !ENV['TRAVIS'] && RUBY_VERSION < "2.0"
gem 'ruby-prof', '~> 0.11.2' if RUBY_VERSION < '2.0'
gem 'debugger' if !ENV['TRAVIS'] && RUBY_VERSION < '2.0' && RUBY_PATCHLEVEL < 286
end
end

Expand Down
6 changes: 4 additions & 2 deletions actionpack/lib/abstract_controller/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ def _handle_action_missing(*args)
# * <tt>string</tt> - The name of the method that handles the action
# * <tt>nil</tt> - No method name could be found. Raise ActionNotFound.
def method_for_action(action_name)
if action_method?(action_name) then action_name
elsif respond_to?(:action_missing, true) then "_handle_action_missing"
if action_method?(action_name)
action_name
elsif respond_to?(:action_missing, true)
"_handle_action_missing"
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions actionpack/lib/action_view/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ module Helpers #:nodoc:

extend ActiveSupport::Concern

included do
extend SanitizeHelper::ClassMethods
end

include ActiveModelHelper
include AssetTagHelper
include AssetUrlHelper
Expand Down
6 changes: 1 addition & 5 deletions actionpack/test/template/sanitize_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_strip_links
end

def test_sanitize_form
assert_sanitized "<form action=\"/foo/bar\" method=\"post\"><input></form>", ''
assert_equal '', sanitize("<form action=\"/foo/bar\" method=\"post\"><input></form>")
end

def test_should_sanitize_illegal_style_properties
Expand Down Expand Up @@ -48,8 +48,4 @@ def test_strip_tags
def test_sanitize_is_marked_safe
assert sanitize("<html><script></script></html>").html_safe?
end

def assert_sanitized(text, expected = nil)
assert_equal((expected || text), sanitize(text))
end
end
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##

* PostgreSQL adapter correctly fetches default values when using multiple schemas and domains in a db. Fixes #7914

*Arturo Pie*

* Learn ActiveRecord::QueryMethods#order work with hash arguments

When symbol or hash passed we convert it to Arel::Nodes::Ordering.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def type_cast(value, column, array_member = false)
else super(value, column)
end
when IPAddr
return super(value, column) unless ['inet','cidr'].includes? column.sql_type
return super(value, column) unless ['inet','cidr'].include? column.sql_type
PostgreSQLColumn.cidr_to_string(value)
else
super(value, column)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,24 +280,21 @@ def pk_and_sequence_for(table) #:nodoc:
end_sql

if result.nil? or result.empty?
# If that fails, try parsing the primary key's default value.
# Support the 7.x and 8.0 nextval('foo'::text) as well as
# the 8.1+ nextval('foo'::regclass).
result = query(<<-end_sql, 'SCHEMA')[0]
SELECT attr.attname,
CASE
WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
substr(split_part(def.adsrc, '''', 2),
strpos(split_part(def.adsrc, '''', 2), '.')+1)
ELSE split_part(def.adsrc, '''', 2)
WHEN split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2) ~ '.' THEN
substr(split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2),
strpos(split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2), '.')+1)
ELSE split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2)
END
FROM pg_class t
JOIN pg_attribute attr ON (t.oid = attrelid)
JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
WHERE t.oid = '#{quote_table_name(table)}'::regclass
AND cons.contype = 'p'
AND def.adsrc ~* 'nextval'
AND pg_get_expr(def.adbin, def.adrelid) ~* 'nextval'
end_sql
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,8 @@ def self.extract_value_from_default(default)
when /\A\(?(-?\d+(\.\d*)?\)?)\z/
$1
# Character types
when /\A'(.*)'::(?:character varying|bpchar|text)\z/m
when /\A\(?'(.*)'::.*\b(?:character varying|bpchar|text)\z/m
$1
# Character types (8.1 formatting)
when /\AE'(.*)'::(?:character varying|bpchar|text)\z/m
$1.gsub(/\\(\d\d\d)/) { $1.oct.chr }
# Binary data types
when /\A'(.*)'::bytea\z/m
$1
Expand Down Expand Up @@ -763,7 +760,8 @@ def select_raw(sql, name = nil)
# - ::regclass is a function that gives the id for a table name
def column_definitions(table_name) #:nodoc:
exec_query(<<-end_sql, 'SCHEMA').rows
SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull, a.atttypid, a.atttypmod
SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '#{quote_table_name(table_name)}'::regclass
Expand Down
22 changes: 17 additions & 5 deletions activerecord/lib/active_record/relation/merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ def merge
# the values.
def other
other = Relation.new(relation.klass, relation.table)
hash.each { |k, v| other.send("#{k}!", v) }
hash.each { |k, v|
if k == :joins
if Hash === v
other.joins!(v)
else
other.joins!(*v)
end
else
other.send("#{k}!", v)
end
}
other
end
end
Expand All @@ -39,16 +49,18 @@ def initialize(relation, other)
@values = other.values
end

NORMAL_VALUES = Relation::SINGLE_VALUE_METHODS +
Relation::MULTI_VALUE_METHODS -
[:where, :order, :bind, :reverse_order, :lock, :create_with, :reordering, :from] # :nodoc:

def normal_values
Relation::SINGLE_VALUE_METHODS +
Relation::MULTI_VALUE_METHODS -
[:where, :order, :bind, :reverse_order, :lock, :create_with, :reordering, :from]
NORMAL_VALUES
end

def merge
normal_values.each do |name|
value = values[name]
relation.send("#{name}!", value) unless value.blank?
relation.send("#{name}!", *value) unless value.blank?
end

merge_multi_values
Expand Down
28 changes: 13 additions & 15 deletions activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ def group!(*args)
#
# User.order('name DESC, email')
# => SELECT "users".* FROM "users" ORDER BY name DESC, email
#
#
# User.order(:name)
# => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC
#
#
# User.order(email: :desc)
# => SELECT "users".* FROM "users" ORDER BY "users"."email" DESC
#
#
# User.order(:name, email: :desc)
# => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC, "users"."email" DESC
def order(*args)
Expand All @@ -218,7 +218,7 @@ def order(*args)
# Like #order, but modifies relation in place.
def order!(*args)
args.flatten!

validate_order_args args

references = args.reject { |arg| Arel::Node === arg }
Expand All @@ -245,7 +245,7 @@ def reorder(*args)
# Like #reorder, but modifies relation in place.
def reorder!(*args)
args.flatten!

validate_order_args args

self.reordering_value = true
Expand All @@ -258,13 +258,11 @@ def reorder!(*args)
# User.joins(:posts)
# => SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id"
def joins(*args)
args.compact.blank? ? self : spawn.joins!(*args)
args.compact.blank? ? self : spawn.joins!(*args.flatten)
end

# Like #joins, but modifies relation in place.
def joins!(*args)
args.flatten!

self.joins_values += args
self
end
Expand Down Expand Up @@ -803,9 +801,9 @@ def reverse_sql_order(order_query)
s.gsub!(/\sasc\Z/i, ' DESC') || s.gsub!(/\sdesc\Z/i, ' ASC') || s.concat(' DESC')
end
when Symbol
{ o => :desc }
{ o => :desc }
when Hash
o.each_with_object({}) do |(field, dir), memo|
o.each_with_object({}) do |(field, dir), memo|
memo[field] = (dir == :asc ? :desc : :asc )
end
else
Expand All @@ -817,25 +815,25 @@ def reverse_sql_order(order_query)
def array_of_strings?(o)
o.is_a?(Array) && o.all?{|obj| obj.is_a?(String)}
end

def build_order(arel)
orders = order_values
orders = reverse_sql_order(orders) if reverse_order_value

orders = orders.uniq.reject(&:blank?).map do |order|
case order
when Symbol
table[order].asc
when Hash
order.map { |field, dir| table[field].send(dir) }
else
else
order
end
end.flatten

arel.order(*orders) unless orders.empty?
end

def validate_order_args(args)
args.select { |a| Hash === a }.each do |h|
unless (h.values - [:asc, :desc]).empty?
Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/validations/uniqueness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ def validate_each(record, attribute, value)
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?

Array(options[:scope]).each do |scope_item|
scope_value = record.read_attribute(scope_item)
reflection = record.class.reflect_on_association(scope_item)
if reflection
scope_value = record.send(reflection.foreign_key)
scope_item = reflection.foreign_key
else
scope_value = record.read_attribute(scope_item)
end
relation = relation.and(table[scope_item].eq(scope_value))
end
Expand Down
13 changes: 13 additions & 0 deletions activerecord/test/cases/adapters/postgresql/quoting_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "cases/helper"
require 'ipaddr'

module ActiveRecord
module ConnectionAdapters
Expand All @@ -20,6 +21,18 @@ def test_type_cast_false
assert_equal 'f', @conn.type_cast(false, c)
end

def test_type_cast_cidr
ip = IPAddr.new('255.0.0.0/8')
c = Column.new(nil, ip, 'cidr')
assert_equal ip, @conn.type_cast(ip, c)
end

def test_type_cast_inet
ip = IPAddr.new('255.1.0.0/8')
c = Column.new(nil, ip, 'inet')
assert_equal ip, @conn.type_cast(ip, c)
end

def test_quote_float_nan
nan = 0.0/0
c = Column.new(nil, 1, 'float')
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/postgresql/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def teardown
end

def test_schema_names
assert_equal ["public", "test_schema", "test_schema2"], @connection.schema_names
assert_equal ["public", "schema_1", "test_schema", "test_schema2"], @connection.schema_names
end

def test_create_schema
Expand Down
40 changes: 40 additions & 0 deletions activerecord/test/cases/defaults_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,43 @@ def test_mysql_integer_not_null_defaults
end
end
end

if current_adapter?(:PostgreSQLAdapter)
class DefaultsUsingMultipleSchemasAndDomainTest < ActiveSupport::TestCase
def setup
@connection = ActiveRecord::Base.connection

@old_search_path = @connection.schema_search_path
@connection.schema_search_path = "schema_1, pg_catalog"
@connection.create_table "defaults" do |t|
t.text "text_col", :default => "some value"
t.string "string_col", :default => "some value"
end
Default.reset_column_information
end

def test_text_defaults_in_new_schema_when_overriding_domain
assert_equal "some value", Default.new.text_col, "Default of text column was not correctly parse"
end

def test_string_defaults_in_new_schema_when_overriding_domain
assert_equal "some value", Default.new.string_col, "Default of string column was not correctly parse"
end

def test_bpchar_defaults_in_new_schema_when_overriding_domain
@connection.execute "ALTER TABLE defaults ADD bpchar_col bpchar DEFAULT 'some value'"
Default.reset_column_information
assert_equal "some value", Default.new.bpchar_col, "Default of bpchar column was not correctly parse"
end

def test_text_defaults_after_updating_column_default
@connection.execute "ALTER TABLE defaults ALTER COLUMN text_col SET DEFAULT 'some text'::schema_1.text"
assert_equal "some text", Default.new.text_col, "Default of text column was not correctly parse after updating default using '::text' since postgreSQL will add parens to the default in db"
end

def teardown
@connection.schema_search_path = @old_search_path
Default.reset_column_information
end
end
end
9 changes: 8 additions & 1 deletion activerecord/test/schema/postgresql_specific_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

execute 'DROP FUNCTION IF EXISTS partitioned_insert_trigger()'

execute "DROP SCHEMA IF EXISTS schema_1 CASCADE"

%w(accounts_id_seq developers_id_seq projects_id_seq topics_id_seq customers_id_seq orders_id_seq).each do |seq_name|
execute "SELECT setval('#{seq_name}', 100)"
end
Expand All @@ -37,7 +39,12 @@
);
_SQL

execute <<_SQL
execute "CREATE SCHEMA schema_1"
execute "CREATE DOMAIN schema_1.text AS text"
execute "CREATE DOMAIN schema_1.varchar AS varchar"
execute "CREATE DOMAIN schema_1.bpchar AS bpchar"

execute <<_SQL
CREATE TABLE geometrics (
id serial primary key,
a_point point,
Expand Down
Loading

0 comments on commit c800e27

Please sign in to comment.