diff --git a/app/controllers/application.rb b/app/controllers/application.rb
index ec07673..3452815 100644
--- a/app/controllers/application.rb
+++ b/app/controllers/application.rb
@@ -42,6 +42,7 @@ class ApplicationController < ActionController::Base
protected
+ # TODO: move this logic somewhere else (Domain class?)
def detect_stuff_by_domain
@domain = Domain.find_by_name(request.host)
if @domain.nil?
diff --git a/app/controllers/features_controller.rb b/app/controllers/features_controller.rb
index dc98684..e931fe7 100644
--- a/app/controllers/features_controller.rb
+++ b/app/controllers/features_controller.rb
@@ -2,6 +2,14 @@ class FeaturesController < ApplicationController
acts_as_virtual_community_admin_controller
def index
- @features = VirtualCommunity::EXISTING_FEATURES
+ @features = VirtualCommunity.available_features
end
+
+ def update
+ @virtual_community.enabled_features = params[:features].keys
+ @virtual_community.save!
+ flash[:notice] = _('Features updated successfully.')
+ redirect_to :action => 'index'
+ end
+
end
diff --git a/app/models/virtual_community.rb b/app/models/virtual_community.rb
index 5f53239..a9b0d6b 100644
--- a/app/models/virtual_community.rb
+++ b/app/models/virtual_community.rb
@@ -1,11 +1,13 @@
class VirtualCommunity < ActiveRecord::Base
- # TODO: these are test features
- EXISTING_FEATURES = {
- 'feature1' => _('Enable Feature 1'),
- 'feature2' => _('Enable Feature 2'),
- 'feature3' => _('Enable Feature 3'),
- }
+ # returns the available features for a VirtualCommunity, in the form of a
+ # hash, with pairs in the form 'feature_name' => 'Feature name'.
+ def self.available_features
+ {
+ 'some_feature' => _('Some feature'),
+ 'other_feature' => _('Other feature'),
+ }
+ end
# #################################################
# Relationships and applied behaviour
@@ -35,6 +37,16 @@ class VirtualCommunity < ActiveRecord::Base
def enabled?(feature)
self.settings["#{feature}_enabled"] == true
end
+
+ def enabled_features=(features)
+ self.class.available_features.keys.each do |feature|
+ if features.include? feature
+ self.enable(feature)
+ else
+ self.disable(feature)
+ end
+ end
+ end
# #################################################
# Validations
diff --git a/app/views/features/_features_table.rhtml b/app/views/features/_features_table.rhtml
new file mode 100644
index 0000000..0529985
--- /dev/null
+++ b/app/views/features/_features_table.rhtml
@@ -0,0 +1,11 @@
+
+ <% form_tag({:action => 'update'}) do %>
+ <% @features.each do |feature, text| %>
+
+ <%= text %> |
+ <%= check_box_tag "features[#{feature}]", '1', @virtual_community.enabled?(feature) %> |
+
+ <% end %>
+ <%= submit_tag %> |
+ <% end %>
+
diff --git a/app/views/features/index.rhtml b/app/views/features/index.rhtml
index 4fff680..1dfefb1 100644
--- a/app/views/features/index.rhtml
+++ b/app/views/features/index.rhtml
@@ -1,13 +1,3 @@
<%= _('Enable/Disable features') %>
-
- <% form_tag({:action => 'update_features'}) do %>
- <% @features.each do |feature, text| %>
-
- <%= text %> |
- <%= check_box_tag "feature[#{feature}]" %> |
-
- <% end %>
- <%= submit_tag %> |
- <% end %>
-
+<%= render :partial => 'features_table' %>
diff --git a/test/functional/features_controller_test.rb b/test/functional/features_controller_test.rb
index d6221ed..25ee0fe 100644
--- a/test/functional/features_controller_test.rb
+++ b/test/functional/features_controller_test.rb
@@ -5,23 +5,30 @@ require 'features_controller'
class FeaturesController; def rescue_action(e) raise e end; end
class FeaturesControllerTest < Test::Unit::TestCase
+
+ fixtures :virtual_communities
+
def setup
@controller = FeaturesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
+ uses_host 'anhetegua.net'
end
def test_listing_features
get :index
assert_template 'index'
- VirtualCommunity::EXISTING_FEATURES.each do |feature, text|
- assert_tag(:tag => 'input', :attributes => { :type => 'checkbox', :name => "feature[#{feature}]" })
+ VirtualCommunity.available_features.each do |feature, text|
+ assert_tag(:tag => 'input', :attributes => { :type => 'checkbox', :name => "features[#{feature}]" })
end
end
def test_update_features
- # flunk 'Not implemented yet'
- assert true
+ get :update, :features => { 'feature1' => '1', 'feature2' => '1' }
+ assert_redirected_to :action => 'index'
+ assert_kind_of String, flash[:notice]
+ v = VirtualCommunity.find(virtual_communities(:colivre_net).id)
+ assert v.enabled?('feature1') && v.enabled?('feature2') && !v.enabled?('feature3')
end
end
diff --git a/test/mocks/test/virtual_community.rb b/test/mocks/test/virtual_community.rb
new file mode 100644
index 0000000..db23b5f
--- /dev/null
+++ b/test/mocks/test/virtual_community.rb
@@ -0,0 +1,11 @@
+require 'app/models/virtual_community'
+
+class VirtualCommunity < ActiveRecord::Base
+ def self.available_features
+ {
+ 'feature1' => 'Enable Feature 1',
+ 'feature2' => 'Enable Feature 2',
+ 'feature3' => 'Enable Feature 3',
+ }
+ end
+end
diff --git a/test/unit/virtual_community_test.rb b/test/unit/virtual_community_test.rb
index 3947192..6a57ad1 100644
--- a/test/unit/virtual_community_test.rb
+++ b/test/unit/virtual_community_test.rb
@@ -26,6 +26,10 @@ class VirtualCommunityTest < Test::Unit::TestCase
assert_kind_of ConfigurableSetting, vc.settings.first
end
+ def test_available_features
+ assert_kind_of Hash, VirtualCommunity.available_features
+ end
+
def test_features
v = virtual_communities(:colivre_net)
v.enable('feature1')
@@ -34,4 +38,19 @@ class VirtualCommunityTest < Test::Unit::TestCase
assert !v.enabled?('feature1')
end
+ def test_enabled_features
+ v = virtual_communities(:colivre_net)
+ v.enabled_features = [ 'feature1', 'feature2' ]
+ assert v.enabled?('feature1') && v.enabled?('feature2') && !v.enabled?('feature3')
+ end
+
+ def test_name_is_mandatory
+ v = VirtualCommunity.new
+ v.valid?
+ assert v.errors.invalid?(:name)
+ v.name = 'blablabla'
+ v.valid?
+ assert !v.errors.invalid?(:name)
+ end
+
end
--
libgit2 0.21.2