Module: EacRailsUtils::Models::TablelessAssociations::ClassMethods
- Defined in:
- lib/eac_rails_utils/models/tableless_associations.rb
Instance Method Summary collapse
-
#belongs_to(name, scope = nil, **options) ⇒ Object
define association like ActiveRecord.
-
#has_many(name, scope = nil, **options, &extension) ⇒ Object
define association like ActiveRecord.
Instance Method Details
#belongs_to(name, scope = nil, **options) ⇒ Object
define association like ActiveRecord
27 28 29 30 31 |
# File 'lib/eac_rails_utils/models/tableless_associations.rb', line 27 def belongs_to(name, scope = nil, **) reflection = ActiveRecord::Associations::Builder::BelongsTo .build(self, name, scope, ) ActiveRecord::Reflection.add_reflection self, name, reflection end |
#has_many(name, scope = nil, **options, &extension) ⇒ Object
define association like ActiveRecord
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/eac_rails_utils/models/tableless_associations.rb', line 34 def has_many(name, scope = nil, **, &extension) # rubocop:disable Naming/PredicateName, Metrics/MethodLength .reverse_merge!(active_model: true, target_ids: "#{name.to_s.singularize}_ids") if scope.is_a?(Hash) .merge!(scope) scope = nil end reflection = ActiveRecord::Associations::Builder::HasManyForActiveModel .build(self, name, scope, , &extension) ActiveRecord::Reflection.add_reflection self, name, reflection mixin = generated_association_methods mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{[:target_ids]}=(other_ids) @#{[:target_ids]} = other_ids association(:#{name}).reset association(:#{name}).reset_scope @#{[:target_ids]} end CODE end |