Sha256: 511a65369524761e4c85111aa00912ecf8b8d691996700de5086fa5d6b895727

Contents?: true

Size: 713 Bytes

Versions: 2

Compression:

Stored size: 713 Bytes

Contents

module AuthHelpers
  # Helper that find or initialize an object by attribute only if the given value
  # is not blank. If it's blank, create a new object using :new.
  #
  def self.find_or_initialize_by_unless_blank(klass, attr, value)
    if value.blank?
      klass.new
    else
      klass.send(:"find_or_initialize_by_#{attr}", value)
    end
  end

  # Creates a new record, assigning the perishable token and an error message.
  #
  def self.new_with_perishable_token_error(klass, message=:invalid, options={})
    record = klass.new(options)
    record.perishable_token = options[:perishable_token]
    record.errors.add(:perishable_token, message, :default => [:"messages.invalid"])
    record
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
josevalim-auth_helpers-0.4.1 lib/auth_helpers.rb
josevalim-auth_helpers-0.5.0 lib/auth_helpers.rb