views:

29

answers:

1

I'm talking half-ruby half-english in the following paragraph

I have Persons and I have 'Pet's. Person has_many Pet. I have a table of Persons displayed with pets included in the cell, like:

def pets_column(record)
  if record.pets.count == 0
    'no pets'
  else
    record.pets.collect{|p| html_escape(p.to_label) }.join('<br />')
  end
end

It is displayed correctly and it is a link which displays a nested table for the pets.

I want to decide that it should be a link or not on a per record basis according to some condition. For example if record_frozen_at is not null then the Pet list of the record also must be frozen. (No addition, no removal, no update for those pets)

(The columns[:pets].clear_link does it for the whole table not on a per record basis.)

A: 

The better solution is to control operations on the nested table instead of totally disabling it.

class PetController < ActionController::Base

  # ...

  protected

  def person_mutable?
    person_id = active_scaffold_constraints[:person]
    return false if person_id.nil?
    person = person.find_by_id(person_id)
    return false if person.nil?
    return person.record_frozen_at.nil?
  end

  def create_authorized?
    return person_mutable?
  end

  def update_authorized?
    return person_mutable?
  end

  def delete_authorized?
    return person_mutable?
  end
end
Notinlist
BTW The original problem is still interesting!!!
Notinlist