Skip to content

HTTPS clone URL

Subversion checkout URL

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