From 00dacfc25b70dd86f30770bcde110f64a7eaa662 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 18 Jul 2012 12:15:06 -0300 Subject: [PATCH] [multiple-templates] Manage templates views and controllers --- app/controllers/admin/templates_controller.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ app/views/admin_panel/edit_templates.rhtml | 10 ---------- app/views/admin_panel/index.rhtml | 2 +- app/views/templates/_create_template_form.html.erb | 19 +++++++++++++++++++ app/views/templates/create_community_template.html.erb | 12 ++++++++++++ app/views/templates/create_enterprise_template.html.erb | 3 +++ app/views/templates/create_person_template.html.erb | 12 ++++++++++++ app/views/templates/index.html.erb | 19 +++++++++++++++++++ test/functional/templates_controller_test.rb | 32 ++++++++++++++++++++++++++++++++ 9 files changed, 150 insertions(+), 11 deletions(-) create mode 100644 app/controllers/admin/templates_controller.rb delete mode 100644 app/views/admin_panel/edit_templates.rhtml create mode 100644 app/views/templates/_create_template_form.html.erb create mode 100644 app/views/templates/create_community_template.html.erb create mode 100644 app/views/templates/create_enterprise_template.html.erb create mode 100644 app/views/templates/create_person_template.html.erb create mode 100644 app/views/templates/index.html.erb create mode 100644 test/functional/templates_controller_test.rb diff --git a/app/controllers/admin/templates_controller.rb b/app/controllers/admin/templates_controller.rb new file mode 100644 index 0000000..d3a532c --- /dev/null +++ b/app/controllers/admin/templates_controller.rb @@ -0,0 +1,52 @@ +class TemplatesController < AdminController + protect 'manage_environment_templates', :environment + + def create_person_template + if request.post? + begin + identifier = params[:name].to_slug + password = Digest::MD5.hexdigest(rand.to_s) + template = User.new(:login => identifier, :email => identifier+'@templates.noo', :password => password, :password_confirmation => password, :person_data => {:name => params[:name], :is_template => true}) + template.save! + session[:notice] = _('New template created') + redirect_to :action => 'edit_templates' + rescue + @error = _('Name has already been taken') + end + end + end + + def create_community_template + if request.post? + begin + create_organization_template(Community) + session[:notice] = _('New template created') + redirect_to :action => 'edit_templates' + rescue + @error = _('Name has already been taken') + end + end + end + + def create_enterprise_template + if request.post? + begin + create_organization_template(Enterprise) + session[:notice] = _('New template created') + redirect_to :action => 'edit_templates' + rescue + @error = _('Name has already been taken') + end + end + end + + private + + def create_organization_template(klass) + identifier = params[:name].to_slug + template = klass.new(:name => params[:name], :identifier => identifier, :is_template => true) + template.save! + end + +end + diff --git a/app/views/admin_panel/edit_templates.rhtml b/app/views/admin_panel/edit_templates.rhtml deleted file mode 100644 index 7e91399..0000000 --- a/app/views/admin_panel/edit_templates.rhtml +++ /dev/null @@ -1,10 +0,0 @@ -

<%= _('Edit Templates') %>

- - diff --git a/app/views/admin_panel/index.rhtml b/app/views/admin_panel/index.rhtml index f5641dc..638bdca 100644 --- a/app/views/admin_panel/index.rhtml +++ b/app/views/admin_panel/index.rhtml @@ -12,7 +12,7 @@ <%= link_to _('Manage User roles'), :controller => 'role' %> <%= link_to _('Manage users'), :controller => 'users' %> <%= link_to _('Manage Validators by region'), :controller => 'region_validators' %> - <%= link_to _('Edit Templates'), :action => 'edit_templates' %> + <%= link_to _('Edit Templates'), :controller => 'templates' %> <%= link_to _('Manage Fields'), :controller => 'features', :action => 'manage_fields' %> <%= link_to _('Set Portal'), :action => 'set_portal_community' %> <% @plugins.dispatch(:admin_panel_links).each do |link| %> diff --git a/app/views/templates/_create_template_form.html.erb b/app/views/templates/_create_template_form.html.erb new file mode 100644 index 0000000..773ff84 --- /dev/null +++ b/app/views/templates/_create_template_form.html.erb @@ -0,0 +1,19 @@ +<% if @error %> +
+

<%= _('The template could not be saved') %>

+

<%= _('There were problems with the following fields:') %>

+ +
+<% end %> + +<% form_tag do %> + <%= labelled_text_field(_('Name')+': ', :name)%> + + <% button_bar do %> + <%= submit_button('save', _('Save'))%> + <%= button('cancel', _('Cancel'), {:action => 'index'})%> + <% end %> +<% end %> + diff --git a/app/views/templates/create_community_template.html.erb b/app/views/templates/create_community_template.html.erb new file mode 100644 index 0000000..6547a1e --- /dev/null +++ b/app/views/templates/create_community_template.html.erb @@ -0,0 +1,12 @@ +<% title = case @kind + when 'person' + + when 'community' + + when 'enterprise' + _('Create enterprise template') +end %> + +

<%= _('Create community template') %>

+ +<%= render :partial => 'create_template_form' %> diff --git a/app/views/templates/create_enterprise_template.html.erb b/app/views/templates/create_enterprise_template.html.erb new file mode 100644 index 0000000..6788765 --- /dev/null +++ b/app/views/templates/create_enterprise_template.html.erb @@ -0,0 +1,3 @@ +

<%= _('Create enterprise template') %>

+ +<%= render :partial => 'create_template_form' %> diff --git a/app/views/templates/create_person_template.html.erb b/app/views/templates/create_person_template.html.erb new file mode 100644 index 0000000..744823a --- /dev/null +++ b/app/views/templates/create_person_template.html.erb @@ -0,0 +1,12 @@ +<% title = case @kind + when 'person' + + when 'community' + _('Create community template') + when 'enterprise' + _('Create enterprise template') +end %> + +

<%= _('Create person template') %>

+ +<%= render :partial => 'create_template_form' %> diff --git a/app/views/templates/index.html.erb b/app/views/templates/index.html.erb new file mode 100644 index 0000000..7d60e48 --- /dev/null +++ b/app/views/templates/index.html.erb @@ -0,0 +1,19 @@ +

<%= _('Edit Templates') %>

+ +<% list_of_templates = [[_('Person') , Person.templates , 'person' ], + [_('Community') , Community.templates , 'community' ], + [_('Enterprise'), Enterprise.templates, 'enterprise']] %> + + diff --git a/test/functional/templates_controller_test.rb b/test/functional/templates_controller_test.rb new file mode 100644 index 0000000..413d79f --- /dev/null +++ b/test/functional/templates_controller_test.rb @@ -0,0 +1,32 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'templates_controller' + +# Re-raise errors caught by the controller. +class TemplatesController; def rescue_action(e) raise e end; end + +class TemplatesControllerTest < ActionController::TestCase + + all_fixtures + def setup + @controller = TemplatesController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + login_as(create_admin_user(Environment.default)) + end + + should 'create person template' do + post :create_person_template, :name => 'Developer' + assert Person['developer'].is_template + end + + should 'create community template' do + post :create_community_template, :name => 'Debian' + assert Community['debian'].is_template + end + + should 'create enterprise template' do + post :create_enterprise_template, :name => 'Free Software Foundation' + assert Enterprise['free-software-foundation'].is_template + end +end + -- libgit2 0.21.2