Skip to content
This repository
Browse code

Minor refactoring in Active Record model schema methods

* Use each_key instead of generating intermediate keys array.
* Use each_with_object instead of inject to build hash.
* Use ternary to return instead of if + assignment.
  • Loading branch information...
commit 15e2b80a27c7fd48f1a3e22d1682281515b2c371 1 parent e7ffa77
Carlos Antonio da Silva carlosantoniodasilva authored

Showing 1 changed file with 3 additions and 5 deletions. Show diff stats Hide diff stats

  1. +3 5 activerecord/lib/active_record/model_schema.rb
8 activerecord/lib/active_record/model_schema.rb
@@ -225,7 +225,7 @@ def column_types # :nodoc:
225 225 def decorate_columns(columns_hash) # :nodoc:
226 226 return if columns_hash.empty?
227 227
228   - serialized_attributes.keys.each do |key|
  228 + serialized_attributes.each_key do |key|
229 229 columns_hash[key] = AttributeMethods::Serialization::Type.new(columns_hash[key])
230 230 end
231 231
@@ -259,13 +259,12 @@ def content_columns
259 259 # and true as the value. This makes it possible to do O(1) lookups in respond_to? to check if a given method for attribute
260 260 # is available.
261 261 def column_methods_hash #:nodoc:
262   - @dynamic_methods_hash ||= column_names.inject(Hash.new(false)) do |methods, attr|
  262 + @dynamic_methods_hash ||= column_names.each_with_object(Hash.new(false)) do |attr, methods|
263 263 attr_name = attr.to_s
264 264 methods[attr.to_sym] = attr_name
265 265 methods["#{attr}=".to_sym] = attr_name
266 266 methods["#{attr}?".to_sym] = attr_name
267 267 methods["#{attr}_before_type_cast".to_sym] = attr_name
268   - methods
269 268 end
270 269 end
271 270
@@ -324,8 +323,7 @@ def initialize_attributes(attributes, options = {}) #:nodoc:
324 323 # Guesses the table name, but does not decorate it with prefix and suffix information.
325 324 def undecorated_table_name(class_name = base_class.name)
326 325 table_name = class_name.to_s.demodulize.underscore
327   - table_name = table_name.pluralize if pluralize_table_names
328   - table_name
  326 + pluralize_table_names ? table_name.pluralize : table_name
329 327 end
330 328
331 329 # Computes and returns a table name according to default conventions.

0 comments on commit 15e2b80

Please sign in to comment.
Something went wrong with that request. Please try again.