Sha256: 2bde9cf02114979edd5bcdd233a0aa393654d069def2e43a95ba3069d42667a4

Contents?: true

Size: 937 Bytes

Versions: 7

Compression:

Stored size: 937 Bytes

Contents

class Factory

  class << self
    attr_accessor :aliases #:nodoc:
  end
  self.aliases = [
    [/(.*)_id/, '\1'],
    [/(.*)/, '\1_id']
  ]

  # Defines a new alias for attributes
  #
  # Arguments:
  #   pattern: (Regexp)
  #     A pattern that will be matched against attributes when looking for
  #     aliases. Contents captured in the pattern can be used in the alias.
  #   replace: (String)
  #     The alias that results from the matched pattern. Captured strings can
  #     be insert like String#sub.
  #
  # Example:
  #   
  #   Factory.alias /(.*)_confirmation/, '\1'
  def self.alias (pattern, replace)
    self.aliases << [pattern, replace]
  end

  def self.aliases_for (attribute) #:nodoc:
    aliases.collect do |params|
      pattern, replace = *params
      if pattern.match(attribute.to_s)
        attribute.to_s.sub(pattern, replace).to_sym
      else
        nil
      end
    end.compact << attribute
  end

end

Version data entries

7 entries across 7 versions & 5 rubygems

Version Path
dima-exe-factory_girl-1.1.5.0 lib/factory_girl/aliases.rb
dima-exe-factory_girl-1.1.5.1 lib/factory_girl/aliases.rb
gsterndale-warrant-0.2.0 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/aliases.rb
gsterndale-warrant-0.3.0 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/aliases.rb
snowblink-factory_girl-1.1.5 lib/factory_girl/aliases.rb
thoughtbot-factory_girl-1.1.5 lib/factory_girl/aliases.rb
factory_girl-1.1.5 lib/factory_girl/aliases.rb