Sha256: 50c95f38a64d6923683b62cf51caf15fb49d9b4587c51ac151191fd1a900e29d

Contents?: true

Size: 700 Bytes

Versions: 4

Compression:

Stored size: 700 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 => :invalid)
    record
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
josevalim-auth_helpers-0.2.0 lib/auth_helpers.rb
josevalim-auth_helpers-0.2.1 lib/auth_helpers.rb
josevalim-auth_helpers-0.3.0 lib/auth_helpers.rb
josevalim-auth_helpers-0.3.1 lib/auth_helpers.rb