aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-04-22 21:53:18 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-04-22 21:53:18 -0300
commita4c4fa307fa0fe88d63d5442a86564dab2b684ae (patch)
tree407ffd1af1326c7c92e99157f0a407d121b5fa54
parent90269802dbf690fbe8be0cd01e3b387a51f02b70 (diff)
downloadrails-a4c4fa307fa0fe88d63d5442a86564dab2b684ae.tar.gz
rails-a4c4fa307fa0fe88d63d5442a86564dab2b684ae.tar.bz2
rails-a4c4fa307fa0fe88d63d5442a86564dab2b684ae.zip
Refactor GeneratedAttributes
* Move reference? method to class to remove duplicated code * Move to_sym typecast from #initialize to .parse method (make it easier to refactor reference?), remove AS object/blank require * Use []= instead of merge!({}) * Remove in? in favor of include?, remove AS object/inclusion require
-rw-r--r--railties/lib/rails/generators/generated_attribute.rb17
1 files changed, 10 insertions, 7 deletions
diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb
index 50e7aa85ac..25d0161e4c 100644
--- a/railties/lib/rails/generators/generated_attribute.rb
+++ b/railties/lib/rails/generators/generated_attribute.rb
@@ -1,6 +1,4 @@
require 'active_support/time'
-require 'active_support/core_ext/object/inclusion'
-require 'active_support/core_ext/object/blank'
module Rails
module Generators
@@ -21,15 +19,20 @@ module Rails
has_index, type = type, nil if INDEX_OPTIONS.include?(type)
type, attr_options = *parse_type_and_options(type)
+ type = type.to_sym if type
- if type.in?(%w(references belongs_to))
- references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? {:unique => true} : true
- attr_options.merge!({:index => references_index})
+ if type && reference?(type)
+ references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { :unique => true } : true
+ attr_options[:index] = references_index
end
new(name, type, has_index, attr_options)
end
+ def reference?(type)
+ [:references, :belongs_to].include? type
+ end
+
private
# parse possible attribute options like :limit for string/text/binary/integer or :precision/:scale for decimals
@@ -48,7 +51,7 @@ module Rails
def initialize(name, type=nil, index_type=false, attr_options={})
@name = name
- @type = (type.presence || :string).to_sym
+ @type = type || :string
@has_index = INDEX_OPTIONS.include?(index_type)
@has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type)
@attr_options = attr_options
@@ -93,7 +96,7 @@ module Rails
end
def reference?
- self.type.in?(:references, :belongs_to)
+ self.class.reference?(type)
end
def has_index?