This project is archived and is in readonly mode.

#5622 ✓wontfix
Mark Dodwell

boolean_attr_accessor

Reported by Mark Dodwell | September 13th, 2010 @ 07:31 AM

I'd like to propose adding a boolean_attr_accessor core extension to Module:

  class Foo
    boolean_attr_accessor :bar
  end

  foo = Foo.new
  foo.bar = "1"
  foo.bar? #=> true

I find myself needing this feature a lot, specifically when adding virtual boolean attributes to ActiveRecord models using them with form helpers in the view.

Patch attached with a rough version.

Thoughts?

Comments and changes to this ticket

  • Rohit Arondekar

    Rohit Arondekar September 13th, 2010 @ 07:38 AM

    • Importance changed from “” to “Low”

    Mark, I think you should also post on the core list: http://groups.google.com/group/rubyonrails-core You'll get more responses there as to whether this is a desired feature or not.

  • Robert Pankowecki

    Robert Pankowecki September 14th, 2010 @ 08:38 AM

    • Tag changed from activesupport, attr_accessor, core_ext, patch to activesupport, attr_accessor, boolean, core_ext, patch

    I use such code for this:

    
    module Kernel
      def Boolean(b)
        return b if [FalseClass, TrueClass, NilClass].any?{|k| b.is_a?(k)}
        case b
        when /^1$/, /^true$/i , /^yes$/i then true
        when /^0$/, /^false$/i , /^no$/i then false
        else
          raise ArgumentError, "Unknown conversion to boolean type from: #{b.class}, Inspect: #{b.inspect}"
        end
      end
    end
    
    def bar=(value)
      write_attribute(:bar, Boolean(value))
    end
    
  • Gaston Ramos

    Gaston Ramos September 15th, 2010 @ 10:17 AM

    I like the idea, but I'm not sure if it is right to take a lot of values as false

  • Aditya Sanghi

    Aditya Sanghi October 6th, 2010 @ 06:56 PM

    You may want to make this a plugin first? If it's a really popular plugin, perhaps it will get included in the future.

  • Ryan Bigg

    Ryan Bigg October 6th, 2010 @ 09:47 PM

    • State changed from “new” to “wontfix”

    Yes, please make this a plugin.

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>

Attachments

Pages