Module: PassiveRecord::Associations

Included in:
ClassMethods
Defined in:
lib/passive_record/associations.rb,
lib/passive_record/associations/has_one.rb,
lib/passive_record/associations/has_many.rb,
lib/passive_record/associations/belongs_to.rb,
lib/passive_record/associations/has_many_through.rb

Defined Under Namespace

Classes: BelongsToAssociation, BelongsToRelation, HasManyAssociation, HasManyRelation, HasManyThroughAssociation, HasManyThroughRelation, HasOneAssociation, HasOneRelation, Relation

Instance Method Summary collapse

Instance Method Details

#associate!(assn) ⇒ Object



8
9
10
11
12
# File 'lib/passive_record/associations.rb', line 8

def associate!(assn)
  @associations ||= []
  @associations += [assn] unless @associations.include?(assn)
  self
end

#associations_id_symsObject



14
15
16
17
18
19
20
21
22
# File 'lib/passive_record/associations.rb', line 14

def associations_id_syms
  @associations&.map do |assn|
    if assn.is_a?(HasOneAssociation) || assn.is_a?(BelongsToAssociation)
      (assn.target_name_symbol.to_s + "_id").to_sym
    else
      (assn.target_name_symbol.to_s.singularize + "_ids").to_sym
    end
  end || []
end

#belongs_to(parent_name_sym, opts = {}) ⇒ Object



24
25
26
27
28
# File 'lib/passive_record/associations.rb', line 24

def belongs_to(parent_name_sym, opts={})
  target_class_name = opts.delete(:class_name) { (parent_name_sym.to_s).split('_').map(&:capitalize).join }
  association = BelongsToAssociation.new(self, target_class_name, parent_name_sym)
  associate!(association)
end

#has_and_belongs_to_many(collection_name_sym) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/passive_record/associations.rb', line 54

def has_and_belongs_to_many(collection_name_sym)
  habtm_join_class_name =
    self.name.split('::').last.singularize +
    collection_name_sym.to_s.camelize.singularize +
    "JoinModel"
  inverse_habtm_join_class_name =
    collection_name_sym.to_s.camelize.singularize +
    self.name.split('::').last.singularize +
    "JoinModel"

  if (Object.const_get(inverse_habtm_join_class_name) rescue false)
    has_many inverse_habtm_join_class_name.underscore.pluralize.to_sym
    has_many collection_name_sym, :through => inverse_habtm_join_class_name.underscore.pluralize.to_sym
  else
    auto_collection_sym = self.name.split('::').last.underscore.pluralize.to_sym
    eval <<-ruby
    class ::#{habtm_join_class_name}                        # class UserRoleJoinModel
      include PassiveRecord                                 #   include PassiveRecord
      belongs_to :#{collection_name_sym.to_s.singularize}   #   belongs_to :role
      belongs_to :#{auto_collection_sym.to_s.singularize}   #   belongs_to :user
    end                                                     # end
    ruby
    has_many habtm_join_class_name.underscore.pluralize.to_sym
    has_many(collection_name_sym, :through => habtm_join_class_name.underscore.pluralize.to_sym)
  end
end

#has_many(collection_name_sym, opts = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/passive_record/associations.rb', line 36

def has_many(collection_name_sym, opts={})
  target_class_name = opts.delete(:class_name) { (collection_name_sym.to_s).split('_').map(&:capitalize).join }

  if opts.key?(:through)
    through_class_collection_name = opts.delete(:through)

    through_class_name = (through_class_collection_name.to_s).split('_').map(&:capitalize).join
    base_association = associations.detect { |assn| assn.child_class_name == through_class_name }

    association = HasManyThroughAssociation.new(self, target_class_name, collection_name_sym, through_class_collection_name, base_association)

    associate!(association)
  else
    association = HasManyAssociation.new(self, target_class_name, collection_name_sym)
    associate!(association)
  end
end

#has_one(child_name_sym) ⇒ Object



30
31
32
33
34
# File 'lib/passive_record/associations.rb', line 30

def has_one(child_name_sym)
  child_class_name = (child_name_sym.to_s).split('_').map(&:capitalize).join
  association = HasOneAssociation.new(self, child_class_name, child_name_sym)
  associate!(association)
end