Module: Associatable

Included in:
ModelBase
Defined in:
lib/scaffold/lib/model/associations/associatable.rb

Instance Method Summary collapse

Instance Method Details

#assoc_optionsObject



35
36
37
38
# File 'lib/scaffold/lib/model/associations/associatable.rb', line 35

def assoc_options
  @assoc_options ||= {}
  @assoc_options
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/scaffold/lib/model/associations/associatable.rb', line 6

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

  self.define_method(name) do
    validations = self.class.validators
      .map { |validator| [validator.attribute, validator.options[:presence]] }

    unless options.optional || validations.include?([options.foreign_key, true])
      self.class.validates options.foreign_key, presence: true 
    end
    
    options.model_class.find(self.send(options.foreign_key))
  end
end

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



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/scaffold/lib/model/associations/associatable.rb', line 22

def has_many(name, options = {})
  if options[:through] && options[:source]
    has_many_through(name, options[:through], options[:source])
  else
    options = HasManyOptions.new(name, self.name, options)
    assoc_options[name] = options
  
    self.define_method(name) do 
      options.model_class.where(options.foreign_key => self.send(options.primary_key)).parsed_query
    end
  end
end

#has_many_through(name, through_name, source_name) ⇒ Object



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
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/scaffold/lib/model/associations/associatable.rb', line 74

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]

    if through_options.is_a?(BelongsToOptions)
      through_self_id = through_options.primary_key
      self_through_id = through_options.foreign_key
    else
      through_self_id = through_options.foreign_key
      self_through_id = through_options.primary_key
    end

    if source_options.is_a?(BelongsToOptions)
      through_source_id = source_options.foreign_key
      source_through_id = source_options.primary_key
    else
      through_source_id = source_options.primary_key
      source_through_id = source_options.foreign_key
    end

    through_table = through_options.table_name
    source_table = source_options.table_name

    key_val = self.send(:id)
    results = DBConnection.execute("      SELECT\n        \#{source_table}.*\n      FROM\n        \#{through_table}\n      JOIN\n        \#{source_table}\n      ON\n        \#{through_table}.\#{through_source_id} = \#{source_table}.\#{source_through_id}\n      JOIN\n        \#{self.class.table_name}\n      ON\n        \#{self.class.table_name}.\#{self_through_id} = \#{through_table}.\#{through_self_id}\n      WHERE\n        \#{self.class.table_name}.id = ?\n    SQL\n\n    source_options.model_class.parse_all(results)\n  end\nend\n", key_val)

#has_one(name, options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/scaffold/lib/model/associations/associatable.rb', line 40

def has_one(name, options)
  through_name = options[:through]
  source_name = options[:source]

  define_method(name) do
    through_options = self.class.assoc_options[through_name]
    source_options = through_options.model_class.assoc_options[source_name]

    through_table = through_options.table_name
    through_pk = through_options.primary_key
    through_fk = through_options.foreign_key

    source_table = source_options.table_name
    source_pk = source_options.primary_key
    source_fk = source_options.foreign_key

    key_val = self.send(through_fk)
    results = DBConnection.execute("      SELECT\n        \#{source_table}.*\n      FROM\n        \#{through_table}\n      JOIN\n        \#{source_table}\n      ON\n        \#{through_table}.\#{source_fk} = \#{source_table}.\#{source_pk}\n      WHERE\n        \#{through_table}.\#{through_pk} = ?\n    SQL\n\n    source_options.model_class.parse_all(results).first\n  end\nend\n", key_val)