Drop Rails3 support

This commit is contained in:
Stephan Kaag
2014-02-17 17:44:28 +01:00
parent 48ac29ae5d
commit f12925887c
27 changed files with 185 additions and 1522 deletions

View File

@ -33,24 +33,13 @@ else
# Outside of test mode, use after_commit
class DiscourseObserver < ActiveRecord::Observer
def after_commit(model)
if rails4?
if model.send(:transaction_include_any_action?, [:create])
after_create_delegator(model)
end
if model.send(:transaction_include_any_action?, [:destroy])
after_destroy_delegator(model)
end
else
if model.send(:transaction_include_action?, :create)
after_create_delegator(model)
end
if model.send(:transaction_include_action?, :destroy)
after_destroy_delegator(model)
end
if model.send(:transaction_include_any_action?, [:create])
after_create_delegator(model)
end
if model.send(:transaction_include_any_action?, [:destroy])
after_destroy_delegator(model)
end
end
end

View File

@ -15,20 +15,6 @@ class ActiveRecord::Base
ActiveRecord::Base.send(:sanitize_sql_array, sql_array)
end
# exists fine in rails4
unless rails4?
# note: update_attributes still spins up a transaction this can cause contention
# this method performs the raw update sidestepping the locking
# exists in rails 4
def update_columns(hash)
self.class.where(self.class.primary_key => self.id).update_all(hash)
hash.each do |k,v|
raw_write_attribute k, v
end
end
end
def exec_sql(*args)
ActiveRecord::Base.exec_sql(*args)
end

View File

@ -1,14 +0,0 @@
unless rails4?
module ActiveRecord
class Relation
# Patch Rails 3 ActiveRecord::Relation to noop on Rails 4 references
# thereby getting code that works for rails 3 and 4 without
# deprecation warnings
def references(*args)
self
end
end
end
end

View File

@ -1,8 +1,6 @@
if rails4?
# https://github.com/rails/arel/pull/206
class Arel::Table
def hash
@name.hash
end
# https://github.com/rails/arel/pull/206
class Arel::Table
def hash
@name.hash
end
end
end

View File

@ -8,30 +8,4 @@ class ActiveRecord::Base
def blank?
false
end
end
unless rails4?
class ActionView::Helpers::AssetTagHelper::AssetIncludeTag
private
# pluralization is fairly expensive, and pluralizing the word javascript 400 times is pointless
# this is fixed in Rails 4
def path_to_asset(source, options = {})
asset_paths.compute_public_path(source, pluralize_asset_name(asset_name), options.merge(:ext => extension))
end
def path_to_asset_source(source)
asset_paths.compute_source_path(source, pluralize_asset_name(asset_name), extension)
end
def pluralize_asset_name(asset_name)
@@pluralization_cache ||= {}
plural = @@pluralization_cache[asset_name] ||= asset_name.to_s.pluralize
end
end
end
end

View File

@ -1,127 +0,0 @@
unless rails4?
module HTML
class WhiteListSanitizer
# Sanitizes a block of css code. Used by #sanitize when it comes across a style attribute
def sanitize_css(style)
# disallow urls
style = style.to_s.gsub(/url\s*\(\s*[^\s)]+?\s*\)\s*/, ' ')
# gauntlet
if style !~ /\A([:,;#%.\sa-zA-Z0-9!]|\w-\w|\'[\s\w]+\'|\"[\s\w]+\"|\([\d,\s]+\))*\z/ ||
style !~ /\A(\s*[-\w]+\s*:\s*[^:;]*(;|$)\s*)*\z/
return ''
end
clean = []
style.scan(/([-\w]+)\s*:\s*([^:;]*)/) do |prop,val|
if allowed_css_properties.include?(prop.downcase)
clean << prop + ': ' + val + ';'
elsif shorthand_css_properties.include?(prop.split('-')[0].downcase)
unless val.split().any? do |keyword|
!allowed_css_keywords.include?(keyword) &&
keyword !~ /\A(#[0-9a-f]+|rgb\(\d+%?,\d*%?,?\d*%?\)?|\d{0,2}\.?\d{0,2}(cm|em|ex|in|mm|pc|pt|px|%|,|\))?)\z/
end
clean << prop + ': ' + val + ';'
end
end
end
clean.join(' ')
end
end
end
module HTML
class WhiteListSanitizer
self.protocol_separator = /:|(&#0*58)|(&#x70)|(&#x0*3a)|(%|&#37;)3A/i
def contains_bad_protocols?(attr_name, value)
uri_attributes.include?(attr_name) &&
(value =~ /(^[^\/:]*):|(&#0*58)|(&#x70)|(&#x0*3a)|(%|&#37;)3A/i && !allowed_protocols.include?(value.split(protocol_separator).first.downcase.strip))
end
end
end
module ActiveRecord
class Relation
def where_values_hash
values = rails_master? ? where_values : with_default_scope.where_values
equalities = values.grep(Arel::Nodes::Equality).find_all { |node|
node.left.relation.name == table_name
}
Hash[equalities.map { |where| [where.left.name, where.right] }].with_indifferent_access
end
end
end
module ActiveRecord
class PredicateBuilder # :nodoc:
def self.build_from_hash(engine, attributes, default_table, allow_table_name = true)
predicates = attributes.map do |column, value|
table = default_table
if allow_table_name && value.is_a?(Hash)
table = Arel::Table.new(column, engine)
if value.empty?
'1 = 2'
else
build_from_hash(engine, value, table, false)
end
else
column = column.to_s
if allow_table_name && column.include?('.')
table_name, column = column.split('.', 2)
table = Arel::Table.new(table_name, engine)
end
attribute = table[column]
case value
when ActiveRecord::Relation
value = value.select(value.klass.arel_table[value.klass.primary_key]) if value.select_values.empty?
attribute.in(value.arel.ast)
when Array, ActiveRecord::Associations::CollectionProxy
values = value.to_a.map {|x| x.is_a?(ActiveRecord::Base) ? x.id : x}
ranges, values = values.partition {|v| v.is_a?(Range) || v.is_a?(Arel::Relation)}
array_predicates = ranges.map {|range| attribute.in(range)}
if values.include?(nil)
values = values.compact
if values.empty?
array_predicates << attribute.eq(nil)
else
array_predicates << attribute.in(values.compact).or(attribute.eq(nil))
end
else
array_predicates << attribute.in(values)
end
array_predicates.inject {|composite, predicate| composite.or(predicate)}
when Range, Arel::Relation
attribute.in(value)
when ActiveRecord::Base
attribute.eq(value.id)
when Class
# FIXME: I think we need to deprecate this behavior
attribute.eq(value.name)
when Integer, ActiveSupport::Duration
# Arel treats integers as literals, but they should be quoted when compared with strings
column = engine.connection.schema_cache.columns_hash[table.name][attribute.name.to_s]
attribute.eq(Arel::Nodes::SqlLiteral.new(engine.connection.quote(value, column)))
else
attribute.eq(value)
end
end
end
predicates.flatten
end
end
end
end

View File

@ -17,10 +17,8 @@ module Trashable
#
scope = if rails_master?
self.all
elsif rails4?
self.all.with_default_scope
else
self.scoped.with_default_scope
self.all.with_default_scope
end
scope.where_values.delete(with_deleted_scope_sql)
@ -28,11 +26,7 @@ module Trashable
end
def with_deleted_scope_sql
if rails4?
all.table[:deleted_at].eq(nil).to_sql
else
scoped.table[:deleted_at].eq(nil).to_sql
end
all.table[:deleted_at].eq(nil).to_sql
end
end

View File

@ -3,23 +3,17 @@
class IpAddressFormatValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if rails4?
# In Rails 4.0.0, ip_address will be nil if an invalid IP address was assigned.
# https://github.com/jetthoughts/rails/commit/0aa95a71b04f2893921c58a7c1d9fca60dbdcbc2
# In Rails 4.0.0, ip_address will be nil if an invalid IP address was assigned.
# https://github.com/jetthoughts/rails/commit/0aa95a71b04f2893921c58a7c1d9fca60dbdcbc2
# BUT: in Rails 4.0.1, validators don't get a chance to
# run before IPAddr::InvalidAddressError is raised.
# I don't see what broke it in rails 4.0.1...
# So this validator doesn't actually do anything anymore.
# But let's keep it in case a future version of rails fixes the problem and allows
# validators to work on inet and cidr columns.
if record.ip_address.nil?
record.errors.add(attribute, :invalid)
end
else
unless !record.ip_address.nil? and record.ip_address.to_s.split('/').first =~ Resolv::AddressRegex
record.errors.add(attribute, :invalid)
end
# BUT: in Rails 4.0.1, validators don't get a chance to
# run before IPAddr::InvalidAddressError is raised.
# I don't see what broke it in rails 4.0.1...
# So this validator doesn't actually do anything anymore.
# But let's keep it in case a future version of rails fixes the problem and allows
# validators to work on inet and cidr columns.
if record.ip_address.nil?
record.errors.add(attribute, :invalid)
end
end