bullet-train-co / bullet_train

The Open Source Ruby on Rails SaaS Template
MIT License
1.58k stars 244 forks source link

Super scaffolding makes some incorrect assumptions about naming #1508

Open jagthedrummer opened 1 month ago

jagthedrummer commented 1 month ago

I first user super scaffolding to create a Task model:

rails g super_scaffold Task Team name:text_field 

Then I want to make it so that a Task can be assigned to many Memberships under the name assigned_to_memberships. (Just using memberships won't work because I also want to have an approvable_by_memberships relationship that also points to the Membership model.)

I tried generating a join model by doing this:

rails generate super_scaffold:join_model Tasks::Assignment assigned_to_membership_id{class_name=Membership} task_id{class_name=Task}

That results in Task getting updated with these lines:

  has_many :assignments, class_name: "Tasks::Assignment", dependent: :destroy
  has_many :memberships, through: :assignments

But I would expect it to be more like this given the names that I'm using:

  has_many :assignments, class_name: "Tasks::Assignment", dependent: :destroy
  has_many :assigned_to_memberships, class_name: "Membership", through: :assignments

It also generates a valid_tasks method in Tasks::Assignment that is defined like this:

  def valid_tasks
    membership.valid_tasks
  end

But the Membership model was not updated to have a valid_tasks method. And again, based on the naming that I'm using I'd expect it to be assigned_to_memberships.valid_tasks.

jagthedrummer commented 4 weeks ago

Just trying to look into this and it looks like this line in the JoinModelScaffolder is what's responsible for getting the has_many declaration wrong.

https://github.com/bullet-train-co/bullet_train-core/blob/9b7f280d3a981b11f8f925e946ebff4c1d029436/bullet_train-super_scaffolding/lib/bullet_train/super_scaffolding/scaffolders/join_model_scaffolder.rb#L84

jagthedrummer commented 4 weeks ago

I think the problem is probably related to the fact that the Scaffolding::ClassNamesTransformer only deals with model/class names and doesn't account for the fact that a relationship might use different names.

https://github.com/bullet-train-co/bullet_train-core/blob/9b7f280d3a981b11f8f925e946ebff4c1d029436/bullet_train-super_scaffolding/lib/scaffolding/class_names_transformer.rb#L1-L8