From bea44cbaa48433d427a9418ff6b5c617b10e0585 Mon Sep 17 00:00:00 2001 From: Chulki Lee Date: Fri, 10 Jan 2014 16:27:18 -0800 Subject: Set NameError#name --- activerecord/lib/active_record/inheritance.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index 949e7678a5..69896f7219 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -126,7 +126,7 @@ module ActiveRecord end end - raise NameError, "uninitialized constant #{candidates.first}" + raise NameError.new("uninitialized constant #{candidates.first}", candidates.first) end end -- cgit v1.2.3 From b242b2dbe75f0b5e86e2ce9ef7c2c5ee96e17862 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Tue, 14 Jan 2014 03:58:22 -0800 Subject: Enum mappings are now exposed via class methods instead of constants. Example: class Conversation < ActiveRecord::Base enum status: [ :active, :archived ] end Before: Conversation::STATUS # => { "active" => 0, "archived" => 1 } After: Conversation.statuses # => { "active" => 0, "archived" => 1 } --- activerecord/lib/active_record/enum.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index d1bc785bee..c34fc086e2 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -56,21 +56,24 @@ module ActiveRecord # In rare circumstances you might need to access the mapping directly. # The mappings are exposed through a constant with the attributes name: # - # Conversation::STATUS # => { "active" => 0, "archived" => 1 } + # Conversation.statuses # => { "active" => 0, "archived" => 1 } # # Use that constant when you need to know the ordinal value of an enum: # - # Conversation.where("status <> ?", Conversation::STATUS[:archived]) + # Conversation.where("status <> ?", Conversation.statuses[:archived]) module Enum def enum(definitions) klass = self definitions.each do |name, values| - # STATUS = { } - enum_values = _enum_methods_module.const_set name.to_s.upcase, ActiveSupport::HashWithIndifferentAccess.new + # statuses = { } + enum_values = ActiveSupport::HashWithIndifferentAccess.new name = name.to_sym + # def self.statuses statuses end + klass.singleton_class.send(:define_method, name.to_s.pluralize) { enum_values } + _enum_methods_module.module_eval do - # def status=(value) self[:status] = STATUS[value] end + # def status=(value) self[:status] = statuses[value] end define_method("#{name}=") { |value| if enum_values.has_key?(value) || value.blank? self[name] = enum_values[value] @@ -84,10 +87,10 @@ module ActiveRecord end } - # def status() STATUS.key self[:status] end + # def status() statuses.key self[:status] end define_method(name) { enum_values.key self[name] } - # def status_before_type_cast() STATUS.key self[:status] end + # def status_before_type_cast() statuses.key self[:status] end define_method("#{name}_before_type_cast") { enum_values.key self[name] } pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index -- cgit v1.2.3 From e8d1d84837a59ef7d73b29b16ee05cd610d30a90 Mon Sep 17 00:00:00 2001 From: Ujjwal Thaakar Date: Tue, 14 Jan 2014 18:53:45 +0530 Subject: Don't try to get the subclass if the inheritance column doesn't exist The `subclass_from_attrs` method is called even if the column specified by the `inheritance_column` setting doesn't exist. This prevents setting associations via the attributes hash if the association name clashes with the value of the setting, typically `:type`. This worked previously in Rails 3.2. --- activerecord/lib/active_record/inheritance.rb | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index 69896f7219..da73112e90 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -18,13 +18,17 @@ module ActiveRecord if abstract_class? || self == Base raise NotImplementedError, "#{self} is an abstract class and cannot be instantiated." end - if (attrs = args.first).is_a?(Hash) - if subclass = subclass_from_attrs(attrs) - return subclass.new(*args, &block) - end + + attrs = args.first + if subclass_from_attributes?(attrs) + subclass = subclass_from_attributes(attrs) + end + + if subclass + subclass.new(*args, &block) + else + super end - # Delegate to the original .new - super end # Returns +true+ if this does not need STI type condition. Returns @@ -172,7 +176,11 @@ module ActiveRecord # is not self or a valid subclass, raises ActiveRecord::SubclassNotFound # If this is a StrongParameters hash, and access to inheritance_column is not permitted, # this will ignore the inheritance column and return nil - def subclass_from_attrs(attrs) + def subclass_from_attributes?(attrs) + columns_hash.include?(inheritance_column) && attrs.is_a?(Hash) + end + + def subclass_from_attributes(attrs) subclass_name = attrs.with_indifferent_access[inheritance_column] if subclass_name.present? && subclass_name != self.name -- cgit v1.2.3 From 547ed456337dda54799d9c5f316dcbce43cfce9d Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Tue, 17 Dec 2013 10:10:23 -0700 Subject: sqlite >= 3.8.0 supports partial indexes --- .../connection_adapters/sqlite3_adapter.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 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 92bb70ba53..170dddb08e 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -155,6 +155,10 @@ module ActiveRecord true end + def supports_partial_index? + sqlite_version >= '3.8.0' + end + # Returns true, since this connection adapter supports prepared statement # caching. def supports_statement_cache? @@ -397,13 +401,25 @@ module ActiveRecord # Returns an array of indexes for the given table. def indexes(table_name, name = nil) #:nodoc: exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", 'SCHEMA').map do |row| + sql = <<-SQL + SELECT sql + FROM sqlite_master + WHERE name=#{quote(row['name'])} AND type='index' + UNION ALL + SELECT sql + FROM sqlite_temp_master + WHERE name=#{quote(row['name'])} AND type='index' + SQL + index_sql = exec_query(sql).first['sql'] + match = /\sWHERE\s+(.+)$/i.match(index_sql) + where = match[1] if match IndexDefinition.new( table_name, row['name'], row['unique'] != 0, exec_query("PRAGMA index_info('#{row['name']}')", "SCHEMA").map { |col| col['name'] - }) + }, nil, nil, where) end end -- cgit v1.2.3