bullet_train icon indicating copy to clipboard operation
bullet_train copied to clipboard

Unable to Super Scaffold attribute with same name as model

Open gazayas opened this issue 2 years ago • 1 comments

Say for instance we Super Scaffold the following.

> rails g model Foo team:references name:string foo:string
> bin/super-scaffold crud Foo Team name:text_field foo:text_field

Both attributes show will show up in the form. スクリーンショット 2023-02-23 9 20 55

However, foo isn't added to the model's strong parameters, so it doesn't get saved. スクリーンショット 2023-02-23 9 21 03

> rails c
Loading development environment (Rails 7.0.4.2)
irb(main):001:0> Foo.first
  Foo Load (0.3ms)  SELECT "foos".* FROM "foos" ORDER BY "foos"."id" ASC LIMIT $1  [["LIMIT", 1]]
=>                                                             
#<Foo:0x0000000116e94500                                       
 id: 1,                                                        
 team_id: 1,                                                   
 name: "Name",                                                 
 foo: nil,                                                     
 created_at: Thu, 23 Feb 2023 00:20:56.652016000 UTC +00:00,   
 updated_at: Thu, 23 Feb 2023 00:20:56.652016000 UTC +00:00>   
irb(main):002:0> exit

I'm assuming we should be able to do this though, because we can do the following without raising any errors in a vanilla Rails app.

> rails g scaffold Foo name:string foo:string

gazayas avatar Feb 23 '23 00:02 gazayas

Found the problem. We have the following in the transformer:

if target_file_content.include?(transformed_content)
  puts "No need to update '#{transformed_file_name}'. It already has '#{transformed_content}'." unless 

target_file_content is a string of the entire file. When Super Scaffolding Foo, even though we're only trying to add an attribute to strong parameters, thetransformed_content is ":foo," which already exists in the controller:

account_load_and_authorize_resource :foo, through: :team, through_association: :foos

I think this one will take a bit of heavy-lifting to make the logic surrounding this problem as a whole smooth and easy-to-understand (for example, we could isolate the strong parameters in the file and analyze that only to see if the new transformed content is included).

I'm not sure if it's currently worth the effort right now though, so I'll let this one sit and handle it later if we want to prioritize this one.

gazayas avatar Feb 23 '23 03:02 gazayas