Sha256: 45ed65e34e58d9a59b6022e6f0ed3e29bb4b6df2a0cba82dd4136cb8324660ea

Contents?: true

Size: 779 Bytes

Versions: 1

Compression:

Stored size: 779 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.2.0 lib/vundabar/orm/associations.rb