Skip to content

Commit

Permalink
Merge pull request #5930 from carlosantoniodasilva/generated-attribut…
Browse files Browse the repository at this point in the history
…e-refactor

Refactor GeneratedAttributes
  • Loading branch information
josevalim committed Apr 23, 2012
2 parents 9026980 + a4c4fa3 commit 5b80667
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions 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
Expand All @@ -21,15 +19,20 @@ def parse(column_definition)
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
Expand All @@ -48,7 +51,7 @@ def parse_type_and_options(type)

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
Expand Down Expand Up @@ -93,7 +96,7 @@ def index_name
end

def reference?
self.type.in?(:references, :belongs_to)
self.class.reference?(type)
end

def has_index?
Expand Down

0 comments on commit 5b80667

Please sign in to comment.