Rails 3.1 and I'm working through activeldap which is not exactly ActiveRecord

class Group < ActiveLdap::Base
def validate
errors.add(:base, "You must enter a value for the 'Common name'") unless self.cn.to_s != ''
errors.add(:base, "You must enter a value for the 'GID Number'") unless self.gidNumber.to_s != ''
errors.add(:base, "You must enter a value for the 'Description'") unless self.description.to_s != ''
errors.add(:base, "You must enter a value for the 'sambaGroupType'") unless self["objectclass"].include? "sambaGroupMapping"
return errors
end

irb(main):014:0> @group = Group.new
=> #<Group objectClass:<top, posixGroup>, must:<cn, gidNumber, objectClass>, may:<description, memberUid, userPassword>, cn: [], commonName: [], description: [], gidNumber: [], memberUid: [], objectClass: ["top", "PosixGroup"], userPassword: []>

irb(main):015:0> @group.validate
=> #<ActiveModel::Errors:0xa4b706c @base=#<Group objectClass:<top, posixGroup>, must:<cn, gidNumber, objectClass>, may:<description, memberUid, userPassword>, cn: [], commonName: [], description: [], gidNumber: [], memberUid: [], objectClass: ["top", "PosixGroup"], userPassword: []>, @messages=#<OrderedHash {:base=>["You must enter a value for the 'Common name'", "You must enter a value for the 'GID Number'", "You must enter a value for the 'Description'", "You must enter a value for the 'sambaGroupType'"]}>>

irb(main):016:0> @group.validate.messages.each do |a|
irb(main):017:1* puts a
irb(main):018:1> end
base
You must enter a value for the 'Common name'
You must enter a value for the 'GID Number'
You must enter a value for the 'Description'
You must enter a value for the 'sambaGroupType'
You must enter a value for the 'Common name'
You must enter a value for the 'GID Number'
You must enter a value for the 'Description'
You must enter a value for the 'sambaGroupType'

Or if I use my rails app instead and put @group.validate into flash hash, I also get duplication...

base You must enter a value for the 'Description' base You must enter a value for the 'Description'

(and I really would prefer to get the 'base' out of there altogether but can't seem to find a way to do that either)

--
Craig White ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ craig.white@ttiltd.com
1.800.869.6908 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ www.ttiassessments.com

Need help communicating between generations at work to achieve your desired success? Let us help!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Frederick Cheung at Nov 29, 2011 at 11:25 pm

    On Nov 29, 5:37 pm, Craig White wrote:
    Rails 3.1 and I'm working through activeldap which is not exactly ActiveRecord

    class Group < ActiveLdap::Base
    def validate
    errors.add(:base, "You must enter a value for the 'Common name'") unless self.cn.to_s != ''
    errors.add(:base, "You must enter a value for the 'GID Number'") unless self.gidNumber.to_s != ''
    errors.add(:base, "You must enter a value for the 'Description'") unless self.description.to_s != ''
    errors.add(:base, "You must enter a value for the 'sambaGroupType'") unless self["objectclass"].include? "sambaGroupMapping"
    return errors
    end
    Everytime validate is called you're adding errors, nothing is
    resetting errors so it stands to reason that you're going to get
    duplicate (or triplicate if validate was called again etc) error
    messages.
    Given that activeldap seems to be using active model for its
    validations stuff it's going to be very close to active record, so you
    shouldn't be calling validate directly - call group.valid? to see if
    the object is valid and if it isn't look at group.errors

    Fred

    irb(main):014:0> @group = Group.new
    => #<Group objectClass:<top, posixGroup>, must:<cn, gidNumber, objectClass>, may:<description, memberUid, userPassword>, cn: [], commonName: [], description: [], gidNumber: [], memberUid: [], objectClass: ["top", "PosixGroup"], userPassword: []>

    irb(main):015:0> @group.validate
    => #<ActiveModel::Errors:0xa4b706c @base=#<Group objectClass:<top, posixGroup>, must:<cn, gidNumber, objectClass>, may:<description, memberUid, userPassword>, cn: [], commonName: [], description: [], gidNumber: [], memberUid: [], objectClass: ["top", "PosixGroup"], userPassword: []>, @messages=#<OrderedHash {:base=>["You must enter a value for the 'Common name'", "You must enter a value for the 'GID Number'", "You must enter a value for the 'Description'", "You must enter a value for the 'sambaGroupType'"]}>>

    irb(main):016:0> @group.validate.messages.each do |a|
    irb(main):017:1* puts a
    irb(main):018:1> end
    base
    You must enter a value for the 'Common name'
    You must enter a value for the 'GID Number'
    You must enter a value for the 'Description'
    You must enter a value for the 'sambaGroupType'
    You must enter a value for the 'Common name'
    You must enter a value for the 'GID Number'
    You must enter a value for the 'Description'
    You must enter a value for the 'sambaGroupType'

    Or if I use my rails app instead and put @group.validate into flash hash, I also get duplication...

    base You must enter a value for the 'Description' base You must enter a value for the 'Description'

    (and I really would prefer to get the 'base' out of there altogether but can't seem to find a way to do that either)

    --
    Craig White ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ craig.wh...@ttiltd.com
    1.800.869.6908 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~www.ttiassessments.com

    Need help communicating between generations at work to achieve your desired success? Let us help!
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Craig White at Nov 29, 2011 at 11:41 pm

    On Nov 29, 2011, at 4:25 PM, Frederick Cheung wrote:
    On Nov 29, 5:37 pm, Craig White wrote:
    Rails 3.1 and I'm working through activeldap which is not exactly ActiveRecord

    class Group < ActiveLdap::Base
    def validate
    errors.add(:base, "You must enter a value for the 'Common name'") unless self.cn.to_s != ''
    errors.add(:base, "You must enter a value for the 'GID Number'") unless self.gidNumber.to_s != ''
    errors.add(:base, "You must enter a value for the 'Description'") unless self.description.to_s != ''
    errors.add(:base, "You must enter a value for the 'sambaGroupType'") unless self["objectclass"].include? "sambaGroupMapping"
    return errors
    end
    Everytime validate is called you're adding errors, nothing is
    resetting errors so it stands to reason that you're going to get
    duplicate (or triplicate if validate was called again etc) error
    messages.
    Given that activeldap seems to be using active model for its
    validations stuff it's going to be very close to active record, so you
    shouldn't be calling validate directly - call group.valid? to see if
    the object is valid and if it isn't look at group.errors
    ----
    thanks Fred...

    valid? indeed works and so does errors and yes, it obviously uses ActiveModel now but since it's LDAP but not user friendly, it's not for the uninitiated.

    @group.errors is a mess and @person.errors - fahgettaboutit and will not be useful to someone trying to use the application so I ended up using .to_a.uniq to toss out the dupes...

    I finally just worked it out a minute ago with this (in my Person model)

    flash[:error] = @person.validate['base'].to_a.uniq.join("<br />")

    apparently have to use .html_safe in my view to view the flash[:error] and it's reasonably friendly now.

    Craig

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedNov 29, '11 at 5:37p
activeNov 29, '11 at 11:41p
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Craig White: 2 posts Frederick Cheung: 1 post

People

Translate

site design / logo © 2021 Grokbase