bullet-train-co / bullet_train-base

MIT License
10 stars 16 forks source link

Missing Translation Error - reproducible with cause #147

Open eadz opened 1 year ago

eadz commented 1 year ago

Hi, I've got a reproducible missing translation error.

I ran the following:

./bin/rails g model Page site:references slug:string title:string 
./bin/super-scaffold crud Page Site,Team title:text_field slug:text_field

I created a Page with a blank slug, then hit the missing translation error due to page.label_string being blank, due to slug being blank.

The code that decided that slug was the label is the following:

p.class.columns_hash.values.find{ |column| column.sql_type_metadata.
type == :string }.map(&:name)

Which take the first string field. I did notice that it added a validates :title, presence: true to the model but not a validates :slug, presence: true' I'm assuming thevalidates :title, presence: true` was added for the exact reason the bug happened?

Maybe this can be solved by the crud generator adding a validation to label_field as well? or a documentation change to let people know that the first string field is used for the model's label?

gazayas commented 1 year ago

It looks like order matters here, and the line that determines whether validates :attribute_name gets scaffolded or not is determined in the Super Scaffolding gem:

is_required = attribute_options[:required] || (scaffolding_options[:type] == :crud && index == 0)

I saw that the order you had for your attributes in your rails g and bin/super-scaffold crud commands are different. Maybe we should raise an error at the CLI level if the developer's attributes aren't in the same order when running bin/super-scaffold crud? It looks like some of the moving parts we have in place depend on at least the first attribute matching when running the commands, so my initial instinct is to handle the issue there.