Module: FlexiModel::Association::ClassMethods
- Defined in:
- lib/flexi_model/association.rb
Instance Method Summary collapse
-
#belongs_to(model_name, options = { }) ⇒ Object
Create relationship between two flexi model based on primary key.
-
#flexi_fields_except_fk ⇒ Object
Return list of fields excluding relationship’s foreign key for.
-
#has_and_belongs_to_many(method_name, options = { }) ⇒ Object
Create many to many relationship between two flexi models based on foreign key.
-
#has_many(model_name, options = { }) ⇒ Object
Create one to many relationship among the flexi models.
Instance Method Details
#belongs_to(model_name, options = { }) ⇒ Object
Create relationship between two flexi model based on primary key
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/flexi_model/association.rb', line 18 def belongs_to(model_name, = { }) _id_field = [:foreign_key] || "#{model_name.to_s}_id" _target_model = ([:class_name] || model_name).to_s.classify self.flexi_field _id_field.to_sym, :integer # Generate dynamic finder method _build_belongs_to_accessors model_name, _target_model, _id_field self.associated_classes["#{model_name.to_s.singularize}_id".to_sym] = _target_model.to_sym (self.associations[:belongs_to] ||= []) << model_name end |
#flexi_fields_except_fk ⇒ Object
Return list of fields excluding relationship’s foreign key for
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/flexi_model/association.rb', line 77 def flexi_fields_except_fk _field_names = self.associations.values.flatten.map { |_v| :"#{_v.to_s.singularize}_id" } if none_flexi_fields.present? none_flexi_fields.each do |field| ['file_name', 'content_type', 'file_size', 'updated_at'].each do |_suffix| _field_names << :"#{field.name.to_s}_#{_suffix}" end end end self.flexi_fields.select { |_f| !_field_names.include?(_f.name.to_sym) } end |
#has_and_belongs_to_many(method_name, options = { }) ⇒ Object
Create many to many relationship between two flexi models based on foreign key
Options
- joining_class - You can define your joining class name or
you can leave it to us to generate on the fly.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/flexi_model/association.rb', line 55 def has_and_belongs_to_many(method_name, = { }) # Get joining class name (dev can specify over :joining_class option) _self_ref, _class_name = _build_joining_class_name(method_name) _joining_class_name = ([:joining_class] || _class_name).to_sym # Build field name _fields = [_build_field_name(self.name), _build_field_name(method_name)] # Eval dynamic joining class _joining_class = _build_joining_class( _joining_class_name, _fields, _self_ref) # Create setter for setting all <target> records _build_habtm_class_accessors( _joining_class_name, method_name, _fields, _self_ref) self.associated_classes["#{method_name.to_s.singularize}_id".to_sym] = _joining_class.name.to_sym (self.associations[:has_and_belongs_to_many] ||= []) << method_name end |
#has_many(model_name, options = { }) ⇒ Object
Create one to many relationship among the flexi models
Options
- :class_name - Target model class name
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/flexi_model/association.rb', line 37 def has_many(model_name, = { }) _class_name = ([:class_name] || model_name.to_s.singularize.classify).to_sym _build_has_many_accessors _class_name, model_name self.associated_classes["#{model_name.to_s.singularize}_id".to_sym] = _class_name.to_sym (self.associations[:has_many] ||= []) << model_name end |