Class: Rails::Generators::GeneratedAttribute

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

Overview

:nodoc:

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GeneratedAttribute.



67
68
69
70
71
72
73
# File 'railties/lib/rails/generators/generated_attribute.rb', line 67

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

#attr_optionsObject (readonly)

Returns the value of attribute attr_options



13
14
15
# File 'railties/lib/rails/generators/generated_attribute.rb', line 13

def attr_options
  @attr_options
end

#index_nameObject



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

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

#nameObject

Returns the value of attribute name



12
13
14
# File 'railties/lib/rails/generators/generated_attribute.rb', line 12

def name
  @name
end

#typeObject

Returns the value of attribute type



12
13
14
# File 'railties/lib/rails/generators/generated_attribute.rb', line 12

def type
  @type
end

Class Method Details

.parse(column_definition) ⇒ Object



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

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)
    if UNIQ_INDEX_OPTIONS.include?(has_index)
      attr_options[:index] = { unique: true }
    end
  end

  new(name, type, has_index, attr_options)
end

.reference?(type) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'railties/lib/rails/generators/generated_attribute.rb', line 37

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

Instance Method Details

#attachment?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'railties/lib/rails/generators/generated_attribute.rb', line 169

def attachment?
  type == :attachment
end

#attachments?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'railties/lib/rails/generators/generated_attribute.rb', line 173

def attachments?
  type == :attachments
end

#column_nameObject



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

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

#defaultObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'railties/lib/rails/generators/generated_attribute.rb', line 91

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,
                    :attachment, :attachments,
                    :rich_text                   then nil
               else
                 ""
  end
end

#field_typeObject



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

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 :rich_text                then :rich_text_area
                  when :boolean                  then :check_box
                  when :attachment, :attachments then :file_field
                  else
                    :text_field
  end
end

#foreign_key?Boolean

Returns:

  • (Boolean)


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

def foreign_key?
  /_id$/.match?(name)
end

#has_index?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'railties/lib/rails/generators/generated_attribute.rb', line 149

def has_index?
  @has_index
end

#has_uniq_index?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'railties/lib/rails/generators/generated_attribute.rb', line 153

def has_uniq_index?
  @has_uniq_index
end

#human_nameObject



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

def human_name
  name.humanize
end

#inject_index_optionsObject



185
186
187
# File 'railties/lib/rails/generators/generated_attribute.rb', line 185

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

#inject_optionsObject



181
182
183
# File 'railties/lib/rails/generators/generated_attribute.rb', line 181

def inject_options
  (+"").tap { |s| options_for_migration.each { |k, v| s << ", #{k}: #{v.inspect}" } }
end

#options_for_migrationObject



189
190
191
192
193
194
195
196
197
198
199
# File 'railties/lib/rails/generators/generated_attribute.rb', line 189

def options_for_migration
  @attr_options.dup.tap do |options|
    if required?
      options[:null] = false
    end

    if reference? && !polymorphic?
      options[:foreign_key] = true
    end
  end
end

#password_digest?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'railties/lib/rails/generators/generated_attribute.rb', line 157

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

#plural_nameObject



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

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

#polymorphic?Boolean

Returns:

  • (Boolean)


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

def polymorphic?
  attr_options[:polymorphic]
end

#reference?Boolean

Returns:

  • (Boolean)


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

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

#required?Boolean

Returns:

  • (Boolean)


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

def required?
  reference? && Rails.application.config.active_record.belongs_to_required_by_default
end

#rich_text?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'railties/lib/rails/generators/generated_attribute.rb', line 165

def rich_text?
  type == :rich_text
end

#singular_nameObject



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

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

#token?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'railties/lib/rails/generators/generated_attribute.rb', line 161

def token?
  type == :token
end

#virtual?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'railties/lib/rails/generators/generated_attribute.rb', line 177

def virtual?
  rich_text? || attachment? || attachments?
end