Commit 4f8868c25720895890b8c4572446719c941c0d98

Authored by Daniela Feitosa
1 parent 28dd8dad

ActionIntem858: Generic Profile Template Usage

  * adding method to apply templates
  * added setting[:templates_id] in environment
  * adding methods and vies to set and edit templates
app/controllers/admin/admin_panel_controller.rb
... ... @@ -16,7 +16,22 @@ class AdminPanelController < AdminController
16 16 end
17 17 end
18 18  
19   - def edit_templates
  19 + def manage_templates
  20 + @person_templates = environment.templates('person')
  21 + @community_templates = environment.templates('community')
  22 + @enterprise_templates = environment.templates('enterprise')
  23 + @templates = @person_templates + @community_templates + @enterprise_templates
20 24 end
21 25  
  26 + def set_template
  27 + environment.person_template = Person.find(params[:environment][:person_template]) if params[:environment][:person_template]
  28 + environment.enterprise_template = Enterprise.find(params[:environment][:enterprise_template]) if params[:environment][:enterprise_template]
  29 + environment.community_template = Community.find(params[:environment][:community_template]) if params[:environment][:community_template]
  30 + if environment.save!
  31 + flash[:notice] = _('Template updated successfully')
  32 + else
  33 + flash[:error] = _('Could not update template')
  34 + end
  35 + redirect_to :action => 'manage_templates'
  36 + end
22 37 end
... ...
app/models/environment.rb
... ... @@ -335,18 +335,57 @@ class Environment < ActiveRecord::Base
335 335 settings[:layout_template] = value
336 336 end
337 337  
338   - def enterprise_template
339   - Enterprise.find_by_id settings[:enterprise_template_id]
340   - end
341   -
342 338 def community_template
343 339 Community.find_by_id settings[:community_template_id]
344 340 end
345 341  
  342 + def community_template=(value)
  343 + settings[:community_template_id] = value.id
  344 + end
  345 +
346 346 def person_template
347 347 Person.find_by_id settings[:person_template_id]
348 348 end
349 349  
  350 + def person_template=(value)
  351 + settings[:person_template_id] = value.id
  352 + end
  353 +
  354 + def enterprise_template
  355 + Enterprise.find_by_id settings[:enterprise_template_id]
  356 + end
  357 +
  358 + def enterprise_template=(value)
  359 + settings[:enterprise_template_id] = value.id
  360 + end
  361 +
  362 + def inactive_enterprise_template
  363 + Enterprise.find_by_id settings[:inactive_enterprise_template_id]
  364 + end
  365 +
  366 + def inactive_enterprise_template=(value)
  367 + settings[:inactive_enterprise_template_id] = value.id
  368 + end
  369 +
  370 + def templates(profile = 'profile')
  371 + klass = profile.classify.constantize
  372 + templates = []
  373 + if settings[:templates_ids]
  374 + settings[:templates_ids].each do |template_id|
  375 + templates << klass.find_by_id(template_id)
  376 + end
  377 + end
  378 + templates.compact
  379 + end
  380 +
  381 + def add_templates=(values)
  382 + if settings[:templates_ids]
  383 + settings[:templates_ids].concat(values.map(&:id))
  384 + else
  385 + settings[:templates_ids] = values.map(&:id)
  386 + end
  387 + end
  388 +
350 389 after_create :create_templates
351 390  
352 391 def create_templates
... ...
app/models/profile.rb
... ... @@ -213,6 +213,12 @@ class Profile &lt; ActiveRecord::Base
213 213 nil
214 214 end
215 215  
  216 + def apply_template(template)
  217 + copy_blocks_from(template)
  218 + copy_articles_from(template)
  219 + self.update_attributes!(:custom_footer => template[:custom_footer], :custom_header => template[:custom_header])
  220 + end
  221 +
216 222 xss_terminate :only => [ :name, :nickname, :address, :contact_phone ]
217 223  
218 224 # returns the contact email for this profile. By default returns the the
... ... @@ -355,6 +361,16 @@ class Profile &lt; ActiveRecord::Base
355 361 end
356 362  
357 363 def copy_article_tree(article, parent=nil)
  364 + original_article = self.articles.find_by_name(article.name)
  365 + if original_article
  366 + num = 2
  367 + new_name = original_article.name + ' ' + num.to_s
  368 + while self.articles.find_by_name(new_name)
  369 + num = num + 1
  370 + new_name = original_article.name + ' ' + num.to_s
  371 + end
  372 + original_article.update_attributes!(:name => new_name)
  373 + end
358 374 article_copy = article.copy(:profile => self, :parent => parent, :advertise => false)
359 375 if article.profile.home_page == article
360 376 self.home_page = article_copy
... ... @@ -440,11 +456,26 @@ class Profile &lt; ActiveRecord::Base
440 456 end
441 457  
442 458 def custom_header
443   - self[:custom_header] || environment.custom_header
  459 + header = self[:custom_header] || environment.custom_header
  460 + if header
  461 + if self.respond_to?(:name) && header.include?('{name}')
  462 + header.gsub('{name}', self.name)
  463 + else
  464 + header
  465 + end
  466 + end
444 467 end
445 468  
446 469 def custom_footer
447   - self[:custom_footer] || environment.custom_footer
  470 + footer = self[:custom_footer] || environment.custom_footer
  471 + if footer
  472 + %w[contact_person contact_email contact_phone location address economic_activity].each do |att|
  473 + if self.respond_to?(att) && footer.include?("{#{att}}")
  474 + footer.gsub!("{#{att}}", self.send(att).to_s)
  475 + end
  476 + end
  477 + end
  478 + footer
448 479 end
449 480  
450 481 def theme
... ...
app/views/admin_panel/index.rhtml
... ... @@ -10,5 +10,5 @@
10 10 <li><%= link_to _('Manage Categories'), :controller => 'categories'%></li>
11 11 <li><%= link_to _('Manage User roles'), :controller => 'role' %></li>
12 12 <li><%= link_to _('Manage Validators by region'), :controller => 'region_validators' %></li>
13   - <li><%= link_to _('Edit Templates'), :action => 'edit_templates' %></li>
  13 + <li><%= link_to _('Manage Templates'), :action => 'manage_templates' %></li>
14 14 </ul>
... ...
app/views/admin_panel/manage_templates.rhtml 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +<h1><%= _('Set Templates') %></h1>
  2 +
  3 +<% labelled_form_for(:environment, @environment, :url => {:action => 'set_template'}) do |f| %>
  4 +<%= current_user.name %>
  5 + <p>
  6 + <label for='environment_person_template'><%= _('Person Template') %></label><br/>
  7 + <%= f.select :person_template, @person_templates.map {|item| [ item.identifier, item.id]}, :selected => (environment.person_template.nil? ? nil : environment.person_template.id) %>
  8 + </p>
  9 +
  10 + <p>
  11 + <label for='environment_community_template'><%= _('Community Template') %></label><br/>
  12 + <%= f.select :community_template, @community_templates.map {|item| [ item.identifier, item.id]}, :selected => (environment.community_template.nil? ? nil : environment.community_template.id) %>
  13 + </p>
  14 +
  15 + <p>
  16 + <label for='environment_enterprise_template'><%= _('Enterprise Template') %></label><br/>
  17 + <%= f.select :enterprise_template, @enterprise_templates.map {|item| [ item.identifier, item.id]}, :selected => (environment.enterprise_template.nil? ? nil : environment.enterprise_template.id) %>
  18 + </p>
  19 +
  20 + <p>
  21 + <label for='environment_inactive_enterprise_template'><%= _('Inactive Enterprise Template') %></label><br/>
  22 + <%= f.select :inactive_enterprise_template, @enterprise_templates.map {|item| [ item.identifier, item.id]}, :selected => (environment.inactive_enterprise_template.nil? ? nil : environment.inactive_enterprise_template.id) %>
  23 + </p>
  24 +
  25 + <% button_bar do %>
  26 + <%= submit_button(:save, _('Save')) %>
  27 + <%= button(:cancel, _('Cancel'), :action => 'index') %>
  28 + <% end %>
  29 +
  30 +<% end %>
  31 +
  32 +<h1><%= _('Edit Templates') %></h1>
  33 +
  34 +<ul>
  35 +<% @templates.each do |template| %>
  36 + <li><%= link_to template.identifier, :controller => 'profile_editor', :profile => template.identifier %></li>
  37 +<% end %>
  38 +</ul>
  39 +
... ...
test/functional/admin_panel_controller_test.rb
... ... @@ -110,4 +110,39 @@ class AdminPanelControllerTest &lt; Test::Unit::TestCase
110 110 assert_equal "This <strong>is</strong> my new environment", Environment.default.message_for_disabled_enterprise
111 111 end
112 112  
  113 + should 'list templates' do
  114 + get :manage_templates
  115 +
  116 + assert_kind_of Array, assigns(:person_templates)
  117 + assert_kind_of Array, assigns(:community_templates)
  118 + assert_kind_of Array, assigns(:enterprise_templates)
  119 + end
  120 +
  121 + should 'display environment template options' do
  122 + e = Environment.default
  123 + @controller.stubs(:environment).returns(e)
  124 + profile_template = Profile.create!(:name =>'template_test', :identifier => 'template_test', :environment => e)
  125 + e.settings[:templates_ids] = [profile_template.id]
  126 + e.save!
  127 + e.stubs(:templates).with('person').returns([profile_template])
  128 + e.stubs(:templates).with('community').returns([profile_template])
  129 + e.stubs(:templates).with('enterprise').returns([profile_template])
  130 +
  131 + assert_equal [profile_template], e.templates('person')
  132 +
  133 + get :manage_templates
  134 + ['person_template', 'community_template', 'enterprise_template'].each do |template|
  135 + assert_tag :tag => 'select', :attributes => { :id => "environment_#{template}"}, :descendant => { :tag => 'option', :content => 'template_test'}
  136 + end
  137 + end
  138 +
  139 + should 'set template' do
  140 + e = Environment.default
  141 + @controller.stubs(:environment).returns(e)
  142 + profile_template = Enterprise.create!(:name =>'template_test', :identifier => 'template_test')
  143 +
  144 + post :set_template, :environment => {:enterprise_template => profile_template.id}
  145 +
  146 + assert_equal profile_template, e.enterprise_template
  147 + end
113 148 end
... ...
test/unit/environment_test.rb
... ... @@ -440,6 +440,58 @@ class EnvironmentTest &lt; Test::Unit::TestCase
440 440 assert !e.person_template.public?
441 441 end
442 442  
  443 + should 'set a template in community_template' do
  444 + e = Environment.create!(:name => 'test_env')
  445 + template = Community.create!(:name => 'Community template 2', :identifier => e.name.to_slug + 'community_template_2', :environment => e, :public_profile => false)
  446 + e.community_template = template
  447 +
  448 + assert_equal template, e.community_template
  449 + end
  450 +
  451 + should 'set a template in person_template' do
  452 + e = Environment.create!(:name => 'test_env')
  453 + template = create_user('person_template_2').person
  454 + e.person_template = template
  455 +
  456 + assert_equal template, e.person_template
  457 + end
  458 +
  459 + should 'set a template in enterprise_template' do
  460 + e = Environment.create!(:name => 'test_env')
  461 + template = Enterprise.create!(:name => 'Enterprise template 2', :identifier => e.name.to_slug + 'enterprise_template', :environment => e, :public_profile => false)
  462 + e.enterprise_template = template
  463 +
  464 + assert_equal template, e.enterprise_template
  465 + end
  466 +
  467 + should 'add templates when it is empty' do
  468 + e = Environment.create!(:name => 'test_env')
  469 + ent_template_a = Enterprise.create!(:name => 'Enterprise template A', :identifier => e.name.to_slug + 'enterprise_template_a', :environment => e, :public_profile => false)
  470 + ent_template_b = Enterprise.create!(:name => 'Enterprise template B', :identifier => e.name.to_slug + 'enterprise_template_b', :environment => e, :public_profile => false)
  471 +
  472 + e.add_templates = [ent_template_a, ent_template_b]
  473 + assert_equal [ent_template_a, ent_template_b], e.templates
  474 + end
  475 +
  476 + should 'add templates when it is not empty' do
  477 + e = Environment.create!(:name => 'test_env')
  478 +
  479 + ent_template_example = Enterprise.create!(:name => 'Enterprise template example', :identifier => e.name.to_slug + 'enterprise_template_example', :environment => e, :public_profile => false)
  480 +
  481 + e.settings[:templates_ids] = [ent_template_example.id]
  482 + e.save
  483 +
  484 + ent_template_a = Enterprise.create!(:name => 'Enterprise template A', :identifier => e.name.to_slug + 'enterprise_template_a', :environment => e, :public_profile => false)
  485 +
  486 + e.add_templates = [ent_template_a]
  487 +
  488 + assert_equal [ent_template_example, ent_template_a], e.templates
  489 + end
  490 +
  491 + should 'have an empty array of templates by default' do
  492 + assert_equal [], Environment.new.templates
  493 + end
  494 +
443 495 should 'not disable ssl by default' do
444 496 e = Environment.new
445 497 assert !e.disable_ssl
... ...
test/unit/profile_test.rb
... ... @@ -767,10 +767,18 @@ class ProfileTest &lt; Test::Unit::TestCase
767 767 assert_equal 'my custom header', Profile.new(:custom_header => 'my custom header').custom_header
768 768 end
769 769  
  770 + should 'provide custom header with variables' do
  771 + assert_equal 'Custom header of Test', Profile.new(:custom_header => 'Custom header of {name}', :name => 'Test').custom_header
  772 + end
  773 +
770 774 should 'provide custom footer' do
771 775 assert_equal 'my custom footer', Profile.new(:custom_footer => 'my custom footer').custom_footer
772 776 end
773 777  
  778 + should 'provide custom footer with variables' do
  779 + assert_equal 'Address: Address for test', Profile.new(:custom_footer => 'Address: {address}', :address => 'Address for test').custom_footer
  780 + end
  781 +
774 782 should 'provide environment header if profile header is blank' do
775 783 profile = Profile.new
776 784 env = mock
... ... @@ -893,6 +901,53 @@ class ProfileTest &lt; Test::Unit::TestCase
893 901 assert_equal 1, p.boxes[0].blocks.size
894 902 end
895 903  
  904 + should 'apply template' do
  905 + template = Profile.create!(:name => 'test template', :identifier => 'test_template')
  906 + template.boxes.destroy_all
  907 + template.boxes << Box.new
  908 + template.boxes[0].blocks << Block.new
  909 + template.save!
  910 +
  911 + p = Profile.create!(:name => 'test prof', :identifier => 'test_prof')
  912 +
  913 + p.apply_template(template)
  914 +
  915 + assert_equal 1, p.boxes.size
  916 + assert_equal 1, p.boxes[0].blocks.size
  917 + end
  918 +
  919 + should 'copy articles when applying template' do
  920 + template = Profile.create!(:name => 'test template', :identifier => 'test_template')
  921 + template.boxes.destroy_all
  922 + template.boxes << Box.new
  923 + template.boxes[0].blocks << Block.new
  924 + template.articles.create(:name => 'template article')
  925 + template.save!
  926 +
  927 + p = Profile.create!(:name => 'test prof', :identifier => 'test_prof')
  928 +
  929 + p.apply_template(template)
  930 +
  931 + assert_not_nil p.articles.find_by_name('template article')
  932 + end
  933 +
  934 + should 'rename existing articles when applying template' do
  935 + template = Profile.create!(:name => 'test template', :identifier => 'test_template')
  936 + template.boxes.destroy_all
  937 + template.boxes << Box.new
  938 + template.boxes[0].blocks << Block.new
  939 + template.articles.create(:name => 'some article')
  940 + template.save!
  941 +
  942 + p = Profile.create!(:name => 'test prof', :identifier => 'test_prof')
  943 + p.articles.create(:name => 'some article')
  944 +
  945 + p.apply_template(template)
  946 +
  947 + assert_not_nil p.articles.find_by_name('some article 2')
  948 + assert_not_nil p.articles.find_by_name('some article')
  949 + end
  950 +
896 951 TMP_THEMES_DIR = RAILS_ROOT + '/test/tmp/profile_themes'
897 952 should 'have themes' do
898 953 Theme.stubs(:user_themes_dir).returns(TMP_THEMES_DIR)
... ...