module Associatable

Public Instance Methods

assoc_options() click to toggle source
# File lib/opal_orm/associatable.rb, line 56
def assoc_options
  @assoc_options ||= {}
end
belongs_to(name, options = {}) click to toggle source
# File lib/opal_orm/associatable.rb, line 37
def belongs_to(name, options = {})
  assoc_options[name] = BelongsToOptions.new(name,options)
  define_method(name) do
    # p "within class"
    # p @assoc_options = BelongsToOptions.new(name,options)
    opts = self.class.assoc_options[name]
    opts.model_class.find(self.send("#{opts.foreign_key}"))
  end
end
has_many(name, options = {}) click to toggle source
# File lib/opal_orm/associatable.rb, line 48
def has_many(name, options = {})
  define_method(name) do
    @options = HasManyOptions.new(name,self.class.to_s,options)
    # TODO: why is this here?
    # cats = @options.model_class.where(@options.foreign_key => id)
  end
end
has_one_through(name, through_name, source_name) click to toggle source
# File lib/opal_orm/associatable.rb, line 60
def has_one_through(name, through_name, source_name)
  define_method(name) do
    through_options = self.class.assoc_options[through_name]
    source_options = through_options.model_class.assoc_options[source_name]

    through =
      through_options.model_class
      .where(through_options.primary_key => id)
      .first

    source_options.model_class.find(through.id)
  end
end