Commit 3b569fa2ace22975284e68df02794cb03457dda2
1 parent
600d8e0f
Exists in
master
and in
29 other branches
Add set as default method for templates
Showing
5 changed files
with
125 additions
and
2 deletions
Show diff stats
app/controllers/admin/templates_controller.rb
@@ -40,8 +40,40 @@ class TemplatesController < AdminController | @@ -40,8 +40,40 @@ class TemplatesController < AdminController | ||
40 | end | 40 | end |
41 | end | 41 | end |
42 | 42 | ||
43 | + def set_community_as_default | ||
44 | + set_as_default(Community.find(params[:template_id])) | ||
45 | + redirect_to :action => 'index' | ||
46 | + end | ||
47 | + | ||
48 | + def set_person_as_default | ||
49 | + set_as_default(Person.find(params[:template_id])) | ||
50 | + redirect_to :action => 'index' | ||
51 | + end | ||
52 | + | ||
53 | + def set_enterprise_as_default | ||
54 | + set_as_default(Enterprise.find(params[:template_id])) | ||
55 | + redirect_to :action => 'index' | ||
56 | + end | ||
57 | + | ||
43 | private | 58 | private |
44 | 59 | ||
60 | + def set_as_default(obj) | ||
61 | + return nil if obj.nil? | ||
62 | + case obj.class.name | ||
63 | + when 'Community' then | ||
64 | + environment.community_default_template = obj | ||
65 | + environment.save! | ||
66 | + when 'Person' then | ||
67 | + environment.person_default_template = obj | ||
68 | + environment.save! | ||
69 | + when 'Enterprise' then | ||
70 | + environment.enterprise_default_template = obj | ||
71 | + environment.save! | ||
72 | + else | ||
73 | + nil | ||
74 | + end | ||
75 | + end | ||
76 | + | ||
45 | def create_organization_template(klass) | 77 | def create_organization_template(klass) |
46 | identifier = params[:name].to_slug | 78 | identifier = params[:name].to_slug |
47 | template = klass.new(:name => params[:name], :identifier => identifier, :is_template => true) | 79 | template = klass.new(:name => params[:name], :identifier => identifier, :is_template => true) |
app/models/environment.rb
@@ -717,6 +717,13 @@ class Environment < ActiveRecord::Base | @@ -717,6 +717,13 @@ class Environment < ActiveRecord::Base | ||
717 | ] | 717 | ] |
718 | end | 718 | end |
719 | 719 | ||
720 | + def is_default_template?(template) | ||
721 | + is_default = template == community_default_template | ||
722 | + is_default = is_default || template == person_default_template | ||
723 | + is_default = is_default || template == enterprise_default_template | ||
724 | + is_default | ||
725 | + end | ||
726 | + | ||
720 | def community_templates | 727 | def community_templates |
721 | self.communities.templates | 728 | self.communities.templates |
722 | end | 729 | end |
app/views/templates/index.html.erb
@@ -16,6 +16,11 @@ | @@ -16,6 +16,11 @@ | ||
16 | <li> | 16 | <li> |
17 | <%= image_tag "icons-app/#{kind}-icon.png" %> | 17 | <%= image_tag "icons-app/#{kind}-icon.png" %> |
18 | <%= link_to(template.name, {:controller => 'profile_editor', :profile => template.identifier}, :title => _('Edit template "%s"') % template.name ) %> | 18 | <%= link_to(template.name, {:controller => 'profile_editor', :profile => template.identifier}, :title => _('Edit template "%s"') % template.name ) %> |
19 | + <% if environment.is_default_template?(template) %> | ||
20 | + <%= _('is the default template') %> | ||
21 | + <% else %> | ||
22 | + <%= link_to(_('Set as default'), {:action => "set_#{kind}_as_default", :template_id => template.id}, :title => _('Set %s template as default') % template.name ) %> | ||
23 | + <% end %> | ||
19 | </li> | 24 | </li> |
20 | <% end %> | 25 | <% end %> |
21 | </ul> | 26 | </ul> |
test/functional/templates_controller_test.rb
@@ -6,14 +6,17 @@ class TemplatesController; def rescue_action(e) raise e end; end | @@ -6,14 +6,17 @@ class TemplatesController; def rescue_action(e) raise e end; end | ||
6 | 6 | ||
7 | class TemplatesControllerTest < ActionController::TestCase | 7 | class TemplatesControllerTest < ActionController::TestCase |
8 | 8 | ||
9 | - all_fixtures | ||
10 | def setup | 9 | def setup |
11 | @controller = TemplatesController.new | 10 | @controller = TemplatesController.new |
12 | @request = ActionController::TestRequest.new | 11 | @request = ActionController::TestRequest.new |
13 | @response = ActionController::TestResponse.new | 12 | @response = ActionController::TestResponse.new |
14 | - login_as(create_admin_user(Environment.default)) | 13 | + Environment.destroy_all |
14 | + @environment = fast_create(Environment, :is_default => true) | ||
15 | + login_as(create_admin_user(@environment)) | ||
15 | end | 16 | end |
16 | 17 | ||
18 | + attr_accessor :environment | ||
19 | + | ||
17 | should 'create person template' do | 20 | should 'create person template' do |
18 | post :create_person_template, :name => 'Developer' | 21 | post :create_person_template, :name => 'Developer' |
19 | assert Person['developer'].is_template | 22 | assert Person['developer'].is_template |
@@ -28,5 +31,45 @@ class TemplatesControllerTest < ActionController::TestCase | @@ -28,5 +31,45 @@ class TemplatesControllerTest < ActionController::TestCase | ||
28 | post :create_enterprise_template, :name => 'Free Software Foundation' | 31 | post :create_enterprise_template, :name => 'Free Software Foundation' |
29 | assert Enterprise['free-software-foundation'].is_template | 32 | assert Enterprise['free-software-foundation'].is_template |
30 | end | 33 | end |
34 | + | ||
35 | + should 'set a community as default template' do | ||
36 | + | ||
37 | + c1= fast_create(Community, :is_template => true, :environment_id => environment.id) | ||
38 | + environment.community_default_template= c1 | ||
39 | + environment.save | ||
40 | + | ||
41 | + c3 = fast_create(Community, :is_template => true, :environment_id => environment.id) | ||
42 | + | ||
43 | + post :set_community_as_default, :template_id => c3.id | ||
44 | + environment.reload | ||
45 | + assert_equal c3, environment.community_default_template | ||
46 | + end | ||
47 | + | ||
48 | + should 'set a person as default template' do | ||
49 | + | ||
50 | + p1= fast_create(Person, :is_template => true, :environment_id => environment.id) | ||
51 | + environment.person_default_template= p1 | ||
52 | + environment.save | ||
53 | + | ||
54 | + p3 = fast_create(Person, :is_template => true, :environment_id => environment.id) | ||
55 | + | ||
56 | + post :set_person_as_default, :template_id => p3.id | ||
57 | + environment.reload | ||
58 | + assert_equal p3, environment.person_default_template | ||
59 | + end | ||
60 | + | ||
61 | + should 'set a enterprise as default template' do | ||
62 | + | ||
63 | + e1= fast_create(Enterprise, :is_template => true, :environment_id => environment.id) | ||
64 | + environment.enterprise_default_template= e1 | ||
65 | + environment.save | ||
66 | + | ||
67 | + e3 = fast_create(Enterprise, :is_template => true, :environment_id => environment.id) | ||
68 | + | ||
69 | + post :set_enterprise_as_default, :template_id => e3.id | ||
70 | + environment.reload | ||
71 | + assert_equal e3, environment.enterprise_default_template | ||
72 | + end | ||
73 | + | ||
31 | end | 74 | end |
32 | 75 |
test/unit/environment_test.rb
@@ -687,6 +687,42 @@ class EnvironmentTest < ActiveSupport::TestCase | @@ -687,6 +687,42 @@ class EnvironmentTest < ActiveSupport::TestCase | ||
687 | assert_equal e3, env.enterprise_default_template | 687 | assert_equal e3, env.enterprise_default_template |
688 | end | 688 | end |
689 | 689 | ||
690 | + should 'is_default_template? method identify a person default template as default' do | ||
691 | + env = fast_create(Environment) | ||
692 | + | ||
693 | + p1 = fast_create(Person, :is_template => true, :environment_id => env.id) | ||
694 | + env.person_default_template= p1.id | ||
695 | + assert env.is_default_template?(p1) | ||
696 | + | ||
697 | + p2 = fast_create(Person, :is_template => true, :environment_id => env.id) | ||
698 | + env.person_default_template= p2.id | ||
699 | + assert !env.is_default_template?(p1) | ||
700 | + end | ||
701 | + | ||
702 | + should 'is_default_template? method identify a community default template as default' do | ||
703 | + env = fast_create(Environment) | ||
704 | + | ||
705 | + c1 = fast_create(Community, :is_template => true, :environment_id => env.id) | ||
706 | + env.community_default_template= c1.id | ||
707 | + assert env.is_default_template?(c1) | ||
708 | + | ||
709 | + c2 = fast_create(Community, :is_template => true, :environment_id => env.id) | ||
710 | + env.community_default_template= c2.id | ||
711 | + assert !env.is_default_template?(c1) | ||
712 | + end | ||
713 | + | ||
714 | + should 'is_default_template? method identify a enterprise default template as default' do | ||
715 | + env = fast_create(Environment) | ||
716 | + | ||
717 | + e1 = fast_create(Enterprise, :is_template => true, :environment_id => env.id) | ||
718 | + env.enterprise_default_template= e1.id | ||
719 | + assert env.is_default_template?(e1) | ||
720 | + | ||
721 | + e2 = fast_create(Enterprise, :is_template => true, :environment_id => env.id) | ||
722 | + env.enterprise_default_template= e2.id | ||
723 | + assert !env.is_default_template?(e1) | ||
724 | + end | ||
725 | + | ||
690 | should 'have a layout template' do | 726 | should 'have a layout template' do |
691 | e = build(Environment, :layout_template => 'mytemplate') | 727 | e = build(Environment, :layout_template => 'mytemplate') |
692 | assert_equal 'mytemplate', e.layout_template | 728 | assert_equal 'mytemplate', e.layout_template |