Commit 180a4d9db964fdc1914446accc752027a12d24f3

Authored by AntonioTerceiro
1 parent f639512c

ActionItem3: properly saving features



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@87 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
... ... @@ -42,6 +42,7 @@ class ApplicationController < ActionController::Base
42 42  
43 43 protected
44 44  
  45 + # TODO: move this logic somewhere else (Domain class?)
45 46 def detect_stuff_by_domain
46 47 @domain = Domain.find_by_name(request.host)
47 48 if @domain.nil?
... ...
app/controllers/features_controller.rb
... ... @@ -2,6 +2,14 @@ class FeaturesController < ApplicationController
2 2 acts_as_virtual_community_admin_controller
3 3  
4 4 def index
5   - @features = VirtualCommunity::EXISTING_FEATURES
  5 + @features = VirtualCommunity.available_features
6 6 end
  7 +
  8 + def update
  9 + @virtual_community.enabled_features = params[:features].keys
  10 + @virtual_community.save!
  11 + flash[:notice] = _('Features updated successfully.')
  12 + redirect_to :action => 'index'
  13 + end
  14 +
7 15 end
... ...
app/models/virtual_community.rb
1 1 class VirtualCommunity < ActiveRecord::Base
2 2  
3   - # TODO: these are test features
4   - EXISTING_FEATURES = {
5   - 'feature1' => _('Enable Feature 1'),
6   - 'feature2' => _('Enable Feature 2'),
7   - 'feature3' => _('Enable Feature 3'),
8   - }
  3 + # returns the available features for a VirtualCommunity, in the form of a
  4 + # hash, with pairs in the form <tt>'feature_name' => 'Feature name'</tt>.
  5 + def self.available_features
  6 + {
  7 + 'some_feature' => _('Some feature'),
  8 + 'other_feature' => _('Other feature'),
  9 + }
  10 + end
9 11  
10 12 # #################################################
11 13 # Relationships and applied behaviour
... ... @@ -35,6 +37,16 @@ class VirtualCommunity &lt; ActiveRecord::Base
35 37 def enabled?(feature)
36 38 self.settings["#{feature}_enabled"] == true
37 39 end
  40 +
  41 + def enabled_features=(features)
  42 + self.class.available_features.keys.each do |feature|
  43 + if features.include? feature
  44 + self.enable(feature)
  45 + else
  46 + self.disable(feature)
  47 + end
  48 + end
  49 + end
38 50  
39 51 # #################################################
40 52 # Validations
... ...
app/views/features/_features_table.rhtml 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +<table>
  2 + <% form_tag({:action => 'update'}) do %>
  3 + <% @features.each do |feature, text| %>
  4 + <tr>
  5 + <td><%= text %></td>
  6 + <td><%= check_box_tag "features[#{feature}]", '1', @virtual_community.enabled?(feature) %></td>
  7 + </tr>
  8 + <% end %>
  9 + <tr><td colspan='2'><%= submit_tag %></td></tr>
  10 + <% end %>
  11 +</table>
... ...
app/views/features/index.rhtml
1 1 <h2><%= _('Enable/Disable features') %></h2>
2 2  
3   -<table>
4   - <% form_tag({:action => 'update_features'}) do %>
5   - <% @features.each do |feature, text| %>
6   - <tr>
7   - <td><%= text %></td>
8   - <td><%= check_box_tag "feature[#{feature}]" %></td>
9   - </tr>
10   - <% end %>
11   - <tr><td colspan='2'><%= submit_tag %></td></tr>
12   - <% end %>
13   -</table>
  3 +<%= render :partial => 'features_table' %>
... ...
test/functional/features_controller_test.rb
... ... @@ -5,23 +5,30 @@ require &#39;features_controller&#39;
5 5 class FeaturesController; def rescue_action(e) raise e end; end
6 6  
7 7 class FeaturesControllerTest < Test::Unit::TestCase
  8 +
  9 + fixtures :virtual_communities
  10 +
8 11 def setup
9 12 @controller = FeaturesController.new
10 13 @request = ActionController::TestRequest.new
11 14 @response = ActionController::TestResponse.new
  15 + uses_host 'anhetegua.net'
12 16 end
13 17  
14 18 def test_listing_features
15 19 get :index
16 20 assert_template 'index'
17   - VirtualCommunity::EXISTING_FEATURES.each do |feature, text|
18   - assert_tag(:tag => 'input', :attributes => { :type => 'checkbox', :name => "feature[#{feature}]" })
  21 + VirtualCommunity.available_features.each do |feature, text|
  22 + assert_tag(:tag => 'input', :attributes => { :type => 'checkbox', :name => "features[#{feature}]" })
19 23 end
20 24 end
21 25  
22 26 def test_update_features
23   - # flunk 'Not implemented yet'
24   - assert true
  27 + get :update, :features => { 'feature1' => '1', 'feature2' => '1' }
  28 + assert_redirected_to :action => 'index'
  29 + assert_kind_of String, flash[:notice]
  30 + v = VirtualCommunity.find(virtual_communities(:colivre_net).id)
  31 + assert v.enabled?('feature1') && v.enabled?('feature2') && !v.enabled?('feature3')
25 32 end
26 33  
27 34 end
... ...
test/mocks/test/virtual_community.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +require 'app/models/virtual_community'
  2 +
  3 +class VirtualCommunity < ActiveRecord::Base
  4 + def self.available_features
  5 + {
  6 + 'feature1' => 'Enable Feature 1',
  7 + 'feature2' => 'Enable Feature 2',
  8 + 'feature3' => 'Enable Feature 3',
  9 + }
  10 + end
  11 +end
... ...
test/unit/virtual_community_test.rb
... ... @@ -26,6 +26,10 @@ class VirtualCommunityTest &lt; Test::Unit::TestCase
26 26 assert_kind_of ConfigurableSetting, vc.settings.first
27 27 end
28 28  
  29 + def test_available_features
  30 + assert_kind_of Hash, VirtualCommunity.available_features
  31 + end
  32 +
29 33 def test_features
30 34 v = virtual_communities(:colivre_net)
31 35 v.enable('feature1')
... ... @@ -34,4 +38,19 @@ class VirtualCommunityTest &lt; Test::Unit::TestCase
34 38 assert !v.enabled?('feature1')
35 39 end
36 40  
  41 + def test_enabled_features
  42 + v = virtual_communities(:colivre_net)
  43 + v.enabled_features = [ 'feature1', 'feature2' ]
  44 + assert v.enabled?('feature1') && v.enabled?('feature2') && !v.enabled?('feature3')
  45 + end
  46 +
  47 + def test_name_is_mandatory
  48 + v = VirtualCommunity.new
  49 + v.valid?
  50 + assert v.errors.invalid?(:name)
  51 + v.name = 'blablabla'
  52 + v.valid?
  53 + assert !v.errors.invalid?(:name)
  54 + end
  55 +
37 56 end
... ...