Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[Feature Request] add :if / :unless conditions for attr_accessible, attr_protected and attr_readonly

#4865

It would be very useful for organising state-driven behaviour for model.

Aim is ability to write code like this

 with_options :if => :new? do |c|
  c.validates :date, :presence => true
  c.attr_accessor :city, :address
 end
 with_options :if => :confirmed? do |c|
  c.attr_accessible :nil
  c.attr_readonly :user_id
 end


def new? ... end def confirmed? ... end

Discussion at state_machine lighthouse: http://pluginaweek.lighthouseapp.com/projects/13288/tickets/49-feat...

Reported by petRUShka · June 15th, 2010 @ 07:30 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Kane
    Kane

    those methods are there to control mass-assignment. I cant see any use in deciding if mass-assignment is allowed or not by state. use case?

    June 15th, 2010 @ 06:50 PM

  2. petRUShka
    petRUShka

    Order created at two step "prebooking" and "book". At prebooking step user set only part of attributes (minimal set of attributes to check whether date is free). And then that attributes can't be changed, because we have limited quotes for hotel at day, and if bad man change, for example, date by mass-assignment we can get an hidden error.

     with_options :if => :prebooking? do |c|
      c.attr_accessor :city, :hotel, :date
     end
     with_options :if => :book? do |c|
      c.attr_accessor :name, :phone, :email
      c.attr_readonly :city, :address, :date
     end
    

    June 15th, 2010 @ 07:04 PM

  3. Kane
    Kane

    to deny changes of some attributes related to the current state validators should be used. i think you misuse those methods. they are only there to control mass-assignment, like in controllers.

    June 15th, 2010 @ 07:13 PM

  4. petRUShka
    petRUShka

    May be you are right

    June 15th, 2010 @ 09:28 PM

  5. José Valim
    José Valim
    • State changed from new to invalid

    June 22nd, 2010 @ 04:31 PM

  6. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:57 AM

  7. bingbing