Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 54 lines (47 sloc) 1.684 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'
d93b45e @jeffkreeftmeijer Rails::Generators::GeneratedAttribute: tests, cleanups and a bugfix […
jeffkreeftmeijer authored
3
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
4 module Rails
5 module Generators
6 class GeneratedAttribute
8c5622f @josevalim Initial scratch of model generators.
josevalim authored
7 attr_accessor :name, :type
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
8
9 def initialize(name, type)
d2fc5e2 @rohit Output a friendly message when no type given for GeneratedAttribute […
rohit authored
10 raise Thor::Error, "Missing type for attribute '#{name}'.\nExample: '#{name}:string' where string is the type." if type.blank?
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
11 @name, @type = name, type.to_sym
12 end
13
14 def field_type
15 @field_type ||= case type
4d1752b @amatsuda generate HTML5 number_field tag for integer fields
amatsuda authored
16 when :integer then :number_field
17 when :float, :decimal then :text_field
18 when :time then :time_select
19 when :datetime, :timestamp then :datetime_select
20 when :date then :date_select
21 when :text then :text_area
22 when :boolean then :check_box
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
23 else
24 :text_field
25 end
26 end
27
28 def default
29 @default ||= case type
30 when :integer then 1
31 when :float then 1.5
32 when :decimal then "9.99"
33 when :datetime, :timestamp, :time then Time.now.to_s(:db)
34 when :date then Date.today.to_s(:db)
35 when :string then "MyString"
36 when :text then "MyText"
37 when :boolean then false
2101930 @josevalim Make explicit that references and belongs_to types have nil as defaul…
josevalim authored
38 when :references, :belongs_to then nil
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
39 else
40 ""
41 end
42 end
43
80cd163 @josevalim Added scaffold views to scaffold controller.
josevalim authored
44 def human_name
45 name.to_s.humanize
46 end
47
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
48 def reference?
733bfa6 @sikachu Remove `#among?` from Active Support
sikachu authored
49 self.type.in?([:references, :belongs_to])
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
50 end
51 end
52 end
53 end
Something went wrong with that request. Please try again.