Module: GroupedModel::ClassMethods

Defined in:
lib/grouped_model.rb

Instance Method Summary collapse

Instance Method Details

#has_groups(options = {}) ⇒ Object Also known as: has_group

Sets up group relations and scopes in this model. No extra columns are required in the model table.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/grouped_model.rb', line 34

def has_groups(options={})
  return if has_groups?
  
  class_eval {
    include GroupedModel::GroupedInstanceMethods

    def self.has_groups?
      true
    end
    
    def self.visible
      ungrouped
    end
  }
  
  has_many :permissions, :as => :permitted
  accepts_nested_attributes_for :permissions
  has_many :groups, :through => :permissions
  Group.define_retrieval_methods(self.to_s)

  named_scope :visible_to, lambda { |reader| 
    conditions = "pp.group_id IS NULL"
    if reader && reader.is_grouped?
      ids = reader.group_ids
      conditions = ["#{conditions} OR pp.group_id IS NULL OR pp.group_id IN(#{ids.map{"?"}.join(',')})", *ids]
    end
    {
      :joins => "LEFT OUTER JOIN permissions as pp on pp.permitted_id = #{self.table_name}.id AND pp.permitted_type = '#{self.to_s}'",
      :group => column_names.map { |n| self.table_name + '.' + n }.join(','),
      :conditions => conditions,
      :readonly => false
    } 
  }

  named_scope :ungrouped, lambda {
    {
      :select => "#{self.table_name}.*, count(pp.id) as group_count",
      :joins => "LEFT OUTER JOIN permissions as pp on pp.permitted_id = #{self.table_name}.id AND pp.permitted_type = '#{self.to_s}'", 
      :having => "group_count = 0",
      :group => column_names.map { |n| self.table_name + '.' + n }.join(','),    # postgres requires that we group by all selected (but not aggregated) columns
      :readonly => false
    }
  } do
    def count
      length
    end
  end

  named_scope :grouped, lambda {
      {
      :select => "#{self.table_name}.*, count(pp.id) as group_count",
      :joins => "LEFT OUTER JOIN permissions as pp on pp.permitted_id = #{self.table_name}.id AND pp.permitted_type = '#{self.to_s}'", 
      :having => "group_count > 0",
      :group => column_names.map { |n| self.table_name + '.' + n }.join(','),
      :readonly => false
    }
  } do
    def count
      length
    end
  end
  
  named_scope :belonging_to, lambda { |group|
    {
      :joins => "INNER JOIN permissions as pp on pp.permitted_id = #{self.table_name}.id AND pp.permitted_type = '#{self.to_s}'", 
      :group => column_names.map { |n| self.table_name + '.' + n }.join(','),
      :conditions => ["pp.group_id = ?", group.id],
      :readonly => false
    }
  }
  
  named_scope :find_these, lambda { |ids|
    ids = ['NULL'] unless ids && ids.any?
    { :conditions => ["#{self.table_name}.id IN (#{ids.map{"?"}.join(',')})", *ids] }
  }
        
end

#has_groups?Boolean Also known as: has_group?

Returns true if group relations have been established in this model.

Returns:

  • (Boolean)


27
28
29
# File 'lib/grouped_model.rb', line 27

def has_groups?
  false
end