Commit 78e32c78c8f58c6e45343f7f869b9e9678596415
1 parent
50f70f55
Exists in
master
and in
29 other branches
ActionItem3: adding boolean features support
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@55 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
4 changed files
with
47 additions
and
17 deletions
Show diff stats
app/models/virtual_community.rb
1 | class VirtualCommunity < ActiveRecord::Base | 1 | class VirtualCommunity < ActiveRecord::Base |
2 | 2 | ||
3 | + # TODO: these are test features | ||
4 | + EXISTING_FEATURES = { | ||
5 | + 'feature1' => _('Feature 1'), | ||
6 | + 'feature2' => _('Feature 2'), | ||
7 | + 'feature3' => _('Feature 3'), | ||
8 | + } | ||
9 | + | ||
10 | + # ################################################# | ||
11 | + # Relationships and applied behaviour | ||
12 | + # ################################################# | ||
13 | + | ||
3 | # One VirtualCommunity can be reached by many domains | 14 | # One VirtualCommunity can be reached by many domains |
4 | has_many :domains, :as => :owner | 15 | has_many :domains, :as => :owner |
5 | 16 | ||
6 | # a VirtualCommunity can be configured | 17 | # a VirtualCommunity can be configured |
7 | acts_as_configurable | 18 | acts_as_configurable |
8 | 19 | ||
20 | + # ################################################# | ||
21 | + # Attributes | ||
22 | + # ################################################# | ||
23 | + | ||
24 | + # Enables a feature | ||
25 | + def enable(feature) | ||
26 | + self.settings["#{feature}_enabled"] = true | ||
27 | + end | ||
28 | + | ||
29 | + # Disables a feature | ||
30 | + def disable(feature) | ||
31 | + self.settings["#{feature}_enabled"] = false | ||
32 | + end | ||
33 | + | ||
34 | + # Tells if a feature is enabled | ||
35 | + def enabled?(feature) | ||
36 | + self.settings["#{feature}_enabled"] == true | ||
37 | + end | ||
38 | + | ||
39 | + # ################################################# | ||
40 | + # Validations | ||
41 | + # ################################################# | ||
42 | + | ||
9 | # <tt>name</tt> is mandatory | 43 | # <tt>name</tt> is mandatory |
10 | validates_presence_of :name | 44 | validates_presence_of :name |
11 | 45 | ||
12 | # only one virtual community can be the default one | 46 | # only one virtual community can be the default one |
13 | validates_uniqueness_of :is_default, :if => (lambda do |virtual_community| virtual_community.is_default? end), :message => _('Only one Virtual Community can be the default one') | 47 | validates_uniqueness_of :is_default, :if => (lambda do |virtual_community| virtual_community.is_default? end), :message => _('Only one Virtual Community can be the default one') |
14 | 48 | ||
15 | - # VirtualCommunity configuration | ||
16 | - serialize :configuration, Hash | ||
17 | - | ||
18 | - # a Hash with configuration parameters for this community. The configuration | ||
19 | - # contains general parameters of the VirtualCommunity as well as | ||
20 | - # enabling/disabling optional features. | ||
21 | - def configuration | ||
22 | - self[:configuration] ||= Hash.new | ||
23 | - end | 49 | + # ################################################# |
50 | + # Business logic in general | ||
51 | + # ################################################# | ||
24 | 52 | ||
25 | # the default VirtualCommunity. | 53 | # the default VirtualCommunity. |
26 | def self.default | 54 | def self.default |
db/migrate/001_create_virtual_communities.rb
@@ -2,7 +2,6 @@ class CreateVirtualCommunities < ActiveRecord::Migration | @@ -2,7 +2,6 @@ class CreateVirtualCommunities < ActiveRecord::Migration | ||
2 | def self.up | 2 | def self.up |
3 | create_table :virtual_communities do |t| | 3 | create_table :virtual_communities do |t| |
4 | t.column :name, :string | 4 | t.column :name, :string |
5 | - t.column :configuration, :text | ||
6 | t.column :is_default, :boolean | 5 | t.column :is_default, :boolean |
7 | end | 6 | end |
8 | ConfigurableSetting.create_table | 7 | ConfigurableSetting.create_table |
test/fixtures/virtual_communities.yml
1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | 1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html |
2 | -first: | 2 | +colivre_net: |
3 | id: 1 | 3 | id: 1 |
4 | name: 'Colivre.net' | 4 | name: 'Colivre.net' |
5 | is_default: true | 5 | is_default: true |
6 | -another: | 6 | +anhetegua_net: |
7 | id: 2 | 7 | id: 2 |
8 | name: 'Anheteguá' | 8 | name: 'Anheteguá' |
9 | is_default: false | 9 | is_default: false |
test/unit/virtual_community_test.rb
@@ -3,11 +3,6 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -3,11 +3,6 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
3 | class VirtualCommunityTest < Test::Unit::TestCase | 3 | class VirtualCommunityTest < Test::Unit::TestCase |
4 | fixtures :virtual_communities | 4 | fixtures :virtual_communities |
5 | 5 | ||
6 | - def test_configuration | ||
7 | - vc = VirtualCommunity.new | ||
8 | - assert_kind_of Hash, vc.configuration | ||
9 | - end | ||
10 | - | ||
11 | def test_exists_default_and_it_is_unique | 6 | def test_exists_default_and_it_is_unique |
12 | VirtualCommunity.delete_all | 7 | VirtualCommunity.delete_all |
13 | vc = VirtualCommunity.new(:name => 'Test Community') | 8 | vc = VirtualCommunity.new(:name => 'Test Community') |
@@ -31,4 +26,12 @@ class VirtualCommunityTest < Test::Unit::TestCase | @@ -31,4 +26,12 @@ class VirtualCommunityTest < Test::Unit::TestCase | ||
31 | assert_kind_of ConfigurableSetting, vc.settings.first | 26 | assert_kind_of ConfigurableSetting, vc.settings.first |
32 | end | 27 | end |
33 | 28 | ||
29 | + def test_features | ||
30 | + v = virtual_communities(:colivre_net) | ||
31 | + v.enable('feature1') | ||
32 | + assert v.enabled?('feature1') | ||
33 | + v.disable('feature1') | ||
34 | + assert !v.enabled?('feature1') | ||
35 | + end | ||
36 | + | ||
34 | end | 37 | end |