Sha256: 867c7e308fe0ced33495a21ed4387f6fed773b747ab532d62ec205ba7c114536

Contents?: true

Size: 780 Bytes

Versions: 1

Compression:

Stored size: 780 Bytes

Contents

module Vundabar
  module Associations
    def has_many(name, options = {})
      define_method(name) do
        class_name = name.to_s.singularize.capitalize
        model = options.fetch(:class_name, class_name).to_constant
        foreign_key_column = self.class.to_s.downcase + "_id"
        foreign_key = options.fetch(:foreign_key, foreign_key_column)
        model.where("#{foreign_key} LIKE ?", id)
      end
    end


    def belongs_to(model_name, options = {})
      define_method(model_name) do
        class_name = model_name.to_s.singularize.capitalize
        model = options.fetch(:class_name, class_name).to_constant
        foreign_key = model_name.to_s.concat("_id")
        value = send(foreign_key)
        model.find_by(id: value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vundabar-0.1.0 lib/vundabar/orm/associations.rb