From 78e32c78c8f58c6e45343f7f869b9e9678596415 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Mon, 9 Jul 2007 21:45:57 +0000 Subject: [PATCH] ActionItem3: adding boolean features support --- app/models/virtual_community.rb | 46 +++++++++++++++++++++++++++++++++++++--------- db/migrate/001_create_virtual_communities.rb | 1 - test/fixtures/virtual_communities.yml | 4 ++-- test/unit/virtual_community_test.rb | 13 ++++++++----- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/app/models/virtual_community.rb b/app/models/virtual_community.rb index 1484f84..5f15bd1 100644 --- a/app/models/virtual_community.rb +++ b/app/models/virtual_community.rb @@ -1,26 +1,54 @@ class VirtualCommunity < ActiveRecord::Base + # TODO: these are test features + EXISTING_FEATURES = { + 'feature1' => _('Feature 1'), + 'feature2' => _('Feature 2'), + 'feature3' => _('Feature 3'), + } + + # ################################################# + # Relationships and applied behaviour + # ################################################# + # One VirtualCommunity can be reached by many domains has_many :domains, :as => :owner # a VirtualCommunity can be configured acts_as_configurable + # ################################################# + # Attributes + # ################################################# + + # Enables a feature + def enable(feature) + self.settings["#{feature}_enabled"] = true + end + + # Disables a feature + def disable(feature) + self.settings["#{feature}_enabled"] = false + end + + # Tells if a feature is enabled + def enabled?(feature) + self.settings["#{feature}_enabled"] == true + end + + # ################################################# + # Validations + # ################################################# + # name is mandatory validates_presence_of :name # only one virtual community can be the default one 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') - # VirtualCommunity configuration - serialize :configuration, Hash - - # a Hash with configuration parameters for this community. The configuration - # contains general parameters of the VirtualCommunity as well as - # enabling/disabling optional features. - def configuration - self[:configuration] ||= Hash.new - end + # ################################################# + # Business logic in general + # ################################################# # the default VirtualCommunity. def self.default diff --git a/db/migrate/001_create_virtual_communities.rb b/db/migrate/001_create_virtual_communities.rb index bf86014..dadb366 100644 --- a/db/migrate/001_create_virtual_communities.rb +++ b/db/migrate/001_create_virtual_communities.rb @@ -2,7 +2,6 @@ class CreateVirtualCommunities < ActiveRecord::Migration def self.up create_table :virtual_communities do |t| t.column :name, :string - t.column :configuration, :text t.column :is_default, :boolean end ConfigurableSetting.create_table diff --git a/test/fixtures/virtual_communities.yml b/test/fixtures/virtual_communities.yml index 57a90aa..4228f5f 100644 --- a/test/fixtures/virtual_communities.yml +++ b/test/fixtures/virtual_communities.yml @@ -1,9 +1,9 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -first: +colivre_net: id: 1 name: 'Colivre.net' is_default: true -another: +anhetegua_net: id: 2 name: 'Anheteguá' is_default: false diff --git a/test/unit/virtual_community_test.rb b/test/unit/virtual_community_test.rb index 02a6cf6..3947192 100644 --- a/test/unit/virtual_community_test.rb +++ b/test/unit/virtual_community_test.rb @@ -3,11 +3,6 @@ require File.dirname(__FILE__) + '/../test_helper' class VirtualCommunityTest < Test::Unit::TestCase fixtures :virtual_communities - def test_configuration - vc = VirtualCommunity.new - assert_kind_of Hash, vc.configuration - end - def test_exists_default_and_it_is_unique VirtualCommunity.delete_all vc = VirtualCommunity.new(:name => 'Test Community') @@ -31,4 +26,12 @@ class VirtualCommunityTest < Test::Unit::TestCase assert_kind_of ConfigurableSetting, vc.settings.first end + def test_features + v = virtual_communities(:colivre_net) + v.enable('feature1') + assert v.enabled?('feature1') + v.disable('feature1') + assert !v.enabled?('feature1') + end + end -- libgit2 0.21.2