diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2016-12-28 21:53:51 -0500 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2016-12-28 21:53:51 -0500 |
commit | 010e246756c09f44e901f4fd8e8eab2cb3022e95 (patch) | |
tree | de880274810f885232ac51bbf529bab0366eca45 /activerecord/lib | |
parent | 09cdf425d70cfec541e0008c28055155d70c6f37 (diff) | |
download | rails-010e246756c09f44e901f4fd8e8eab2cb3022e95.tar.gz rails-010e246756c09f44e901f4fd8e8eab2cb3022e95.tar.bz2 rails-010e246756c09f44e901f4fd8e8eab2cb3022e95.zip |
Fix Rubocop violations and fix documentation visibility
Some methods were added to public API in
5b14129d8d4ad302b4e11df6bd5c7891b75f393c and they should be not part of
the public API.
Diffstat (limited to 'activerecord/lib')
15 files changed, 147 insertions, 147 deletions
diff --git a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb index 42a90b449c..6b71826431 100644 --- a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb +++ b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb @@ -78,9 +78,9 @@ module ActiveRecord::Associations::Builder # :nodoc: private - def self.suppress_composite_primary_key(pk) - pk unless pk.is_a?(Array) - end + def self.suppress_composite_primary_key(pk) + pk unless pk.is_a?(Array) + end } join_model.name = "HABTM_#{association_name.to_s.camelize}" diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 9f91f2b536..0d84805b4d 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -1128,12 +1128,12 @@ module ActiveRecord private - def find_nth_with_limit(index, limit) # :doc: + def find_nth_with_limit(index, limit) load_target if find_from_target? super end - def find_nth_from_last(index) # :doc: + def find_nth_from_last(index) load_target if find_from_target? super end diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index ffb83a972c..8fcac82a0d 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -52,7 +52,7 @@ module ActiveRecord private - def attribute_method?(attr_name) # :doc: + def attribute_method?(attr_name) attr_name == "id" || super end diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 727b4288e9..369a6e35aa 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -24,7 +24,7 @@ module ActiveRecord # to allocate an object on each call to the attribute method. # Making it frozen means that it doesn't get duped when used to # key the @attributes in read_attribute. - def define_method_attribute(name) # :doc: + def define_method_attribute(name) safe_name = name.unpack("h*".freeze).first temp_method = "__temp__#{safe_name}" diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb index a80ee74f47..fe0e01db28 100644 --- a/activerecord/lib/active_record/attribute_methods/write.rb +++ b/activerecord/lib/active_record/attribute_methods/write.rb @@ -10,7 +10,7 @@ module ActiveRecord module ClassMethods private - def define_method_attribute=(name) # :doc: + def define_method_attribute=(name) safe_name = name.unpack("h*".freeze).first ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name 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 fab5bd0db7..e444cec72b 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -363,31 +363,31 @@ module ActiveRecord private # Returns a subquery for the given key using the join information. - def subquery_for(key, select) # :doc: + def subquery_for(key, select) subselect = select.clone subselect.projections = [key] subselect end # Returns an ActiveRecord::Result instance. - def select(sql, name = nil, binds = []) # :doc: + def select(sql, name = nil, binds = []) exec_query(sql, name, binds, prepare: false) end - def select_prepared(sql, name = nil, binds = []) # :doc: + def select_prepared(sql, name = nil, binds = []) exec_query(sql, name, binds, prepare: true) end - def sql_for_insert(sql, pk, id_value, sequence_name, binds) # :doc: + def sql_for_insert(sql, pk, id_value, sequence_name, binds) [sql, binds] end - def last_inserted_id(result) # :doc: + def last_inserted_id(result) row = result.rows.first row && row.first end - def binds_from_relation(relation, binds) # :doc: + def binds_from_relation(relation, binds) if relation.is_a?(Relation) && binds.empty? relation, binds = relation.arel, relation.bound_attributes end 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 2b131045db..9c820ce585 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -1171,7 +1171,7 @@ module ActiveRecord private - def add_index_sort_order(quoted_columns, **options) # :doc: + def add_index_sort_order(quoted_columns, **options) if order = options[:order] case order when Hash @@ -1186,7 +1186,7 @@ module ActiveRecord end # Overridden by the MySQL adapter for supporting index lengths - def add_options_for_index_columns(quoted_columns, **options) # :doc: + def add_options_for_index_columns(quoted_columns, **options) if supports_index_sort_order? quoted_columns = add_index_sort_order(quoted_columns, options) end @@ -1194,14 +1194,14 @@ module ActiveRecord quoted_columns end - def quoted_columns_for_index(column_names, **options) # :doc: + def quoted_columns_for_index(column_names, **options) return [column_names] if column_names.is_a?(String) quoted_columns = Hash[column_names.map { |name| [name.to_sym, quote_column_name(name).dup] }] add_options_for_index_columns(quoted_columns, options).values end - def index_name_for_remove(table_name, options = {}) # :doc: + def index_name_for_remove(table_name, options = {}) return options[:name] if can_remove_index_by_name?(options) checks = [] @@ -1231,7 +1231,7 @@ module ActiveRecord end end - def rename_table_indexes(table_name, new_name) # :doc: + def rename_table_indexes(table_name, new_name) indexes(new_name).each do |index| generated_index_name = index_name(table_name, column: index.columns) if generated_index_name == index.name @@ -1240,7 +1240,7 @@ module ActiveRecord end end - def rename_column_indexes(table_name, column_name, new_column_name) # :doc: + def rename_column_indexes(table_name, column_name, new_column_name) column_name, new_column_name = column_name.to_s, new_column_name.to_s indexes(table_name).each do |index| next unless index.columns.include?(new_column_name) diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 1badbb576d..4046b3829d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -571,7 +571,7 @@ module ActiveRecord end end - def translate_exception_class(e, sql) # :doc: + def translate_exception_class(e, sql) begin message = "#{e.class.name}: #{e.message}: #{sql}" rescue Encoding::CompatibilityError @@ -596,7 +596,7 @@ module ActiveRecord raise translate_exception_class(e, sql) end - def translate_exception(exception, message) # :doc: + def translate_exception(exception, message) # override in derived class case exception when RuntimeError @@ -606,7 +606,7 @@ module ActiveRecord end end - def without_prepared_statement?(binds) # :doc: + def without_prepared_statement?(binds) !prepared_statements || binds.empty? end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 68a88e71ba..0717fc4526 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -701,7 +701,7 @@ module ActiveRecord end end - def extract_precision(sql_type) # :doc: + def extract_precision(sql_type) if /time/.match?(sql_type) super || 0 else @@ -709,11 +709,11 @@ module ActiveRecord end end - def fetch_type_metadata(sql_type, extra = "") # :doc: + def fetch_type_metadata(sql_type, extra = "") MySQL::TypeMetadata.new(super(sql_type), extra: extra) end - def add_index_length(quoted_columns, **options) # :doc: + def add_index_length(quoted_columns, **options) if length = options[:length] case length when Hash @@ -727,7 +727,7 @@ module ActiveRecord quoted_columns end - def add_options_for_index_columns(quoted_columns, **options) # :doc: + def add_options_for_index_columns(quoted_columns, **options) quoted_columns = add_index_length(quoted_columns, options) super end @@ -743,7 +743,7 @@ module ActiveRecord ER_CANNOT_ADD_FOREIGN = 1215 ER_CANNOT_CREATE_TABLE = 1005 - def translate_exception(exception, message) # :doc: + def translate_exception(exception, message) case error_number(exception) when ER_DUP_ENTRY RecordNotUnique.new(message) @@ -770,13 +770,13 @@ module ActiveRecord end end - def add_column_sql(table_name, column_name, type, options = {}) # :doc: + def add_column_sql(table_name, column_name, type, options = {}) td = create_table_definition(table_name) cd = td.new_column_definition(column_name, type, options) schema_creation.accept(AddColumnDefinition.new(cd)) end - def change_column_sql(table_name, column_name, type, options = {}) # :doc: + def change_column_sql(table_name, column_name, type, options = {}) column = column_for(table_name, column_name) unless options_include_default?(options) @@ -796,7 +796,7 @@ module ActiveRecord schema_creation.accept(ChangeColumnDefinition.new(cd, column.name)) end - def rename_column_sql(table_name, column_name, new_column_name) # :doc: + def rename_column_sql(table_name, column_name, new_column_name) column = column_for(table_name, column_name) options = { default: column.default, @@ -810,30 +810,30 @@ module ActiveRecord schema_creation.accept(ChangeColumnDefinition.new(cd, column.name)) end - def remove_column_sql(table_name, column_name, type = nil, options = {}) # :doc: + def remove_column_sql(table_name, column_name, type = nil, options = {}) "DROP #{quote_column_name(column_name)}" end - def remove_columns_sql(table_name, *column_names) # :doc: + def remove_columns_sql(table_name, *column_names) column_names.map { |column_name| remove_column_sql(table_name, column_name) } end - def add_index_sql(table_name, column_name, options = {}) # :doc: + def add_index_sql(table_name, column_name, options = {}) index_name, index_type, index_columns, _, index_algorithm, index_using = add_index_options(table_name, column_name, options) index_algorithm[0, 0] = ", " if index_algorithm.present? "ADD #{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns})#{index_algorithm}" end - def remove_index_sql(table_name, options = {}) # :doc: + def remove_index_sql(table_name, options = {}) index_name = index_name_for_remove(table_name, options) "DROP INDEX #{index_name}" end - def add_timestamps_sql(table_name, options = {}) # :doc: + def add_timestamps_sql(table_name, options = {}) [add_column_sql(table_name, :created_at, :datetime, options), add_column_sql(table_name, :updated_at, :datetime, options)] end - def remove_timestamps_sql(table_name, options = {}) # :doc: + def remove_timestamps_sql(table_name, options = {}) [remove_column_sql(table_name, :updated_at), remove_column_sql(table_name, :created_at)] end diff --git a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb index 1e13890eca..78e7181266 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb @@ -54,7 +54,7 @@ module ActiveRecord private - def last_inserted_id(result) # :doc: + def last_inserted_id(result) @connection.last_id end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 72947a78f5..0ebd907cc0 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -415,7 +415,7 @@ module ActiveRecord SERIALIZATION_FAILURE = "40001" DEADLOCK_DETECTED = "40P01" - def translate_exception(exception, message) # :doc: + def translate_exception(exception, message) return exception unless exception.respond_to?(:result) case exception.result.try(:error_field, PGresult::PG_DIAG_SQLSTATE) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index f2c84cd782..e761b9531a 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -518,11 +518,11 @@ module ActiveRecord SELECT #{quoted_from_columns} FROM #{quote_table_name(from)}") end - def sqlite_version # :doc: + def sqlite_version @sqlite_version ||= SQLite3Adapter::Version.new(select_value("select sqlite_version(*)")) end - def translate_exception(exception, message) # :doc: + def translate_exception(exception, message) case exception.message # SQLite 3.8.2 returns a newly formatted error message: # UNIQUE constraint failed: *table_name*.*column_name* diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index d74f15d479..270511bede 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -441,139 +441,139 @@ module ActiveRecord private - def find_with_ids(*ids) # :doc: - raise UnknownPrimaryKey.new(@klass) if primary_key.nil? + def find_with_ids(*ids) + raise UnknownPrimaryKey.new(@klass) if primary_key.nil? - expects_array = ids.first.kind_of?(Array) - return ids.first if expects_array && ids.first.empty? + expects_array = ids.first.kind_of?(Array) + return ids.first if expects_array && ids.first.empty? - ids = ids.flatten.compact.uniq + ids = ids.flatten.compact.uniq - case ids.size - when 0 - raise RecordNotFound, "Couldn't find #{@klass.name} without an ID" - when 1 - result = find_one(ids.first) - expects_array ? [ result ] : result - else - find_some(ids) + case ids.size + when 0 + raise RecordNotFound, "Couldn't find #{@klass.name} without an ID" + when 1 + result = find_one(ids.first) + expects_array ? [ result ] : result + else + find_some(ids) + end + rescue ::RangeError + raise RecordNotFound, "Couldn't find #{@klass.name} with an out of range ID" end - rescue ::RangeError - raise RecordNotFound, "Couldn't find #{@klass.name} with an out of range ID" - end - def find_one(id) # :doc: - if ActiveRecord::Base === id - id = id.id - ActiveSupport::Deprecation.warn(<<-MSG.squish) + def find_one(id) + if ActiveRecord::Base === id + id = id.id + ActiveSupport::Deprecation.warn(<<-MSG.squish) You are passing an instance of ActiveRecord::Base to `find`. Please pass the id of the object by calling `.id`. MSG - end + end - relation = where(primary_key => id) - record = relation.take + relation = where(primary_key => id) + record = relation.take - raise_record_not_found_exception!(id, 0, 1) unless record + raise_record_not_found_exception!(id, 0, 1) unless record - record - end + record + end - def find_some(ids) # :doc: - return find_some_ordered(ids) unless order_values.present? + def find_some(ids) + return find_some_ordered(ids) unless order_values.present? - result = where(primary_key => ids).to_a + result = where(primary_key => ids).to_a - expected_size = - if limit_value && ids.size > limit_value - limit_value - else - ids.size - end + expected_size = + if limit_value && ids.size > limit_value + limit_value + else + ids.size + end - # 11 ids with limit 3, offset 9 should give 2 results. - if offset_value && (ids.size - offset_value < expected_size) - expected_size = ids.size - offset_value - end + # 11 ids with limit 3, offset 9 should give 2 results. + if offset_value && (ids.size - offset_value < expected_size) + expected_size = ids.size - offset_value + end - if result.size == expected_size - result - else - raise_record_not_found_exception!(ids, result.size, expected_size) + if result.size == expected_size + result + else + raise_record_not_found_exception!(ids, result.size, expected_size) + end end - end - def find_some_ordered(ids) # :doc: - ids = ids.slice(offset_value || 0, limit_value || ids.size) || [] + def find_some_ordered(ids) + ids = ids.slice(offset_value || 0, limit_value || ids.size) || [] - result = except(:limit, :offset).where(primary_key => ids).records + result = except(:limit, :offset).where(primary_key => ids).records - if result.size == ids.size - pk_type = @klass.type_for_attribute(primary_key) + if result.size == ids.size + pk_type = @klass.type_for_attribute(primary_key) - records_by_id = result.index_by(&:id) - ids.map { |id| records_by_id.fetch(pk_type.cast(id)) } - else - raise_record_not_found_exception!(ids, result.size, ids.size) + records_by_id = result.index_by(&:id) + ids.map { |id| records_by_id.fetch(pk_type.cast(id)) } + else + raise_record_not_found_exception!(ids, result.size, ids.size) + end end - end - def find_take # :doc: - if loaded? - records.first - else - @take ||= limit(1).records.first + def find_take + if loaded? + records.first + else + @take ||= limit(1).records.first + end end - end - def find_take_with_limit(limit) # :doc: - if loaded? - records.take(limit) - else - limit(limit).to_a + def find_take_with_limit(limit) + if loaded? + records.take(limit) + else + limit(limit).to_a + end end - end - def find_nth(index) # :doc: - @offsets[offset_index + index] ||= find_nth_with_limit(index, 1).first - end + def find_nth(index) + @offsets[offset_index + index] ||= find_nth_with_limit(index, 1).first + end - def find_nth_with_limit(index, limit) # :doc: - if loaded? - records[index, limit] || [] - else - relation = if order_values.empty? && primary_key - order(arel_attribute(primary_key).asc) + def find_nth_with_limit(index, limit) + if loaded? + records[index, limit] || [] else - self + relation = if order_values.empty? && primary_key + order(arel_attribute(primary_key).asc) + else + self + end + + relation = relation.offset(offset_index + index) unless index.zero? + relation.limit(limit).to_a end - - relation = relation.offset(offset_index + index) unless index.zero? - relation.limit(limit).to_a end - end - def find_nth_from_last(index) # :doc: - if loaded? - records[-index] - else - relation = if order_values.empty? && primary_key - order(arel_attribute(primary_key).asc) + def find_nth_from_last(index) + if loaded? + records[-index] else - self + relation = if order_values.empty? && primary_key + order(arel_attribute(primary_key).asc) + else + self + end + + relation.to_a[-index] + # TODO: can be made more performant on large result sets by + # for instance, last(index)[-index] (which would require + # refactoring the last(n) finder method to make test suite pass), + # or by using a combination of reverse_order, limit, and offset, + # e.g., reverse_order.offset(index-1).first end - - relation.to_a[-index] - # TODO: can be made more performant on large result sets by - # for instance, last(index)[-index] (which would require - # refactoring the last(n) finder method to make test suite pass), - # or by using a combination of reverse_order, limit, and offset, - # e.g., reverse_order.offset(index-1).first end - end - def find_last(limit) - limit ? records.last(limit) : records.last - end + def find_last(limit) + limit ? records.last(limit) : records.last + end end end diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 119372b4bf..27cdf8cb7e 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -171,14 +171,14 @@ module ActiveRecord end end - private + private - def valid_scope_name?(name) # :doc: - if respond_to?(name, true) && logger - logger.warn "Creating scope :#{name}. " \ - "Overwriting existing method #{self.name}.#{name}." + def valid_scope_name?(name) + if respond_to?(name, true) && logger + logger.warn "Creating scope :#{name}. " \ + "Overwriting existing method #{self.name}.#{name}." + end end - end end end end diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 7cc43041fe..9633f226f0 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -70,11 +70,11 @@ module ActiveRecord private - def default_validation_context # :doc: + def default_validation_context new_record? ? :create : :update end - def raise_validation_error # :doc: + def raise_validation_error raise(RecordInvalid.new(self)) end |