Class: Rails::Generators::GeneratedAttribute

Inherits:
Object
  • Object
show all
Defined in:
railties/lib/rails/generators/generated_attribute.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ GeneratedAttribute


40
41
42
43
44
45
46
# File 'railties/lib/rails/generators/generated_attribute.rb', line 40

def initialize(name, type=nil, index_type=false, attr_options={})
  @name           = name
  @type           = (type.presence || :string).to_sym
  @has_index      = %w(index uniq).include?(index_type)
  @has_uniq_index = %w(uniq).include?(index_type)
  @attr_options   = attr_options
end

Instance Attribute Details

#attr_optionsObject (readonly)

Returns the value of attribute attr_options


9
10
11
# File 'railties/lib/rails/generators/generated_attribute.rb', line 9

def attr_options
  @attr_options
end

#nameObject

Returns the value of attribute name


8
9
10
# File 'railties/lib/rails/generators/generated_attribute.rb', line 8

def name
  @name
end

#typeObject

Returns the value of attribute type


8
9
10
# File 'railties/lib/rails/generators/generated_attribute.rb', line 8

def type
  @type
end

Class Method Details

.parse(column_definition) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
# File 'railties/lib/rails/generators/generated_attribute.rb', line 12

def parse(column_definition)
  name, type, has_index = column_definition.split(':')

  # if user provided "name:index" instead of "name:string:index"
  # type should be set blank so GeneratedAttribute's constructor
  # could set it to :string
  has_index, type = type, nil if %w(index uniq).include?(type)

  type, attr_options = *parse_type_and_options(type)
  new(name, type, has_index, attr_options)
end

Instance Method Details

#defaultObject


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'railties/lib/rails/generators/generated_attribute.rb', line 62

def default
  @default ||= case type
    when :integer                     then 1
    when :float                       then 1.5
    when :decimal                     then "9.99"
    when :datetime, :timestamp, :time then Time.now.to_s(:db)
    when :date                        then Date.today.to_s(:db)
    when :string                      then name == "type" ? "" : "MyString"
    when :text                        then "MyText"
    when :boolean                     then false
    when :references, :belongs_to     then nil
    else
      ""
  end
end

#field_typeObject


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'railties/lib/rails/generators/generated_attribute.rb', line 48

def field_type
  @field_type ||= case type
    when :integer              then :number_field
    when :float, :decimal      then :text_field
    when :time                 then :time_select
    when :datetime, :timestamp then :datetime_select
    when :date                 then :date_select
    when :text                 then :text_area
    when :boolean              then :check_box
    else
      :text_field
  end
end

#has_index?Boolean


90
91
92
# File 'railties/lib/rails/generators/generated_attribute.rb', line 90

def has_index?
  @has_index
end

#has_uniq_index?Boolean


94
95
96
# File 'railties/lib/rails/generators/generated_attribute.rb', line 94

def has_uniq_index?
  @has_uniq_index
end

#human_nameObject


78
79
80
# File 'railties/lib/rails/generators/generated_attribute.rb', line 78

def human_name
  name.to_s.humanize
end

#index_nameObject


82
83
84
# File 'railties/lib/rails/generators/generated_attribute.rb', line 82

def index_name
  reference? ? "#{name}_id" : name
end

#inject_index_optionsObject


102
103
104
# File 'railties/lib/rails/generators/generated_attribute.rb', line 102

def inject_index_options
  has_uniq_index? ? ", :unique => true" : ''
end

#inject_optionsObject


98
99
100
# File 'railties/lib/rails/generators/generated_attribute.rb', line 98

def inject_options
  "".tap { |s| @attr_options.each { |k,v| s << ", :#{k} => #{v.inspect}" } }
end

#reference?Boolean


86
87
88
# File 'railties/lib/rails/generators/generated_attribute.rb', line 86

def reference?
  self.type.in?([:references, :belongs_to])
end