Module: Ixtlan::Models::Group

Defined in:
lib/ixtlan/models/group.rb

Constant Summary collapse

LOCALE =
Object.full_const_get(Models::LOCALE)
DOMAIN =
Object.full_const_get(Models::DOMAIN)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



11
12
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ixtlan/models/group.rb', line 11

def self.included(model)
    model.send(:include, DataMapper::Resource)
    model.send(:include, UpdateChildren)

    model.property :id, ::DataMapper::Types::Serial, :field => "gidnumber"

    model.property :name, String, :required => true , :format => /^[^<'&">]*$/, :length => 32, :field => "cn", :unique_index => true

    if !model.const_defined?('LDAP') || !model.const_get('LDAP')
      model.timestamps :created_at
      model.modified_by Ixtlan::Models::USER, :created_by
    end

    model.class_eval <<-EOS, __FILE__, __LINE__
  if protected_instance_methods.find {|m| m == 'to_x'}.nil?

    protected

    alias :to_x :to_xml_document
    def to_xml_document(opts, doc = nil)
      unless(opts[:methods])
        opts.merge!({
                      :skip_types => true,
                      :skip_empty_tags => true,
                      :methods => [:created_by], 
                      :created_by => {
                        :methods => [], 
                        :exclude => [:created_at, :updated_at, :hashed_password, :created_by_id, :updated_by_id, :language]
                      }
                    })
      end
      unless(opts[:exclude])
        opts.merge!({:exclude => [:created_by_id]})            
      end
      to_x(opts, doc)
    end
  end

  def self.first_or_get!(id_or_name)
    first(:name => id_or_name) || get!(id_or_name)
  end

  def self.first_or_get(id_or_name)
    first(:name => id_or_name) || get(id_or_name)
  end
EOS
end

Instance Method Details

#domains(user = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ixtlan/models/group.rb', line 96

def domains(user = nil)
  if @domains.nil? or not user.nil?

    # TODO spec the empty array to make sure new relations are stored
    # in the database or the domains collection is empty before filling it
    @domains = ::DataMapper::Collection.new(::DataMapper::Query.new(self.repository, DOMAIN), [])

    return @domains if user.nil?

    DomainGroupUser.all(:group_id => id, :user_id => user.id).each{ |glu| @domains << glu.domain }
    def @domains.group=(group)
      @group = group
    end
    @domains.group = self
    def @domains.user=(user)
      @user = user
    end
    @domains.user = user
    def @domains.<<(domain)
      unless member? domain
        DomainGroupUser.create(:group_id => @group.id, :user_id => @user.id, :domain_id => domain.id)
        super
      end

      self
    end
    def @domains.delete(domain)
      glu = DomainGroupUser.first(:group_id => @group.id, :user_id => @user.id, :domain_id => domain.id)
      if glu
        glu.destroy
      end
      super
    end
  end
  @domains
end

#domains_admin?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/ixtlan/models/group.rb', line 141

def domains_admin?
  attribute_get(:name) == 'domains'
end

#locales(user = nil) ⇒ Object



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
# File 'lib/ixtlan/models/group.rb', line 59

def locales(user = nil)
  if @locales.nil? or not user.nil?

    # TODO spec the empty array to make sure new relations are stored
    # in the database or the locales collection is empty before filling it
    @locales = ::DataMapper::Collection.new(::DataMapper::Query.new(self.repository, LOCALE), [])

    return @locales if user.nil?

    GroupLocaleUser.all(:group_id => id, :user_id => user.id).each{ |glu| @locales << glu.locale }
    def @locales.group=(group)
      @group = group
    end
    @locales.group = self
    def @locales.user=(user)
      @user = user
    end
    @locales.user = user
    def @locales.<<(locale)
      unless member? locale
        GroupLocaleUser.create(:group_id => @group.id, :user_id => @user.id, :locale_id => locale.id)
        super
      end

      self
    end
    def @locales.delete(locale)
      glu = GroupLocaleUser.first(:group_id => @group.id, :user_id => @user.id, :locale_id => locale.id)
      if glu
        glu.destroy
      end
      super
    end
  end
  @locales
end

#locales_admin?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/ixtlan/models/group.rb', line 137

def locales_admin?
  attribute_get(:name) == 'locales'
end

#root?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/ixtlan/models/group.rb', line 133

def root?
  attribute_get(:name) == 'root'
end