This project is archived and is in readonly mode.

#4865 ✓invalid
petRUShka

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

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

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...

Comments and changes to this ticket

  • Kane

    Kane June 15th, 2010 @ 06:50 PM

    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?

  • petRUShka

    petRUShka June 15th, 2010 @ 07:04 PM

    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
    
  • Kane

    Kane June 15th, 2010 @ 07:13 PM

    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.

  • petRUShka

    petRUShka June 15th, 2010 @ 09:28 PM

    May be you are right

  • José Valim

    José Valim June 22nd, 2010 @ 04:31 PM

    • State changed from “new” to “invalid”
  • Ryan Bigg

    Ryan Bigg October 11th, 2010 @ 10:57 AM

    Automatic cleanup of spam.

  • bingbing

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages