Module: Associatable

Included in:
TGauge::TRecordBase
Defined in:
lib/app/models/associatable.rb

Instance Method Summary collapse

Instance Method Details

#assoc_optionsObject



114
115
116
# File 'lib/app/models/associatable.rb', line 114

def assoc_options
  @assoc_options ||= {}
end

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

Phase IIIb



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/app/models/associatable.rb', line 36

def belongs_to(name, options = {})
  options = BelongsToOptions.new(name, options)

  define_method(name) do
    fkey_val = self.send(options.foreign_key)
    class_obj = options.model_class

    class_obj.where(options.primary_key => fkey_val).first
  end

  assoc_options[name] = options
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/app/models/associatable.rb', line 49

def has_many(name, options = {})
  # ...
  unless options.first[0] == :through
    options = HasManyOptions.new(name, self.to_s, options)

    define_method(name) do
      class_obj = options.model_class
      pr_key = self.send(options.primary_key)
      class_obj.where(options.foreign_key => pr_key)
    end

    assoc_options[name] = options
  else
    options[:source] ||= name
    through(name, options[:through], options[:source])
  end
end

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



109
110
111
112
# File 'lib/app/models/associatable.rb', line 109

def has_one (name, options = {})
  options[:source] ||= name
  through(name, options[:through], options[:source])
end

#through(name, through_name, source_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/app/models/associatable.rb', line 67

def 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]

    if through_options.is_a?(BelongsToOptions)
      # A to B has a belongs_to relationship
      a_test_key = self.send(through_options.foreign_key)
      b_column = through_options.primary_key
    else
      # A to B has a has_many relationship
      a_test_key = self.send(through_options.primary_key)
      b_column = through_options.foreign_key
    end

    if source_options.is_a?(BelongsToOptions)
      # B to C has a belongs_to relationship
      b_test_key = source_options.foreign_key
      c_column = source_options.primary_key
    else
      # B to C has a has_many relationship
      b_test_key = source_options.primary_key
      c_column = source_options.foreign_key
    end
    source_table = source_options.table_name
    through_table = through_options.table_name

    all_data = DBConnection.execute(<<-SQL)
      SELECT
        #{source_table}.*
      FROM
        #{through_table}
      JOIN
        #{source_table} ON #{source_table}.#{c_column} = #{through_table}.#{b_test_key}
      WHERE
        #{through_table}.#{b_column} = #{a_test_key}
    SQL

    source_options.model_class.parse_all(all_data)
  end
end