Skip to content
This repository
Browse code

Merge pull request #5930 from carlosantoniodasilva/generated-attribut…

…e-refactor

Refactor GeneratedAttributes
  • Loading branch information...
commit 5b8066715823220ebfe6773b8ac77dcd6d7954f6 2 parents 9026980 + a4c4fa3
José Valim josevalim authored

Showing 1 changed file with 10 additions and 7 deletions. Show diff stats Hide diff stats

  1. +10 7 railties/lib/rails/generators/generated_attribute.rb
17 railties/lib/rails/generators/generated_attribute.rb
... ... @@ -1,6 +1,4 @@
1 1 require 'active_support/time'
2   -require 'active_support/core_ext/object/inclusion'
3   -require 'active_support/core_ext/object/blank'
4 2
5 3 module Rails
6 4 module Generators
@@ -21,15 +19,20 @@ def parse(column_definition)
21 19 has_index, type = type, nil if INDEX_OPTIONS.include?(type)
22 20
23 21 type, attr_options = *parse_type_and_options(type)
  22 + type = type.to_sym if type
24 23
25   - if type.in?(%w(references belongs_to))
26   - references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? {:unique => true} : true
27   - attr_options.merge!({:index => references_index})
  24 + if type && reference?(type)
  25 + references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { :unique => true } : true
  26 + attr_options[:index] = references_index
28 27 end
29 28
30 29 new(name, type, has_index, attr_options)
31 30 end
32 31
  32 + def reference?(type)
  33 + [:references, :belongs_to].include? type
  34 + end
  35 +
33 36 private
34 37
35 38 # parse possible attribute options like :limit for string/text/binary/integer or :precision/:scale for decimals
@@ -48,7 +51,7 @@ def parse_type_and_options(type)
48 51
49 52 def initialize(name, type=nil, index_type=false, attr_options={})
50 53 @name = name
51   - @type = (type.presence || :string).to_sym
  54 + @type = type || :string
52 55 @has_index = INDEX_OPTIONS.include?(index_type)
53 56 @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type)
54 57 @attr_options = attr_options
@@ -93,7 +96,7 @@ def index_name
93 96 end
94 97
95 98 def reference?
96   - self.type.in?(:references, :belongs_to)
  99 + self.class.reference?(type)
97 100 end
98 101
99 102 def has_index?

0 comments on commit 5b80667

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