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
126
127
128
129
|
# File 'lib/generators/pu/lib/plutonium_generators/model_generator_base.rb', line 73
def parse(model_name, column_definition)
options_content = nil
if column_definition.include?("{")
options_content = (column_definition)
if options_content
start_idx = column_definition.index("{")
end_idx = start_idx + options_content.length + 2 column_definition = column_definition[0...start_idx] + "{OPTIONS}" + column_definition[end_idx..]
end
end
name, type, index_type = column_definition.split(":")
type = type&.sub("{OPTIONS}", "{#{options_content}}") if options_content
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 "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)
attr_options[:index] = {unique: true}
end
if name.include? "/"
attr_options[:to_table] = name.underscore.tr("/", "_").pluralize.to_sym
attr_options[:class_name] = name.classify
name = name.underscore
if (shared_namespace = find_shared_namespace(model_name, name, separator: "/"))
name = name.sub("#{shared_namespace}/", "")
end
name = name.tr("/", "_")
end
end
new(name, type, index_type, attr_options)
end
|