Sha256: 6ea7d1294bfb4f1c6d5a91650d5a32216d11f1dae69538fac16d217b47ea41d6

Contents?: true

Size: 734 Bytes

Versions: 1

Compression:

Stored size: 734 Bytes

Contents

module GetOrBuild
  module AssociationBuilder
    # This is ghost method which generates methods like: "association_name_or_build", for example:
    # class User < ActiveRecord::Base
    #   belongs_to :company
    # end
    #
    # u = User.new
    # u.company           # => nil
    # u.company_or_build  # => #<Company:0x007fe7218be420>
    # Here the method `company_or_build` for `User` instane `u` was generated
    def method_missing(method, *args)
      if method =~ /(\w+)_or_build$/
        if result = send($1, *args)
          result
        else
          builder_method = "build_#{$1}"
          send(builder_method, *args) if respond_to?(builder_method)
        end
      else
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
get_or_build-0.0.2 lib/get_or_build/association_builder.rb