Skip to content
Newer
Older
100644 143 lines (118 sloc) 4.09 KB
d93b45e @jeffkreeftmeijer Rails::Generators::GeneratedAttribute: tests, cleanups and a bugfix [#…
jeffkreeftmeijer authored
1 require 'active_support/time'
2
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
3 module Rails
4 module Generators
521e2ee update Rails::Generators docs [ci skip]
Francesco Rodriguez authored
5 class GeneratedAttribute # :nodoc:
2f632f5 @carlosantoniodasilva Extract index and uniq index options to constants
carlosantoniodasilva authored
6 INDEX_OPTIONS = %w(index uniq)
7 UNIQ_INDEX_OPTIONS = %w(uniq)
8
b4e97ea @josevalim Tidy up migration types.
josevalim authored
9 attr_accessor :name, :type
10 attr_reader :attr_options
211d88b @lexmag Add join table migration generator
lexmag authored
11 attr_writer :index_name
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
12
b4e97ea @josevalim Tidy up migration types.
josevalim authored
13 class << self
14 def parse(column_definition)
15 name, type, has_index = column_definition.split(':')
16
17 # if user provided "name:index" instead of "name:string:index"
18 # type should be set blank so GeneratedAttribute's constructor
19 # could set it to :string
85629c8 @vijaydev fix a broken test
vijaydev authored
20 has_index, type = type, nil if INDEX_OPTIONS.include?(type)
b4e97ea @josevalim Tidy up migration types.
josevalim authored
21
22 type, attr_options = *parse_type_and_options(type)
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
23 type = type.to_sym if type
ca0af82 @joshuap Automatically create indexes for references/belongs_to statements in …
joshuap authored
24
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
25 if type && reference?(type)
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
26 references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { unique: true } : true
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
27 attr_options[:index] = references_index
938464d @vijaydev the index option is always created if the type is one of references o…
vijaydev authored
28 end
ca0af82 @joshuap Automatically create indexes for references/belongs_to statements in …
joshuap authored
29
b4e97ea @josevalim Tidy up migration types.
josevalim authored
30 new(name, type, has_index, attr_options)
31 end
32
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
33 def reference?(type)
34 [:references, :belongs_to].include? type
35 end
36
b4e97ea @josevalim Tidy up migration types.
josevalim authored
37 private
38
94b230e @lexmag Add polymorphic option to model generator
lexmag authored
39 # parse possible attribute options like :limit for string/text/binary/integer, :precision/:scale for decimals or :polymorphic for references/belongs_to
b4e97ea @josevalim Tidy up migration types.
josevalim authored
40 # when declaring options curly brackets should be used
41 def parse_type_and_options(type)
42 case type
8a04bd0 @josevalim Fix regexp intervals.
josevalim authored
43 when /(string|text|binary|integer)\{(\d+)\}/
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
44 return $1, limit: $2.to_i
7619bcf @fxn rewrites a couple of alternations in regexps as character classes
fxn authored
45 when /decimal\{(\d+)[,.-](\d+)\}/
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
46 return :decimal, precision: $1.to_i, scale: $2.to_i
94b230e @lexmag Add polymorphic option to model generator
lexmag authored
47 when /(references|belongs_to)\{polymorphic\}/
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
48 return $1, polymorphic: true
b4e97ea @josevalim Tidy up migration types.
josevalim authored
49 else
50 return type, {}
51 end
52 end
53 end
54
55 def initialize(name, type=nil, index_type=false, attr_options={})
56 @name = name
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
57 @type = type || :string
2f632f5 @carlosantoniodasilva Extract index and uniq index options to constants
carlosantoniodasilva authored
58 @has_index = INDEX_OPTIONS.include?(index_type)
59 @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type)
b4e97ea @josevalim Tidy up migration types.
josevalim authored
60 @attr_options = attr_options
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
61 end
62
63 def field_type
64 @field_type ||= case type
4d1752b @amatsuda generate HTML5 number_field tag for integer fields
amatsuda authored
65 when :integer then :number_field
66 when :float, :decimal then :text_field
67 when :time then :time_select
68 when :datetime, :timestamp then :datetime_select
69 when :date then :date_select
70 when :text then :text_area
71 when :boolean then :check_box
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
72 else
73 :text_field
74 end
75 end
76
77 def default
78 @default ||= case type
79 when :integer then 1
80 when :float then 1.5
81 when :decimal then "9.99"
82 when :datetime, :timestamp, :time then Time.now.to_s(:db)
83 when :date then Date.today.to_s(:db)
eeb58b0 @marcandre Generate valid default fixtures for models with a type column
marcandre authored
84 when :string then name == "type" ? "" : "MyString"
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
85 when :text then "MyText"
86 when :boolean then false
2101930 @josevalim Make explicit that references and belongs_to types have nil as defaul…
josevalim authored
87 when :references, :belongs_to then nil
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
88 else
89 ""
90 end
91 end
92
5a7d319 @lexmag Add fkey attributes to `join_table` migration generator
lexmag authored
93 def plural_name
94 name.sub(/_id$/, '').pluralize
95 end
96
80cd163 @josevalim Added scaffold views to scaffold controller.
josevalim authored
97 def human_name
5a7d319 @lexmag Add fkey attributes to `join_table` migration generator
lexmag authored
98 name.humanize
80cd163 @josevalim Added scaffold views to scaffold controller.
josevalim authored
99 end
100
b4e97ea @josevalim Tidy up migration types.
josevalim authored
101 def index_name
95ccbd8 @rafaelfranca Add GeneratedAttribute#column_name to get the name of the column in the
rafaelfranca authored
102 @index_name ||= if polymorphic?
103 %w(id type).map { |t| "#{name}_#{t}" }
94b230e @lexmag Add polymorphic option to model generator
lexmag authored
104 else
95ccbd8 @rafaelfranca Add GeneratedAttribute#column_name to get the name of the column in the
rafaelfranca authored
105 column_name
94b230e @lexmag Add polymorphic option to model generator
lexmag authored
106 end
b4e97ea @josevalim Tidy up migration types.
josevalim authored
107 end
108
95ccbd8 @rafaelfranca Add GeneratedAttribute#column_name to get the name of the column in the
rafaelfranca authored
109 def column_name
110 @column_name ||= reference? ? "#{name}_id" : name
111 end
112
5a7d319 @lexmag Add fkey attributes to `join_table` migration generator
lexmag authored
113 def foreign_key?
114 !!(name =~ /_id$/)
115 end
116
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
117 def reference?
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
118 self.class.reference?(type)
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
119 end
b4e97ea @josevalim Tidy up migration types.
josevalim authored
120
94b230e @lexmag Add polymorphic option to model generator
lexmag authored
121 def polymorphic?
122 self.attr_options.has_key?(:polymorphic)
123 end
124
7a47f36 @german added ability to specify from cli when generating a model/migration w…
german authored
125 def has_index?
126 @has_index
127 end
128
129 def has_uniq_index?
130 @has_uniq_index
131 end
132
133 def inject_options
7c9bcbb @josevalim Use 1.9 hash syntax instead.
josevalim authored
134 "".tap { |s| @attr_options.each { |k,v| s << ", #{k}: #{v.inspect}" } }
7a47f36 @german added ability to specify from cli when generating a model/migration w…
german authored
135 end
136
137 def inject_index_options
7c9bcbb @josevalim Use 1.9 hash syntax instead.
josevalim authored
138 has_uniq_index? ? ", unique: true" : ""
7a47f36 @german added ability to specify from cli when generating a model/migration w…
german authored
139 end
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
140 end
141 end
142 end
Something went wrong with that request. Please try again.