lportal 1.0.9 → 1.0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/ChangeLog +71 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +6 -5
  4. data/Rakefile +23 -0
  5. data/init.rb +1 -1
  6. data/lib/acts/resourceful.rb +104 -0
  7. data/lib/company.rb +25 -0
  8. data/lib/group.rb +83 -29
  9. data/lib/journal/article.rb +8 -4
  10. data/lib/mb/category.rb +30 -59
  11. data/lib/mb/message.rb +24 -67
  12. data/lib/permission.rb +16 -0
  13. data/lib/release.rb +9 -0
  14. data/lib/resource.rb +19 -0
  15. data/lib/resource_code.rb +21 -1
  16. data/lib/role.rb +7 -27
  17. data/lib/tag/asset.rb +26 -4
  18. data/lib/user.rb +20 -23
  19. data/lib/web/layout.rb +134 -39
  20. data/lib/web/layout_set.rb +8 -4
  21. data/lib/web/portlet.rb +92 -20
  22. data/lib/web/portlet_preferences.rb +176 -13
  23. data/lib/web/portlet_properties.rb +32 -0
  24. data/lib/web/typesettings.rb +32 -7
  25. data/lportal.rb +30 -3
  26. data/migrations/20090101000001_add_sequences.rb +64 -0
  27. data/migrations/20090309000001_portlet_properties.rb +19 -0
  28. data/portlets.rb +76 -0
  29. data/schema.rb +34 -0
  30. data/test/unit/company_test.rb +21 -9
  31. data/test/unit/dl_file_test.rb +2 -1
  32. data/test/unit/group_test.rb +74 -16
  33. data/test/unit/journal/article_test.rb +15 -1
  34. data/test/unit/mb/category_test.rb +12 -15
  35. data/test/unit/mb/message_test.rb +3 -2
  36. data/test/unit/mb/thread_test.rb +1 -1
  37. data/test/unit/organization_test.rb +7 -2
  38. data/test/unit/phone_test.rb +2 -0
  39. data/test/unit/release_test.rb +25 -0
  40. data/test/unit/role_test.rb +7 -7
  41. data/test/unit/tag/asset_test.rb +59 -2
  42. data/test/unit/user_test.rb +73 -77
  43. data/test/unit/web/layout_set_test.rb +18 -1
  44. data/test/unit/web/layout_test.rb +191 -5
  45. data/test/unit/web/portlet_preferences_test.rb +314 -17
  46. data/test/unit/web/portlet_properties_test.rb +36 -0
  47. data/test/unit/web/portlet_test.rb +121 -19
  48. data/test/unit/web/typesettings_test.rb +24 -11
  49. data/version.rb +1 -2
  50. metadata +20 -6
  51. data/install.rb +0 -1
data/ChangeLog ADDED
@@ -0,0 +1,71 @@
1
+ = 1.0.17
2
+ - introduced schema versions, automatic detection from the release_ table
3
+ - migrations imported from caterpillar trunk, define the migrations directory for caterpillar
4
+ - Group#asset_viewer_portlet(), the asset viewer selection is based on the schema version
5
+ - Liferay portlet path definitions (tagged_content, asset_publisher)
6
+ - Journal::Article#path(), Tag::Asset#path()
7
+ - fixed journal article link
8
+ - Web::PortletPreferences and Web::Portlet: cleanup
9
+ - DRY up various models with Acts::Resourceful
10
+ - api change; layout.contents -> layout.settings
11
+ - api change; user.user_permissions -> user.permissions
12
+ - self.get methods to ResourceCode, Resource and Permission
13
+ - Typesettings: accept Strings and Portlet* models
14
+ - Company: methods to find various groups by type
15
+
16
+
17
+ = 1.0.9
18
+ - creation of MB::Messages
19
+ - new models: SocialActivity, SocialRelation, RatingsStats, MB::MessageFlag, MB::StatsUser
20
+ - URL path to download Document Library File
21
+ - helper methods to find various types of groups and groups assets tags
22
+
23
+
24
+ = 1.0.8
25
+ - Uniqueness scopes in User
26
+ - Group members taken from Organization, if Group type if Organization
27
+ - Role creation and deletion
28
+ - Group creation
29
+ - Added a method to clean Phone number
30
+ - Web::Layout creation
31
+ - Typesettings model for Layout content
32
+ - MB::Category creation, MB::Category.path
33
+ - User.activate, User.portrait_path, User.path
34
+ - Group.name returns the name of Group's owner, if classnameid != 0
35
+ - Layout portlets helper methods
36
+ - fixed Tag::Asset relations
37
+
38
+ Known bugs:
39
+ - Typesettings does not parse all existing Layouts correctly!
40
+
41
+
42
+ = 1.0.7
43
+ - User creation
44
+ - new models: Listtype, Announcement::Delivery
45
+ - Counter increment method
46
+ - better creation of Phone, Contact and Group
47
+
48
+
49
+ = 1.0.6
50
+ - fixed a bug in user.gsm= method
51
+ - portlet names
52
+ - Company's guest
53
+ - layouts and various Liferay URL handling
54
+
55
+
56
+ = 1.0.5
57
+ - Group types
58
+
59
+
60
+ = 1.0.4
61
+ - Saving user's birthday through User.birthday=
62
+ - New relations
63
+ - New models
64
+ - More tests!
65
+
66
+
67
+ = 1.0.0
68
+ Initial release on 18.11.2008.
69
+
70
+ The structure is usable (though not complete), so tagged as version 1.0.0.
71
+
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008,2009 Mikael Lammentausta
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README CHANGED
@@ -61,20 +61,21 @@ You can view this page in the Liferay "Communities" portlet via "Available Commu
61
61
  === Portlets and Caterpillar
62
62
  To add new portlets the Caterpillar mixins and migrations are required. This data does not live in the lportal database, and this library does not understand about the environment in which Liferay is. Caterpillar, however, does. It parses some Liferay XML configuration files and makes the data available by defining a couple of new methods to classes in the Web module.
63
63
 
64
- The Layout contents are defined in its typesettings. The Web::Typesettings class makes their definitions more Ruby-like. To add a message_boards portlet into column 2:
64
+ The Layout settings are configured via Web::Typesettings. The ”settings” method handles the conversion between the ”typesettings” string and the object model. To add a message_boards portlet into column 2:
65
65
 
66
- layout.typesettings = Web::Typesettings.new.message_boards(:column => 2).to_s
66
+ layout.settings = Web::Typesettings.new.message_boards(:column => 2)
67
67
 
68
68
  => "layout-template-id=2_columns_ii\ncolumn-2=19,"
69
69
 
70
70
 
71
71
  The other way around, find the private layouts of "group" that have the Message Board portlet:
72
72
 
73
- layouts = group.private_layouts.select{|l| l.contents.include?('message_boards')}
73
+ layouts = group.select_layouts_with('message_boards',:private)
74
74
 
75
- Or, using a method designed for this:
75
+ Which is a short form for:
76
+
77
+ layouts = group.private_layouts.select{|l| l.settings.include?('message_boards')}
76
78
 
77
- layouts = group.select_layouts_with('message_boards',:private)
78
79
  => [#<Web::Layout plid: 8400000, groupid: 8400004, companyid: 10109, privatelayout: true, layoutid: 1, parentlayoutid: 0, name: "<?xml version='1.0' encoding='UTF-8'?><root availab...", title: "<root />", description: "", type_: "portlet", typesettings: "layout-template-id=2_columns_ii\ncolumn-2=19,", hidden_: false, friendlyurl: "/1", iconimage: false, iconimageid: 0, themeid: nil, colorschemeid: nil, wapthemeid: nil, wapcolorschemeid: nil, css: nil, priority: 0, dlfolderid: 0>]
79
80
 
80
81
  Or query what portlets are in the Layout:
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the liferay plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the liferay plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Liferay ActiveRecord models'
19
+ rdoc.rdoc_files.include('README')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ rdoc.options << '-U' << '-x deprecated'
22
+ rdoc.options << '--line-numbers' << '--inline-source'
23
+ end
data/init.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  file = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
2
2
  this_dir = File.dirname(File.expand_path(file))
3
3
 
4
- require File.join(this_dir,'lportal')
4
+ require File.join(this_dir,'lportal')
@@ -0,0 +1,104 @@
1
+ module Acts #:nodoc:
2
+
3
+ # This +acts_as+ extension provides the capabilities for creating +ResourceCode+s, +Resource+s
4
+ # and +Permission+s.
5
+ #
6
+ # The model needs to have defined their +liferay_class+, +companyid+, and for scope 2 resources,
7
+ # their +groupid+. These functions may be overridden in the model itself.
8
+ module Resourceful
9
+ def self.included(base)
10
+ base.extend(ClassMethods)
11
+ end
12
+
13
+ module ClassMethods
14
+ def acts_as_resourceful(options = {})
15
+
16
+ class_eval <<-EOV
17
+ include Acts::Resourceful::InstanceMethods
18
+
19
+ def acts_as_resourceful_class
20
+ ::#{self.name}
21
+ end
22
+ EOV
23
+ end
24
+ end
25
+
26
+ # All the methods available to a record that has had <tt>acts_as_resourceful</tt> specified.
27
+ module InstanceMethods
28
+
29
+ # ResourceCode associated to this instance (and scope)
30
+ def resource_code(scope)
31
+ ResourceCode.get({
32
+ :companyid => self.companyid,
33
+ :name => self.liferay_class,
34
+ :scope => scope
35
+ })
36
+ end
37
+
38
+ # Default primkey in scope 1 is +self.companyid+. Override in the model if necessary.
39
+ def scope1_primkey
40
+ self.companyid
41
+ end
42
+
43
+ # Default primkey in scope 2 is +self.groupid+. Override in the model if necessary.
44
+ def scope2_primkey
45
+ raise ('Instance %s does not belong to a group' % self) unless self.group
46
+ self.groupid
47
+ end
48
+
49
+ # Default primkey in scope 4 is +self.id+. Override in the model if necessary.
50
+ def scope4_primkey
51
+ self.id
52
+ end
53
+
54
+ # resource by codeid
55
+ def resource(rc)
56
+ case rc.scope
57
+ when 1
58
+ primkey = self.scope1_primkey
59
+ when 2
60
+ primkey = self.scope2_primkey
61
+ when 4
62
+ primkey = self.scope4_primkey
63
+ else
64
+ raise 'unknown scope'
65
+ end
66
+ if primkey.nil?
67
+ logger.warn 'No primkey'
68
+ return nil
69
+ end
70
+ logger.debug 'primkey: %s' % primkey
71
+ Resource.get({
72
+ :codeid => rc.codeid,
73
+ :primkey => primkey
74
+ })
75
+ end
76
+
77
+ # When creating new instances, it is common to find a resource code, and a resource that matches the code.
78
+ # If they cannot be found from the database, they are created.
79
+ # This method takes care of all that.
80
+ def get_resource(args)
81
+ scope = args[:scope]
82
+ raise 'No scope' unless scope
83
+ logger.debug 'Get resource for %s, scope %i' % [self.liferay_class, scope]
84
+ rc = self.resource_code(scope)
85
+ logger.debug rc.inspect
86
+ r = self.resource(rc)
87
+ logger.debug r.inspect
88
+ return r
89
+ end
90
+
91
+ # Finds or creates a new Permission (and Resource + ResourceCode) for a given action
92
+ # and a specific scope.
93
+ def permission(actionid, scope)
94
+ resource = get_resource(:scope => scope)
95
+ Permission.get(
96
+ :companyid => self.companyid,
97
+ :actionid => actionid,
98
+ :resourceid => resource.id
99
+ )
100
+ end
101
+
102
+ end
103
+ end
104
+ end
data/lib/company.rb CHANGED
@@ -44,6 +44,31 @@ class Company < ActiveRecord::Base
44
44
  User.find(:first, :conditions => "companyid=#{self.id} AND defaultuser = true" )
45
45
  end
46
46
 
47
+ # Oddly enough, guest user's does not have a personal group relation in 5.1.1 nor 5.2.1
48
+ def guest_group
49
+ Group.find(:first, :conditions => "companyid=#{self.id} AND name='Guest'")
50
+ end
51
+
52
+ # 0 = personal user groups
53
+ def personal_groups
54
+ Group.find_personal self.id
55
+ end
56
+
57
+ # 1 = public, guest can see
58
+ def public_groups
59
+ Group.find_public self.id
60
+ end
61
+
62
+ # 2 = user-created open communities
63
+ def open_groups
64
+ Group.find_open self.id
65
+ end
66
+
67
+ # 3 = private, also contains admins
68
+ def private_groups
69
+ Group.find_private self.id
70
+ end
71
+
47
72
  # has_one :frontpage,
48
73
  # :class_name => "Web::LayoutSet",
49
74
  # :foreign_key => "companyid",
data/lib/group.rb CHANGED
@@ -3,6 +3,8 @@ class Group < ActiveRecord::Base
3
3
  set_table_name :group_
4
4
  set_primary_key :groupid
5
5
 
6
+ acts_as_resourceful
7
+
6
8
  public
7
9
 
8
10
  # com.liferay.portal.model.Group
@@ -100,18 +102,7 @@ class Group < ActiveRecord::Base
100
102
  # +41 6 10155
101
103
  #
102
104
  # Create a resource with scope=4 for this Layout.
103
- rc = self.resource_code(4)
104
- unless rc
105
- rc = ResourceCode.create(
106
- :companyid => self.companyid,
107
- :name => self.liferay_class,
108
- :scope => 4
109
- )
110
- end
111
- r = Resource.create(
112
- :codeid => rc.id,
113
- :primkey => self.id
114
- )
105
+ r = get_resource(:scope => 4)
115
106
 
116
107
  # Permissions (given to administrators)
117
108
 
@@ -131,13 +122,13 @@ class Group < ActiveRecord::Base
131
122
  #
132
123
 
133
124
  self.class.actions.each do |actionid|
134
- p = Permission.create(
125
+ p = Permission.get(
135
126
  :companyid => self.companyid,
136
127
  :actionid => actionid,
137
128
  :resourceid => r.id
138
129
  )
139
130
  self.company.administrators.each do |user|
140
- user.user_permissions << p
131
+ user.permissions << p
141
132
  end
142
133
  end
143
134
  end
@@ -192,6 +183,10 @@ class Group < ActiveRecord::Base
192
183
  :class_name => 'Tag::Asset',
193
184
  :foreign_key => 'groupid'
194
185
 
186
+ has_many :mbcategories,
187
+ :class_name => 'MB::Category',
188
+ :foreign_key => 'groupid'
189
+
195
190
  # a group may have two layoutsets (public and private)
196
191
  has_many :layoutsets,
197
192
  :class_name => 'Web::LayoutSet',
@@ -224,7 +219,7 @@ class Group < ActiveRecord::Base
224
219
 
225
220
 
226
221
  # --
227
- # Class methods
222
+ # Class methods, in functional paradigm
228
223
  # ++
229
224
 
230
225
  class << self
@@ -292,9 +287,6 @@ class Group < ActiveRecord::Base
292
287
  end
293
288
 
294
289
 
295
- has_one :resource,
296
- :foreign_key => 'primkey'
297
-
298
290
  has_many :mbcategories,
299
291
  :class_name => 'MB::Category',
300
292
  :foreign_key => 'groupid'
@@ -303,6 +295,10 @@ class Group < ActiveRecord::Base
303
295
  self.active_
304
296
  end
305
297
 
298
+ def is_personal?
299
+ self.type_ == 0
300
+ end
301
+
306
302
  def is_public?
307
303
  self.type_ == 1
308
304
  end
@@ -320,28 +316,26 @@ class Group < ActiveRecord::Base
320
316
  case pl
321
317
  when :public
322
318
  self.public_layouts.any? ?
323
- self.public_layoutset.url_prefix + self.friendlyurl : nil
319
+ self.public_layoutset.url_prefix + self.friendlyurl : ''
324
320
 
325
321
  when :private
326
322
  self.private_layouts.any? ?
327
- self.private_layoutset.url_prefix + self.friendlyurl : nil
323
+ self.private_layoutset.url_prefix + self.friendlyurl : ''
328
324
 
329
325
  else
330
- nil
326
+ ''
331
327
  end
332
328
  end
333
329
 
334
- # ResourceCode associated to this instance (and scope)
335
- def resource_code(scope=4)
336
- ResourceCode.find(:first,
337
- :conditions => "companyid=#{self.companyid} AND name='#{self.liferay_class}' AND scope=#{scope}")
338
- end
339
-
340
330
  # Selects the layout (from both public and private) that has the portlet.
331
+ #
341
332
  # Parameters:
342
- # - portlet can be either a String of portlet name (eg. 'message_boards') or a Web::PortletName instance.
333
+ # - portlet can be either a String of portlet name (eg. 'message_boards'),
334
+ # or a Web::Portlet, Web::PortletPreferences or Web::PortletProperties instance.
343
335
  # - pl only check public or private layouts? defaults to both. ( nil | :public | :private )
336
+ #
344
337
  def select_layouts_with(portlet,pl=nil)
338
+ name = (portlet.is_a?(String) ? portlet : portlet.name)
345
339
  layouts = (
346
340
  if pl.nil?
347
341
  self.layouts
@@ -354,7 +348,10 @@ class Group < ActiveRecord::Base
354
348
  end
355
349
  )
356
350
 
357
- layouts.select{|l| l.contents.include?(portlet)}
351
+ # layouts.select{|l| l.settings.include?(portlet)}
352
+ layouts.select do |l|
353
+ l.portlets.map {|p| p.name==name}.any?
354
+ end
358
355
  end
359
356
 
360
357
  # Does any of the layouts include this portlet? See #select_layouts_with
@@ -367,4 +364,61 @@ class Group < ActiveRecord::Base
367
364
  self.assets.collect(&:tags).flatten.uniq
368
365
  end
369
366
 
367
+ # Unless the tagged_content portlet is found in any of the groups layouts,
368
+ # a new layout (public by default) is created with this portlet.
369
+ #
370
+ # See #select_layouts_with
371
+ #
372
+ # The asset viewer is different in different versions of the Liferay database.
373
+ # - 5.1.x: tagged_content
374
+ # - 5.2.x: asset_publisher
375
+ def asset_viewer_portlet(params={})
376
+ if Lportal::Schema.buildnumber < 5200
377
+ logger.debug 'Using lportal schema 5.1.x'
378
+ params[:name] ||= 'tagged_content'
379
+ else
380
+ logger.debug 'Using lportal schema 5.2.x'
381
+ params[:name] ||= 'asset_publisher'
382
+ end
383
+
384
+ portlet = Web::Portlet.find_by_name(params[:name])
385
+ raise ('Portlet ”%s” not found! Check that Caterpillar migrations are up-to-date' % params[:name]) unless portlet
386
+
387
+ # this says: if <tt>pl</tt> is undefined, use public layout, else use what <tt>pl</tt> specified.
388
+ params.update(:privatelayout => ( params[:privatelayout].nil? ? false : params[:privatelayout] ))
389
+
390
+ pl = (params[:privatelayout] ? :private : :public) # a small "type conversion"
391
+ layouts = self.select_layouts_with(portlet,pl)
392
+
393
+ ### Use existing layout and portlet
394
+ if layouts.any?
395
+ logger.debug 'Layout with tagged_content portlet found..'
396
+ layout = layouts.first
397
+ layout.portlets.each do |p|
398
+ return p if p.name==portlet.name
399
+ end
400
+ end
401
+
402
+ ### Create Layout + PortletPreferences
403
+ logger.info 'Creating new Layout ”%s” in Group %s (%i)' % [params[:name],self.name,self.id]
404
+
405
+ params[:friendlyurl] ||= ('/%s' % params[:name])
406
+ params.update(:group => self)
407
+
408
+ layout = Web::Layout.create(params)
409
+ logger.debug 'Layout ready'
410
+ logger.debug layout.inspect
411
+
412
+ layout.settings = Web::Typesettings.new
413
+ layout.columns=1
414
+
415
+ # add the portlet to the layout
416
+ layout.<<( portlet )
417
+ layout.save
418
+
419
+ portlet.reload
420
+ logger.debug portlet.preferences.inspect
421
+ return portlet.preferences
422
+ end
423
+
370
424
  end
@@ -27,16 +27,20 @@ module Journal
27
27
  self.resource.asset
28
28
  end
29
29
 
30
- has_many :properties,
31
- :class_name => 'TagsProperty',
32
- :foreign_key => 'entryid'
33
-
30
+ # has_many :properties,
31
+ # :class_name => 'TagsProperty',
32
+ # :foreign_key => 'entryid'
33
+ #
34
34
  # # association to assets
35
35
  # has_and_belongs_to_many :assets,
36
36
  # :join_table => 'tagsassets_tagsentries',
37
37
  # :foreign_key => 'entryid',
38
38
  # :association_foreign_key => 'assetid'
39
39
 
40
+ # Path to this article. See Tag::Asset#path.
41
+ def path
42
+ self.asset.path(self.articleid)
43
+ end
40
44
 
41
45
  end
42
46
 
data/lib/mb/category.rb CHANGED
@@ -4,6 +4,8 @@ module MB
4
4
  set_table_name :mbcategory
5
5
  set_primary_key :categoryid
6
6
 
7
+ acts_as_resourceful
8
+
7
9
  # this causes trouble when modifying instance attributes (!)
8
10
  #validates_uniqueness_of :uuid_
9
11
 
@@ -81,52 +83,6 @@ module MB
81
83
  # +212 105 10166
82
84
  # +213 106 10307
83
85
 
84
- rc = self.resource_code(1)
85
- unless rc
86
- rc = ResourceCode.create(
87
- :companyid => self.companyid,
88
- :name => self.liferay_class,
89
- :scope => 1
90
- )
91
- end
92
- unless Resource.find(:first, :conditions => "codeid=#{rc.id} AND primkey='#{self.companyid}'")
93
- Resource.create(
94
- :codeid => rc.id,
95
- :primkey => self.companyid
96
- )
97
- end
98
-
99
- rc = self.resource_code(2)
100
- unless rc
101
- rc = ResourceCode.create(
102
- :companyid => self.companyid,
103
- :name => self.liferay_class,
104
- :scope => 2
105
- )
106
- end
107
- unless Resource.find(:first, :conditions => "codeid=#{rc.id} AND primkey='#{self.group.id}'")
108
- Resource.create(
109
- :codeid => rc.id,
110
- :primkey => self.group.id
111
- )
112
- end
113
-
114
- # Create a resource with scope=4 for this Layout.
115
- rc = self.resource_code(4)
116
- unless rc
117
- rc = ResourceCode.create(
118
- :companyid => self.companyid,
119
- :name => self.liferay_class,
120
- :scope => 4
121
- )
122
- end
123
- resource = Resource.create(
124
- :codeid => rc.id,
125
- :primkey => self.id
126
- )
127
-
128
- # Permissions to scope 4
129
-
130
86
  # COPY permission_ (permissionid, companyid, actionid, resourceid) FROM stdin;
131
87
  # +312 10109 ADD_FILE 213
132
88
  # +313 10109 ADD_MESSAGE 213
@@ -153,13 +109,33 @@ module MB
153
109
  # +10129 321
154
110
  # +10129 322
155
111
 
112
+ get_resource(:scope => 1)
113
+ get_resource(:scope => 2)
114
+
115
+ # Permissions to scope 4
116
+
156
117
  self.class.actions.each do |actionid|
157
- self.user.user_permissions << Permission.create(
158
- :companyid => self.companyid,
159
- :actionid => actionid,
160
- :resourceid => resource.id
161
- )
118
+ self.user.permissions << permission(actionid,4)
119
+ end
120
+ end
121
+
122
+ # Finds existing or creates a new Resource (TODO: DRY up!!)
123
+ # Args Hash:
124
+ # - :codeid
125
+ # - :primkey
126
+ def self.get(args)
127
+ conditions = []
128
+ args.each_pair{ |k,v|
129
+ conditions << (k==:name ?
130
+ ("%s='%s'" % [k,v]) : ("%s=%s" % [k,v]))
131
+ }
132
+ #puts conditions.inspect
133
+ r = self.find(:first, :conditions => conditions.join(' AND '))
134
+ unless r
135
+ logger.debug 'creating new %s' % self.class
136
+ r = self.create(args)
162
137
  end
138
+ return r
163
139
  end
164
140
 
165
141
  belongs_to :group,
@@ -189,19 +165,14 @@ module MB
189
165
  :foreign_key => :parentcategoryid
190
166
 
191
167
  has_many :threads,
192
- :class_name => 'MB::Thread',
193
- :foreign_key => 'categoryid'
168
+ :class_name => 'MB::Thread',
169
+ :foreign_key => 'categoryid',
170
+ :order => 'lastpostdate DESC'
194
171
 
195
172
  has_many :messages,
196
173
  :class_name => 'MB::Message',
197
174
  :foreign_key => 'categoryid'
198
175
 
199
- # ResourceCode associated to this instance (and scope)
200
- def resource_code(scope=4)
201
- ResourceCode.find(:first,
202
- :conditions => "companyid=#{self.companyid} AND name='#{self.liferay_class}' AND scope=#{scope}")
203
- end
204
-
205
176
  # URL path to this Category. Works on Liferay 5.1.1.
206
177
  # Parameters:
207
178
  # - pl only check public or private layouts? defaults to both. ( nil | :public | :private )