Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 120 lines (99 sloc) 3.627 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
5 class GeneratedAttribute
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
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
11
b4e97ea @josevalim Tidy up migration types.
josevalim authored
12 class << self
13 def parse(column_definition)
14 name, type, has_index = column_definition.split(':')
15
16 # if user provided "name:index" instead of "name:string:index"
17 # type should be set blank so GeneratedAttribute's constructor
18 # could set it to :string
85629c8 @vijaydev fix a broken test
vijaydev authored
19 has_index, type = type, nil if INDEX_OPTIONS.include?(type)
b4e97ea @josevalim Tidy up migration types.
josevalim authored
20
21 type, attr_options = *parse_type_and_options(type)
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
22 type = type.to_sym if type
ca0af82 @joshuap Automatically create indexes for references/belongs_to statements in …
joshuap authored
23
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
24 if type && reference?(type)
25 references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { :unique => true } : true
26 attr_options[:index] = references_index
938464d @vijaydev the index option is always created if the type is one of references o…
vijaydev authored
27 end
ca0af82 @joshuap Automatically create indexes for references/belongs_to statements in …
joshuap authored
28
b4e97ea @josevalim Tidy up migration types.
josevalim authored
29 new(name, type, has_index, attr_options)
30 end
31
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
32 def reference?(type)
33 [:references, :belongs_to].include? type
34 end
35
b4e97ea @josevalim Tidy up migration types.
josevalim authored
36 private
37
38 # parse possible attribute options like :limit for string/text/binary/integer or :precision/:scale for decimals
39 # when declaring options curly brackets should be used
40 def parse_type_and_options(type)
41 case type
8a04bd0 @josevalim Fix regexp intervals.
josevalim authored
42 when /(string|text|binary|integer)\{(\d+)\}/
b4e97ea @josevalim Tidy up migration types.
josevalim authored
43 return $1, :limit => $2.to_i
7619bcf @fxn rewrites a couple of alternations in regexps as character classes
fxn authored
44 when /decimal\{(\d+)[,.-](\d+)\}/
b04bce2 @sikachu Fix failing test from [7619bcf2]
sikachu authored
45 return :decimal, :precision => $1.to_i, :scale => $2.to_i
b4e97ea @josevalim Tidy up migration types.
josevalim authored
46 else
47 return type, {}
48 end
49 end
50 end
51
52 def initialize(name, type=nil, index_type=false, attr_options={})
53 @name = name
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
54 @type = type || :string
2f632f5 @carlosantoniodasilva Extract index and uniq index options to constants
carlosantoniodasilva authored
55 @has_index = INDEX_OPTIONS.include?(index_type)
56 @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type)
b4e97ea @josevalim Tidy up migration types.
josevalim authored
57 @attr_options = attr_options
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
58 end
59
60 def field_type
61 @field_type ||= case type
4d1752b @amatsuda generate HTML5 number_field tag for integer fields
amatsuda authored
62 when :integer then :number_field
63 when :float, :decimal then :text_field
64 when :time then :time_select
65 when :datetime, :timestamp then :datetime_select
66 when :date then :date_select
67 when :text then :text_area
68 when :boolean then :check_box
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
69 else
70 :text_field
71 end
72 end
73
74 def default
75 @default ||= case type
76 when :integer then 1
77 when :float then 1.5
78 when :decimal then "9.99"
79 when :datetime, :timestamp, :time then Time.now.to_s(:db)
80 when :date then Date.today.to_s(:db)
eeb58b0 @marcandre Generate valid default fixtures for models with a type column
marcandre authored
81 when :string then name == "type" ? "" : "MyString"
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
82 when :text then "MyText"
83 when :boolean then false
2101930 @josevalim Make explicit that references and belongs_to types have nil as defaul…
josevalim authored
84 when :references, :belongs_to then nil
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
85 else
86 ""
87 end
88 end
89
80cd163 @josevalim Added scaffold views to scaffold controller.
josevalim authored
90 def human_name
91 name.to_s.humanize
92 end
93
b4e97ea @josevalim Tidy up migration types.
josevalim authored
94 def index_name
95 reference? ? "#{name}_id" : name
96 end
97
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
98 def reference?
a4c4fa3 @carlosantoniodasilva Refactor GeneratedAttributes
carlosantoniodasilva authored
99 self.class.reference?(type)
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
100 end
b4e97ea @josevalim Tidy up migration types.
josevalim authored
101
7a47f36 @german added ability to specify from cli when generating a model/migration w…
german authored
102 def has_index?
103 @has_index
104 end
105
106 def has_uniq_index?
107 @has_uniq_index
108 end
109
110 def inject_options
7c9bcbb @josevalim Use 1.9 hash syntax instead.
josevalim authored
111 "".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
112 end
113
114 def inject_index_options
7c9bcbb @josevalim Use 1.9 hash syntax instead.
josevalim authored
115 has_uniq_index? ? ", unique: true" : ""
7a47f36 @german added ability to specify from cli when generating a model/migration w…
german authored
116 end
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
117 end
118 end
119 end
Something went wrong with that request. Please try again.