Module: Associatable

Included in:
SQLObject
Defined in:
lib/opal_orm/associatable.rb

Instance Method Summary collapse

Instance Method Details

#assoc_optionsObject



56
57
58
# File 'lib/opal_orm/associatable.rb', line 56

def assoc_options
  @assoc_options ||= {}
end

#belongs_to(name, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# 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 = {}) ⇒ Object



48
49
50
51
52
53
54
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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