Module: Associatable
- Included in:
- ModelBase
- Defined in:
- lib/scaffold/lib/model/associations/associatable.rb
Instance Method Summary collapse
- #assoc_options ⇒ Object
- #belongs_to(name, options = {}) ⇒ Object
- #has_many(name, options = {}) ⇒ Object
- #has_many_through(name, through_name, source_name) ⇒ Object
- #has_one(name, options) ⇒ Object
Instance Method Details
#assoc_options ⇒ Object
35 36 37 38 |
# File 'lib/scaffold/lib/model/associations/associatable.rb', line 35 def ||= {} 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, = {}) = BelongsToOptions.new(name, ) [name] = self.define_method(name) do validations = self.class.validators .map { |validator| [validator.attribute, validator.[:presence]] } unless .optional || validations.include?([.foreign_key, true]) self.class.validates .foreign_key, presence: true end .model_class.find(self.send(.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, = {}) if [:through] && [:source] has_many_through(name, [:through], [:source]) else = HasManyOptions.new(name, self.name, ) [name] = self.define_method(name) do .model_class.where(.foreign_key => self.send(.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 = self.class.[through_name] = .model_class.[source_name] if .is_a?(BelongsToOptions) through_self_id = .primary_key self_through_id = .foreign_key else through_self_id = .foreign_key self_through_id = .primary_key end if .is_a?(BelongsToOptions) through_source_id = .foreign_key source_through_id = .primary_key else through_source_id = .primary_key source_through_id = .foreign_key end through_table = .table_name source_table = .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, ) through_name = [:through] source_name = [:source] define_method(name) do = self.class.[through_name] = .model_class.[source_name] through_table = .table_name through_pk = .primary_key through_fk = .foreign_key source_table = .table_name source_pk = .primary_key source_fk = .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) |