Class: PlutoniumGenerators::ModelGeneratorBase::GeneratedAttribute
- Inherits:
-
Rails::Generators::GeneratedAttribute
- Object
- Rails::Generators::GeneratedAttribute
- PlutoniumGenerators::ModelGeneratorBase::GeneratedAttribute
- Defined in:
- lib/generators/pu/lib/plutonium_generators/model_generator_base.rb
Overview
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.parse(model_name, column_definition) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/generators/pu/lib/plutonium_generators/model_generator_base.rb', line 73 def parse(model_name, column_definition) # Protect content inside {} from being split on colons # e.g., "status:string{default:draft}" -> split correctly # Handles nested braces like "data:jsonb{default:{}}" = nil if column_definition.include?("{") = extract_braced_content(column_definition) if start_idx = column_definition.index("{") end_idx = start_idx + .length + 2 # +2 for { and } column_definition = column_definition[0...start_idx] + "{OPTIONS}" + column_definition[end_idx..] end end name, type, index_type = column_definition.split(":") # Restore options content type = type&.sub("{OPTIONS}", "{#{}}") if # 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 "Could not generate field '#{name}', as it is already defined by Active Record." end if type && !valid_type?(type) raise "Could not generate field '#{name}' with unknown type '#{type}'." end if index_type && !valid_index_type?(index_type) raise "Could not generate field '#{name}' with unknown index '#{index_type}'." end if type && reference?(type) if Rails::Generators::GeneratedAttribute::UNIQ_INDEX_OPTIONS.include?(index_type) [:index] = {unique: true} end if name.include? "/" [:to_table] = name.underscore.tr("/", "_").pluralize.to_sym [:class_name] = name.classify name = PlutoniumGenerators::Generator.derive_association_name(model_name, name) end end new(name, type, index_type, ) end |
Instance Method Details
#attribute_name ⇒ Object
271 272 273 274 275 276 277 |
# File 'lib/generators/pu/lib/plutonium_generators/model_generator_base.rb', line 271 def attribute_name if cents? name.sub("_cents", "") else name end end |
#cents? ⇒ Boolean
267 268 269 |
# File 'lib/generators/pu/lib/plutonium_generators/model_generator_base.rb', line 267 def cents? type == :integer && name.ends_with?("_cents") end |
#options_for_migration ⇒ Object
279 280 281 282 283 284 285 286 |
# File 'lib/generators/pu/lib/plutonium_generators/model_generator_base.rb', line 279 def super.tap do || if [:to_table] [:foreign_key] = {to_table: .delete(:to_table)} end .delete(:class_name) end end |
#required? ⇒ Boolean
259 260 261 262 263 264 265 |
# File 'lib/generators/pu/lib/plutonium_generators/model_generator_base.rb', line 259 def required? if .key?(:null) ![:null] else super end end |