Sha256: 990301bdeb2dff49fb9d65b5baab77354e6dd543bd9cf57ea4e959c7f35cc5cf

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

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)                                       # if result = send("company", *args)
          result                                                          #   result
        else                                                              # else
          builder_method = "build_#{$1}"                                  #   builder_method = "build_company"
          send(builder_method, *args) if respond_to?(builder_method)      #   send(builder_method, *args) if respond_to?(builder_method)
        end                                                               # end
      else
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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