Class: Rails::Generators::GeneratedAttribute

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

Overview

:nodoc:

Constant Summary collapse

INDEX_OPTIONS =
%w(index uniq)
UNIQ_INDEX_OPTIONS =
%w(uniq)
DEFAULT_TYPES =
%w(
  attachment
  attachments
  belongs_to
  boolean
  date
  datetime
  decimal
  digest
  float
  integer
  references
  rich_text
  string
  text
  time
  timestamp
  token
)

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.



109
110
111
112
113
114
115
# File 'lib/rails/generators/generated_attribute.rb', line 109

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.



31
32
33
# File 'lib/rails/generators/generated_attribute.rb', line 31

def attr_options
  @attr_options
end

#index_nameObject



163
164
165
166
167
168
169
# File 'lib/rails/generators/generated_attribute.rb', line 163

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.



30
31
32
# File 'lib/rails/generators/generated_attribute.rb', line 30

def name
  @name
end

#typeObject

Returns the value of attribute type.



30
31
32
# File 'lib/rails/generators/generated_attribute.rb', line 30

def type
  @type
end

Class Method Details

.dangerous_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def dangerous_name?(name)
  defined?(ActiveRecord::Base) &&
    ActiveRecord::Base.dangerous_attribute_method?(name)
end

.parse(column_definition) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rails/generators/generated_attribute.rb', line 35

def parse(column_definition)
  name, type, index_type = 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
  index_type, type = type, nil if valid_index_type?(type)

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

  if dangerous_name?(name)
    raise Error, "Could not generate field '#{name}', as it is already defined by Active Record."
  end

  if type && !valid_type?(type)
    raise Error, "Could not generate field '#{name}' with unknown type '#{type}'."
  end

  if index_type && !valid_index_type?(index_type)
    raise Error, "Could not generate field '#{name}' with unknown index '#{index_type}'."
  end

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

  new(name, type, index_type, attr_options)
end

.reference?(type) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.valid_index_type?(index_type) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid_index_type?(index_type)
  INDEX_OPTIONS.include?(index_type.to_s)
end

.valid_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/rails/generators/generated_attribute.rb', line 72

def valid_type?(type)
  DEFAULT_TYPES.include?(type.to_s) ||
    !defined?(ActiveRecord::Base) ||
    ActiveRecord::Base.lease_connection.valid_type?(type)
end

Instance Method Details

#attachment?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/rails/generators/generated_attribute.rb', line 211

def attachment?
  type == :attachment
end

#attachments?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/rails/generators/generated_attribute.rb', line 215

def attachments?
  type == :attachments
end

#column_nameObject



171
172
173
# File 'lib/rails/generators/generated_attribute.rb', line 171

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

#defaultObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rails/generators/generated_attribute.rb', line 133

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_fs(:db)
               when :date                        then Date.today.to_fs(: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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rails/generators/generated_attribute.rb', line 117

def field_type
  @field_type ||= case type
                  when :integer                  then :number_field
                  when :float, :decimal          then :text_field
                  when :time                     then :time_field
                  when :datetime, :timestamp     then :datetime_field
                  when :date                     then :date_field
                  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)


175
176
177
# File 'lib/rails/generators/generated_attribute.rb', line 175

def foreign_key?
  name.end_with?("_id")
end

#has_index?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/rails/generators/generated_attribute.rb', line 191

def has_index?
  @has_index
end

#has_uniq_index?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/rails/generators/generated_attribute.rb', line 195

def has_uniq_index?
  @has_uniq_index
end

#human_nameObject



159
160
161
# File 'lib/rails/generators/generated_attribute.rb', line 159

def human_name
  name.humanize
end

#inject_index_optionsObject



227
228
229
# File 'lib/rails/generators/generated_attribute.rb', line 227

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

#inject_optionsObject



223
224
225
# File 'lib/rails/generators/generated_attribute.rb', line 223

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

#options_for_migrationObject



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rails/generators/generated_attribute.rb', line 231

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)


199
200
201
# File 'lib/rails/generators/generated_attribute.rb', line 199

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

#plural_nameObject



151
152
153
# File 'lib/rails/generators/generated_attribute.rb', line 151

def plural_name
  name.delete_suffix("_id").pluralize
end

#polymorphic?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/rails/generators/generated_attribute.rb', line 183

def polymorphic?
  attr_options[:polymorphic]
end

#reference?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/rails/generators/generated_attribute.rb', line 179

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

#required?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/rails/generators/generated_attribute.rb', line 187

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

#rich_text?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/rails/generators/generated_attribute.rb', line 207

def rich_text?
  type == :rich_text
end

#singular_nameObject



155
156
157
# File 'lib/rails/generators/generated_attribute.rb', line 155

def singular_name
  name.delete_suffix("_id").singularize
end

#to_sObject



243
244
245
246
247
248
249
250
251
# File 'lib/rails/generators/generated_attribute.rb', line 243

def to_s
  if has_uniq_index?
    "#{name}:#{type}#{print_attribute_options}:uniq"
  elsif has_index?
    "#{name}:#{type}#{print_attribute_options}:index"
  else
    "#{name}:#{type}#{print_attribute_options}"
  end
end

#token?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/rails/generators/generated_attribute.rb', line 203

def token?
  type == :token
end

#virtual?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/rails/generators/generated_attribute.rb', line 219

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