Class: Rails::Generators::GeneratedAttribute
- Inherits:
-
Object
- Object
- Rails::Generators::GeneratedAttribute
- 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
-
#attr_options ⇒ Object
readonly
Returns the value of attribute attr_options.
- #index_name ⇒ Object
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
- .dangerous_name?(name) ⇒ Boolean
- .parse(column_definition) ⇒ Object
- .reference?(type) ⇒ Boolean
- .valid_index_type?(index_type) ⇒ Boolean
- .valid_type?(type) ⇒ Boolean
Instance Method Summary collapse
- #attachment? ⇒ Boolean
- #attachments? ⇒ Boolean
- #column_name ⇒ Object
- #default ⇒ Object
- #field_type ⇒ Object
- #foreign_key? ⇒ Boolean
- #has_index? ⇒ Boolean
- #has_uniq_index? ⇒ Boolean
- #human_name ⇒ Object
-
#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ GeneratedAttribute
constructor
A new instance of GeneratedAttribute.
- #inject_index_options ⇒ Object
- #inject_options ⇒ Object
- #options_for_migration ⇒ Object
- #password_digest? ⇒ Boolean
- #plural_name ⇒ Object
- #polymorphic? ⇒ Boolean
- #reference? ⇒ Boolean
- #required? ⇒ Boolean
- #rich_text? ⇒ Boolean
- #singular_name ⇒ Object
- #to_s ⇒ Object
- #token? ⇒ Boolean
- #virtual? ⇒ Boolean
Constructor Details
#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ GeneratedAttribute
Returns a new instance of GeneratedAttribute.
114 115 116 117 118 119 120 |
# File 'lib/rails/generators/generated_attribute.rb', line 114 def initialize(name, type = nil, index_type = false, = {}) @name = name @type = type || :string @has_index = INDEX_OPTIONS.include?(index_type) @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type) @attr_options = end |
Instance Attribute Details
#attr_options ⇒ Object (readonly)
Returns the value of attribute attr_options.
32 33 34 |
# File 'lib/rails/generators/generated_attribute.rb', line 32 def @attr_options end |
#index_name ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/rails/generators/generated_attribute.rb', line 168 def index_name @index_name ||= if polymorphic? %w(id type).map { |t| "#{name}_#{t}" } else column_name end end |
#name ⇒ Object
Returns the value of attribute name.
31 32 33 |
# File 'lib/rails/generators/generated_attribute.rb', line 31 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
31 32 33 |
# File 'lib/rails/generators/generated_attribute.rb', line 31 def type @type end |
Class Method Details
.dangerous_name?(name) ⇒ Boolean
68 69 70 71 |
# File 'lib/rails/generators/generated_attribute.rb', line 68 def dangerous_name?(name) defined?(ActiveRecord::Base) && ActiveRecord::Base.dangerous_attribute_method?(name) end |
.parse(column_definition) ⇒ Object
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 66 |
# File 'lib/rails/generators/generated_attribute.rb', line 36 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, = *(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) [:index] = { unique: true } end end new(name, type, index_type, ) end |
.reference?(type) ⇒ Boolean
83 84 85 |
# File 'lib/rails/generators/generated_attribute.rb', line 83 def reference?(type) [:references, :belongs_to].include? type end |
.valid_index_type?(index_type) ⇒ Boolean
79 80 81 |
# File 'lib/rails/generators/generated_attribute.rb', line 79 def valid_index_type?(index_type) INDEX_OPTIONS.include?(index_type.to_s) end |
.valid_type?(type) ⇒ Boolean
73 74 75 76 77 |
# File 'lib/rails/generators/generated_attribute.rb', line 73 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
216 217 218 |
# File 'lib/rails/generators/generated_attribute.rb', line 216 def type == :attachment end |
#attachments? ⇒ Boolean
220 221 222 |
# File 'lib/rails/generators/generated_attribute.rb', line 220 def type == :attachments end |
#column_name ⇒ Object
176 177 178 |
# File 'lib/rails/generators/generated_attribute.rb', line 176 def column_name @column_name ||= reference? ? "#{name}_id" : name end |
#default ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rails/generators/generated_attribute.rb', line 138 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_type ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rails/generators/generated_attribute.rb', line 122 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 :textarea when :rich_text then :rich_textarea when :boolean then :checkbox when :attachment, :attachments then :file_field else :text_field end end |
#foreign_key? ⇒ Boolean
180 181 182 |
# File 'lib/rails/generators/generated_attribute.rb', line 180 def foreign_key? name.end_with?("_id") end |
#has_index? ⇒ Boolean
196 197 198 |
# File 'lib/rails/generators/generated_attribute.rb', line 196 def has_index? @has_index end |
#has_uniq_index? ⇒ Boolean
200 201 202 |
# File 'lib/rails/generators/generated_attribute.rb', line 200 def has_uniq_index? @has_uniq_index end |
#human_name ⇒ Object
164 165 166 |
# File 'lib/rails/generators/generated_attribute.rb', line 164 def human_name name.humanize end |
#inject_index_options ⇒ Object
232 233 234 |
# File 'lib/rails/generators/generated_attribute.rb', line 232 def has_uniq_index? ? ", unique: true" : "" end |
#inject_options ⇒ Object
228 229 230 |
# File 'lib/rails/generators/generated_attribute.rb', line 228 def (+"").tap { |s| .each { |k, v| s << ", #{k}: #{v.inspect}" } } end |
#options_for_migration ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rails/generators/generated_attribute.rb', line 236 def @attr_options.dup.tap do || if required? [:null] = false end if reference? && !polymorphic? [:foreign_key] = true end end end |
#password_digest? ⇒ Boolean
204 205 206 |
# File 'lib/rails/generators/generated_attribute.rb', line 204 def password_digest? name == "password" && type == :digest end |
#plural_name ⇒ Object
156 157 158 |
# File 'lib/rails/generators/generated_attribute.rb', line 156 def plural_name name.delete_suffix("_id").pluralize end |
#polymorphic? ⇒ Boolean
188 189 190 |
# File 'lib/rails/generators/generated_attribute.rb', line 188 def polymorphic? [:polymorphic] end |
#reference? ⇒ Boolean
184 185 186 |
# File 'lib/rails/generators/generated_attribute.rb', line 184 def reference? self.class.reference?(type) end |
#required? ⇒ Boolean
192 193 194 |
# File 'lib/rails/generators/generated_attribute.rb', line 192 def required? reference? && Rails.application.config.active_record.belongs_to_required_by_default end |
#rich_text? ⇒ Boolean
212 213 214 |
# File 'lib/rails/generators/generated_attribute.rb', line 212 def rich_text? type == :rich_text end |
#singular_name ⇒ Object
160 161 162 |
# File 'lib/rails/generators/generated_attribute.rb', line 160 def singular_name name.delete_suffix("_id").singularize end |
#to_s ⇒ Object
248 249 250 251 252 253 254 255 256 |
# File 'lib/rails/generators/generated_attribute.rb', line 248 def to_s if has_uniq_index? "#{name}:#{type}#{}:uniq" elsif has_index? "#{name}:#{type}#{}:index" else "#{name}:#{type}#{}" end end |
#token? ⇒ Boolean
208 209 210 |
# File 'lib/rails/generators/generated_attribute.rb', line 208 def token? type == :token end |
#virtual? ⇒ Boolean
224 225 226 |
# File 'lib/rails/generators/generated_attribute.rb', line 224 def virtual? rich_text? || || end |