Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Tests for generating join models with alternate names #1543

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jagthedrummer
Copy link
Contributor

@jagthedrummer jagthedrummer commented Jun 14, 2024

Test setup:

  rails g super_scaffold Task Team name:text_field --navbar="ti ti-list"
  rails generate super_scaffold:join_model TaskAssignment assigned_to_membership_id{class_name=Membership} assigned_task_id{class_name=Task}

First failing test, fails with:

Minitest::UnexpectedError: NameError: undefined local variable or method membership' for an instance of TaskAssignment app/models/task_assignment.rb:24:in valid_assigned_tasks'
bullet_train-scope_validator (1.7.12) lib/validators/scope_validator.rb:8:in validate_each' test/system/super_scaffolding/super_scaffolding_test.rb:294:in block in class:SuperScaffoldingSystemTest'

This error can be fixed by changing app/models/task_assignment.rb from
this:

  belongs_to :assigned_to_membership, class_name: "Membership"
  belongs_to :assigned_task, class_name: "Task"

To this:

  belongs_to :assigned_to_membership, class_name: "Membership", foreign_key: :assigned_to_membership_id
  belongs_to :assigned_task, class_name: "Task", foreign_key: :assigned_task_id

Then the next error is:

Minitest::UnexpectedError: NameError: undefined local variable or method membership' for an instance of TaskAssignment app/models/task_assignment.rb:24:in valid_assigned_tasks'
bullet_train-scope_validator (1.7.12) lib/validators/scope_validator.rb:8:in validate_each' test/system/super_scaffolding/super_scaffolding_test.rb:294:in block in class:SuperScaffoldingSystemTest'

Change app/models/task_assignment.rb from:

  def valid_assigned_tasks
    membership.valid_tasks
  end

To:

  def valid_assigned_tasks
    assigned_to_membership.valid_assigned_tasks
  end

Then the error is:

Minitest::UnexpectedError: NoMethodError: undefined method valid_assigned_tasks' for an instance of Membership app/models/task_assignment.rb:24:in valid_assigned_tasks'
bullet_train-scope_validator (1.7.12) lib/validators/scope_validator.rb:8:in validate_each' test/system/super_scaffolding/super_scaffolding_test.rb:294:in block in class:SuperScaffoldingSystemTest'

Add a method like this to app/models/membership.rb

  def valid_assigned_tasks
    team.tasks
  end

@jagthedrummer
Copy link
Contributor Author

jagthedrummer commented Jun 14, 2024

Note to self, output of generating the join model

$ rails generate super_scaffold:join_model TaskAssignment assigned_to_membership_id{class_name=Membership} assigned_task_id{class_name=Task}
Generating model with 'bin/rails generate model TaskAssignment assigned_to_membership:references assigned_task:references'
Updating './app/models/task_assignment.rb'.
Updating './app/models/task_assignment.rb'.
Replacing in 'db/migrate/20240614204754_create_task_assignments.rb'.
Replacing in 'db/migrate/20240614204754_create_task_assignments.rb'.
Updating './app/models/task_assignment.rb'.
No need to update './app/models/task_assignment.rb'. It already has 'belongs_to :assigned_to_membership, class_name: "Membership"'.
Updating './app/models/task_assignment.rb'.
Updating './app/models/task_assignment.rb'.
Replacing in 'db/migrate/20240614204754_create_task_assignments.rb'.
Replacing in 'db/migrate/20240614204754_create_task_assignments.rb'.
Updating './app/models/task_assignment.rb'.
No need to update './app/models/task_assignment.rb'. It already has 'belongs_to :assigned_task, class_name: "Task"'.
Updating './app/models/membership.rb'.
Updating './app/models/membership.rb'.
Updating './app/models/task.rb'.
Updating './app/models/task.rb'.

Minitest::UnexpectedError:         NameError: undefined local variable or method `membership' for an instance of TaskAssignment
            app/models/task_assignment.rb:24:in `valid_assigned_tasks'
            bullet_train-scope_validator (1.7.12) lib/validators/scope_validator.rb:8:in `validate_each'
            test/system/super_scaffolding/super_scaffolding_test.rb:294:in `block in <class:SuperScaffoldingSystemTest>'

This error can be fixed by changing `app/models/task_assignment.rb` from
this:

```ruby
  belongs_to :assigned_to_membership, class_name: "Membership"
  belongs_to :assigned_task, class_name: "Task"
```

To this:

```ruby
  belongs_to :assigned_to_membership, class_name: "Membership", foreign_key: :assigned_to_membership_id
  belongs_to :assigned_task, class_name: "Task", foreign_key: :assigned_task_id
```

Then the next error is:

Minitest::UnexpectedError:         NameError: undefined local variable or method `membership' for an instance of TaskAssignment
            app/models/task_assignment.rb:24:in `valid_assigned_tasks'
            bullet_train-scope_validator (1.7.12) lib/validators/scope_validator.rb:8:in `validate_each'
            test/system/super_scaffolding/super_scaffolding_test.rb:294:in `block in <class:SuperScaffoldingSystemTest>'

Change `app/models/task_assignment.rb` from:

```ruby
  def valid_assigned_tasks
    membership.valid_tasks
  end
```

To:

```ruby
  def valid_assigned_tasks
    assigned_to_membership.valid_assigned_tasks
  end
```

Then the error is:

Minitest::UnexpectedError:         NoMethodError: undefined method `valid_assigned_tasks' for an instance of Membership
            app/models/task_assignment.rb:24:in `valid_assigned_tasks'
            bullet_train-scope_validator (1.7.12) lib/validators/scope_validator.rb:8:in `validate_each'
            test/system/super_scaffolding/super_scaffolding_test.rb:294:in `block in <class:SuperScaffoldingSystemTest>'

Add a method like this to `app/models/membership.rb`

```ruby
  def valid_assigned_tasks
    team.tasks
  end
```
@jagthedrummer jagthedrummer force-pushed the jeremy/join-model-attribute-naming-redux branch from 984f7b1 to 1f388ee Compare June 19, 2024 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant