Module: Associatable

Included in:
Bezel::BezelrecordBase
Defined in:
lib/bezelrecord_base/associatable.rb

Instance Method Summary collapse

Instance Method Details

#assoc_optionsObject



64
65
66
# File 'lib/bezelrecord_base/associatable.rb', line 64

def assoc_options
  @prior_options ||= {}
end

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bezelrecord_base/associatable.rb', line 38

def belongs_to(name, options = {})
  options = BelongsToOptions.new(name, options)
  @prior_options = assoc_options
  @prior_options[name] = options

  define_method(options.class_name.downcase.to_sym) do
    foreign_key = send(options.foreign_key)
    the_class = options.model_class
    the_class.where(options.primary_key => foreign_key).first
  end
end

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



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bezelrecord_base/associatable.rb', line 50

def has_many(name, options = {})
  options = HasManyOptions.new(name, self,  options)
  @prior_options = assoc_options
  if @prior_options[name.to_s.singularize.to_sym].nil?
    @prior_options[name.to_s.singularize.to_sym] = options
  else
    @prior_options[name.to_s.singularize.to_sym] << options
  end
  define_method(options.table_name.to_sym) do
    primary_key = send(options.primary_key)
    options.model_class.where(options.foreign_key => primary_key)
  end
end

#has_many_through(name, through_name, source_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bezelrecord_base/associatable.rb', line 89

def has_many_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.to_s.singularize.to_sym]
    search_classes = through_options.model_class.where(through_options.foreign_key => send(source_options.primary_key))

    search_by = []
    search_classes.each do |classes|
      search_by << classes.send(source_options.primary_key)
    end

    results = DBConnection.execute(<<-SQL, *search_by)
      SELECT
      #{source_options.table_name}.*
      FROM
      #{through_options.table_name}
      JOIN
      #{source_options.table_name} ON #{source_options.table_name}.#{source_options.foreign_key.to_s} = #{through_options.table_name}.#{through_options.primary_key.to_s}
      WHERE
      #{through_options.table_name}.#{through_options.primary_key} IN (#{Array.new(search_by.length,"?").join(", ")})
      SQL
    results.map{|result| source_options.model_class.new(result)}
  end
end

#has_one_through(name, through_name, source_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bezelrecord_base/associatable.rb', line 68

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]
    search_by = send(through_options.foreign_key)

    results = DBConnection.execute(<<-SQL, search_by)
      SELECT
      #{source_options.table_name}.*
      FROM
      #{through_options.table_name}
      JOIN
      #{source_options.table_name} ON #{source_options.table_name}.#{source_options.primary_key.to_s} = #{through_options.table_name}.#{source_options.foreign_key.to_s}
      WHERE
      #{through_options.table_name}.#{through_options.primary_key} = ?
    SQL
    source_options.model_class.new(results.first)
  end
end