Class: Rails::Generators::GeneratedAttribute

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

Overview

:nodoc:

Constant Summary

INDEX_OPTIONS =
%w(index uniq)
UNIQ_INDEX_OPTIONS =
%w(uniq)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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



55
56
57
58
59
60
61
# File 'railties/lib/rails/generators/generated_attribute.rb', line 55

def initialize(name, type=nil, index_type=false, attr_options={})
  @name           = name
  @type           = type || :string
  @has_index      = INDEX_OPTIONS.include?(index_type)
  @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type)
  @attr_options   = attr_options
end

Instance Attribute Details

- (Object) attr_options (readonly)

Returns the value of attribute attr_options



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

def attr_options
  @attr_options
end

- (Object) index_name



105
106
107
108
109
110
111
# File 'railties/lib/rails/generators/generated_attribute.rb', line 105

def index_name
  @index_name ||= if polymorphic?
    %w(id type).map { |t| "#{name}_#{t}" }
  else
    column_name
  end
end

- (Object) name

Returns the value of attribute name



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

def name
  @name
end

- (Object) type

Returns the value of attribute type



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

def type
  @type
end

Class Method Details

+ (Object) parse(column_definition)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'railties/lib/rails/generators/generated_attribute.rb', line 14

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 INDEX_OPTIONS.include?(type)

  type, attr_options = *parse_type_and_options(type)
  type = type.to_sym if type

  if type && reference?(type)
    references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { unique: true } : true
    attr_options[:index] = references_index
  end

  new(name, type, has_index, attr_options)
end

+ (Boolean) reference?(type)



33
34
35
# File 'railties/lib/rails/generators/generated_attribute.rb', line 33

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

Instance Method Details

- (Object) column_name



113
114
115
# File 'railties/lib/rails/generators/generated_attribute.rb', line 113

def column_name
  @column_name ||= reference? ? "#{name}_id" : name
end

- (Object) default



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'railties/lib/rails/generators/generated_attribute.rb', line 77

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

- (Object) field_type



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

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

- (Boolean) foreign_key?



117
118
119
# File 'railties/lib/rails/generators/generated_attribute.rb', line 117

def foreign_key?
  !!(name =~ /_id$/)
end

- (Boolean) has_index?



129
130
131
# File 'railties/lib/rails/generators/generated_attribute.rb', line 129

def has_index?
  @has_index
end

- (Boolean) has_uniq_index?



133
134
135
# File 'railties/lib/rails/generators/generated_attribute.rb', line 133

def has_uniq_index?
  @has_uniq_index
end

- (Object) human_name



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

def human_name
  name.humanize
end

- (Object) inject_index_options



145
146
147
# File 'railties/lib/rails/generators/generated_attribute.rb', line 145

def inject_index_options
  has_uniq_index? ? ", unique: true" : ""
end

- (Object) inject_options



141
142
143
# File 'railties/lib/rails/generators/generated_attribute.rb', line 141

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

- (Boolean) password_digest?



137
138
139
# File 'railties/lib/rails/generators/generated_attribute.rb', line 137

def password_digest?
  name == 'password' && type == :digest 
end

- (Object) plural_name



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

def plural_name
  name.sub(/_id$/, '').pluralize
end

- (Boolean) polymorphic?



125
126
127
# File 'railties/lib/rails/generators/generated_attribute.rb', line 125

def polymorphic?
  self.attr_options.has_key?(:polymorphic)
end

- (Boolean) reference?



121
122
123
# File 'railties/lib/rails/generators/generated_attribute.rb', line 121

def reference?
  self.class.reference?(type)
end

- (Object) singular_name



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

def singular_name
  name.sub(/_id$/, '').singularize
end