Commit 78e32c78c8f58c6e45343f7f869b9e9678596415
1 parent
50f70f55
Exists in
master
and in
22 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 | 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 | 14 | # One VirtualCommunity can be reached by many domains |
| 4 | 15 | has_many :domains, :as => :owner |
| 5 | 16 | |
| 6 | 17 | # a VirtualCommunity can be configured |
| 7 | 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 | 43 | # <tt>name</tt> is mandatory |
| 10 | 44 | validates_presence_of :name |
| 11 | 45 | |
| 12 | 46 | # only one virtual community can be the default one |
| 13 | 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 | 53 | # the default VirtualCommunity. |
| 26 | 54 | def self.default | ... | ... |
db/migrate/001_create_virtual_communities.rb
test/fixtures/virtual_communities.yml
test/unit/virtual_community_test.rb
| ... | ... | @@ -3,11 +3,6 @@ require File.dirname(__FILE__) + '/../test_helper' |
| 3 | 3 | class VirtualCommunityTest < Test::Unit::TestCase |
| 4 | 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 | 6 | def test_exists_default_and_it_is_unique |
| 12 | 7 | VirtualCommunity.delete_all |
| 13 | 8 | vc = VirtualCommunity.new(:name => 'Test Community') |
| ... | ... | @@ -31,4 +26,12 @@ class VirtualCommunityTest < Test::Unit::TestCase |
| 31 | 26 | assert_kind_of ConfigurableSetting, vc.settings.first |
| 32 | 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 | 37 | end | ... | ... |