Module: ActsAsJoinable::ClassMethods

Defined in:
lib/acts-as-joinable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_joinableObject



58
59
60
# File 'lib/acts-as-joinable.rb', line 58

def acts_as_joinable
  acts_as_joinable_on
end

#acts_as_joinable_on(*args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/acts-as-joinable.rb', line 30

def acts_as_joinable_on(*args, &block)
  args << block
  if joinable?
    write_inheritable_attribute(:acts_as_joinable_config, args)
  else
    write_inheritable_attribute(:acts_as_joinable_config, args)
    class_inheritable_reader(:acts_as_joinable_config)
    
    class_eval do
      def self.joinable?
        true
      end
      
      include ActsAsJoinable::Core
      include ActsAsJoinable::Dirty
    end
  end
end

#acts_as_relationshipObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/acts-as-joinable.rb', line 81

def acts_as_relationship
  belongs_to :parent, :polymorphic => true
  belongs_to :child,  :polymorphic => true
  
#      validates_uniqueness_of :parent_id, :scope => [:parent_type, :child_id]
#      validates_uniqueness_of :child_id, :scope => [:child_type, :parent_id]

#      ActsAsJoinable.models.each do |m|
#        belongs_to "parent_#{m}".intern, :foreign_key => 'parent_id', :class_name => m.camelize
#        belongs_to "child_#{m}".intern,  :foreign_key => 'child_id',  :class_name => m.camelize
#      end
end

#count_joins(options = {}) ⇒ Object

Group.count_joins(:as => :parent, :conditions)



50
51
52
53
54
55
56
# File 'lib/acts-as-joinable.rb', line 50

def count_joins(options = {})
  relationship  = options.delete(:as) || :parent
  conditions    = options[:conditions] || {}
  conditions["#{relationship.to_s}_type".to_sym] = ([self.name] + (self.send(:subclasses) - self.included_modules).map(&:name)).flatten.uniq
  group         = options[:group] || "#{relationship}_id"
  Relationship.count(:conditions => conditions, :group => group)
end

#joinableObject



62
63
64
# File 'lib/acts-as-joinable.rb', line 62

def joinable
  acts_as_joinable_on
end

#joinable?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/acts-as-joinable.rb', line 26

def joinable?
  false
end

#joins(*args, &block) ⇒ Object



70
71
72
# File 'lib/acts-as-joinable.rb', line 70

def joins(*args, &block)
  acts_as_joinable_on(*args, &block)
end

#joins_many(*args, &block) ⇒ Object



66
67
68
# File 'lib/acts-as-joinable.rb', line 66

def joins_many(*args, &block)
  acts_as_joinable_on(*args, &block)
end

#joins_one(*args, &block) ⇒ Object



74
75
76
77
78
79
# File 'lib/acts-as-joinable.rb', line 74

def joins_one(*args, &block)
  options = args.extract_options!
  options[:limit] = 1
  args << options
  acts_as_joinable_on(*args, &block)
end