Skip to content

Re-enable test for Workshops/new in workshops_categories_and_sectors_spec.rb:9 #742

@maebeale

Description

@maebeale

Workshop categories & sectors / CREATE workshop / it assigns categories and sectors from checkboxes

This test is failing saying that there was no Workshop created.

It's failing bc going to /workshops/new to create a new Workshop AND clicking the checkboxes for sectors or categories is trying to create associated sectorable_items or categorizable_items but the sectorable_id or categorizable_id of this new workshop is empty bc this new workshop doesn't have an id yet (bc it's a new workshop).

There is a special handshake in the workshops_controller (assign_associations(@workshop)), and a callback on the workshop model (after_save :assign_pending_associations), and those along w the validations on sectorable_id and categorizable_id are making it so this test fails.

This does work correctly in the UI, which is why we've marked this test xit for now.

We'd rather not stub or hack the test too much, as we do want a system test that makes sure we can create a new workshop that has some checkboxes clicked for sector and/or category.

So, this ticket is asking to figure out what about the test is making it be handled differently than the code (otherwise why does it work in the UI, but not in the test?), and to make it so we are testing the feature and that we can validate that the feature is working with the test.

We have some hacks in workshop.rb rn that we prob want to remove as part of this work?

  # Temporary storage for association IDs during creation
  attr_accessor :pending_sector_ids, :pending_category_ids

  # Callbacks
  after_save :assign_pending_associations

 # Override sector_ids= to defer association assignment until after save
  def sector_ids=(ids)
    if new_record?
      @pending_sector_ids = ids
    else
      super
    end
  end

  # Override category_ids= to defer association assignment until after save
  def category_ids=(ids)
    if new_record?
      @pending_category_ids = ids
    else
      super
    end
  end

  private

  def assign_pending_associations
    # Only run this on initial save, not on subsequent updates
    return unless @pending_sector_ids || @pending_category_ids

    if @pending_sector_ids
      self.sectors = Sector.where(id: @pending_sector_ids)
      @pending_sector_ids = nil
    end

    if @pending_category_ids
      self.categories = Category.where(id: @pending_category_ids)
      @pending_category_ids = nil
    end
  end

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions