Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 54 lines (47 sloc) 1.706 kb
d93b45e Jeff Kreeftmeijer Rails::Generators::GeneratedAttribute: tests, cleanups and a bugfix [#46...
jeffkreeftmeijer authored
1 require 'active_support/time'
a9f3c9d Prem Sichanugrist Using Object#in? and Object#either? in various places
sikachu authored
2 require 'active_support/core_ext/object/inclusion'
d93b45e Jeff Kreeftmeijer Rails::Generators::GeneratedAttribute: tests, cleanups and a bugfix [#46...
jeffkreeftmeijer authored
3
75fbd73 José Valim Add GeneratedAttribute.
josevalim authored
4 module Rails
5 module Generators
6 class GeneratedAttribute
8c5622f José Valim Initial scratch of model generators.
josevalim authored
7 attr_accessor :name, :type
75fbd73 José Valim Add GeneratedAttribute.
josevalim authored
8
9 def initialize(name, type)
d2fc5e2 Rohit Arondekar Output a friendly message when no type given for GeneratedAttribute [#54...
rohit authored
10 raise Thor::Error, "Missing type for attribute '#{name}'.\nExample: '#{name}:string' where string is the type." if type.blank?
75fbd73 José Valim Add GeneratedAttribute.
josevalim authored
11 @name, @type = name, type.to_sym
12 end
13
14 def field_type
15 @field_type ||= case type
4d1752b Akira Matsuda 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 José Valim 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)
f4858e4 Marc-André Lafortune Generate valid default fixtures for models with a type column
marcandre authored
35 when :string then name == "type" ? "" : "MyString"
75fbd73 José Valim Add GeneratedAttribute.
josevalim authored
36 when :text then "MyText"
37 when :boolean then false
2101930 José Valim Make explicit that references and belongs_to types have nil as default v...
josevalim authored
38 when :references, :belongs_to then nil
75fbd73 José Valim Add GeneratedAttribute.
josevalim authored
39 else
40 ""
41 end
42 end
43
80cd163 José Valim Added scaffold views to scaffold controller.
josevalim authored
44 def human_name
45 name.to_s.humanize
46 end
47
75fbd73 José Valim Add GeneratedAttribute.
josevalim authored
48 def reference?
733bfa6 Prem Sichanugrist Remove `#among?` from Active Support
sikachu authored
49 self.type.in?([:references, :belongs_to])
75fbd73 José Valim Add GeneratedAttribute.
josevalim authored
50 end
51 end
52 end
53 end
Something went wrong with that request. Please try again.