mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #5930 from carlosantoniodasilva/generated-attribute-refactor
Refactor GeneratedAttributes
This commit is contained in:
commit
5b80667158
1 changed files with 10 additions and 7 deletions
|
@ -1,6 +1,4 @@
|
||||||
require 'active_support/time'
|
require 'active_support/time'
|
||||||
require 'active_support/core_ext/object/inclusion'
|
|
||||||
require 'active_support/core_ext/object/blank'
|
|
||||||
|
|
||||||
module Rails
|
module Rails
|
||||||
module Generators
|
module Generators
|
||||||
|
@ -21,15 +19,20 @@ module Rails
|
||||||
has_index, type = type, nil if INDEX_OPTIONS.include?(type)
|
has_index, type = type, nil if INDEX_OPTIONS.include?(type)
|
||||||
|
|
||||||
type, attr_options = *parse_type_and_options(type)
|
type, attr_options = *parse_type_and_options(type)
|
||||||
|
type = type.to_sym if type
|
||||||
|
|
||||||
if type.in?(%w(references belongs_to))
|
if type && reference?(type)
|
||||||
references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? {:unique => true} : true
|
references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { :unique => true } : true
|
||||||
attr_options.merge!({:index => references_index})
|
attr_options[:index] = references_index
|
||||||
end
|
end
|
||||||
|
|
||||||
new(name, type, has_index, attr_options)
|
new(name, type, has_index, attr_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reference?(type)
|
||||||
|
[:references, :belongs_to].include? type
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# parse possible attribute options like :limit for string/text/binary/integer or :precision/:scale for decimals
|
# 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={})
|
def initialize(name, type=nil, index_type=false, attr_options={})
|
||||||
@name = name
|
@name = name
|
||||||
@type = (type.presence || :string).to_sym
|
@type = type || :string
|
||||||
@has_index = INDEX_OPTIONS.include?(index_type)
|
@has_index = INDEX_OPTIONS.include?(index_type)
|
||||||
@has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type)
|
@has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type)
|
||||||
@attr_options = attr_options
|
@attr_options = attr_options
|
||||||
|
@ -93,7 +96,7 @@ module Rails
|
||||||
end
|
end
|
||||||
|
|
||||||
def reference?
|
def reference?
|
||||||
self.type.in?(:references, :belongs_to)
|
self.class.reference?(type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_index?
|
def has_index?
|
||||||
|
|
Loading…
Reference in a new issue