Module: ActivityNotification::Association
- Extended by:
- ActiveSupport::Concern
- Included in:
- Notifiable, Notifier, ORM::Mongoid::Notification, ORM::Mongoid::Subscription, Subscriber, Target
- Defined in:
- lib/activity_notification/orm/mongoid.rb,
lib/activity_notification/orm/active_record.rb
Class Method Summary collapse
-
.belongs_to_polymorphic_xdb_record(name, _options = {}) ⇒ Object
Defines polymorphic belongs_to association with models in other database.
-
.has_many_polymorphic_xdb_records(name, options = {}) ⇒ Object
Defines polymorphic has_many association with models in other database.
-
.has_many_records(name, options = {}) ⇒ ActiveRecord_AssociationRelation<Object>
Defines has_many association with ActivityNotification models.
Class Method Details
.belongs_to_polymorphic_xdb_record(name, _options = {}) ⇒ Object
Defines polymorphic belongs_to association with models in other database.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/activity_notification/orm/mongoid.rb', line 13 def belongs_to_polymorphic_xdb_record(name, = {}) association_name = name.to_s.singularize.underscore id_field, type_field = "#{association_name}_id", "#{association_name}_type" field id_field, type: Integer field type_field, type: String self.instance_eval do define_method(name) do |reload = false| reload and self.instance_variable_set("@#{name}", nil) if self.instance_variable_get("@#{name}").blank? if (class_name = self.send(type_field)).present? object_class = class_name.classify.constantize self.instance_variable_set("@#{name}", object_class.where(object_class.primary_key => self.send(id_field)).first) end end self.instance_variable_get("@#{name}") end define_method("#{name}=") do |new_instance| if new_instance.nil? then instance_id, instance_type = nil, nil else instance_id, instance_type = new_instance.id, new_instance.class.name end self.send("#{id_field}=", instance_id) self.send("#{type_field}=", instance_type) self.instance_variable_set("@#{name}", nil) end end end |
.has_many_polymorphic_xdb_records(name, options = {}) ⇒ Object
TODO:
Add dependent option
Defines polymorphic has_many association with models in other database.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/activity_notification/orm/mongoid.rb', line 42 def has_many_polymorphic_xdb_records(name, = {}) association_name = [:as] || name.to_s.underscore id_field, type_field = "#{association_name}_id", "#{association_name}_type" object_name = [:class_name] || name.to_s.singularize.camelize object_class = object_name.classify.constantize self.instance_eval do define_method(name) do |reload = false| reload and self.instance_variable_set("@#{name}", nil) if self.instance_variable_get("@#{name}").blank? self.instance_variable_set("@#{name}", object_class.where(id_field => self.id, type_field => self.class.name)) end self.instance_variable_get("@#{name}") end end end |
.has_many_records(name, options = {}) ⇒ ActiveRecord_AssociationRelation<Object>
Defines has_many association with ActivityNotification models.
8 9 10 |
# File 'lib/activity_notification/orm/mongoid.rb', line 8 def has_many_records(name, = {}) has_many_polymorphic_xdb_records name, end |