From 68a4fe77f3bb6c7f35ada5ef0cd527dbf93ed177 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 18 Jul 2012 11:23:25 -0300 Subject: [PATCH] [licenses] Adding controllers and views for licenses management --- app/controllers/admin/licenses_controller.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ app/views/admin_panel/index.rhtml | 1 + app/views/licenses/_form.html.erb | 12 ++++++++++++ app/views/licenses/create.html.erb | 2 ++ app/views/licenses/edit.html.erb | 2 ++ app/views/licenses/index.html.erb | 20 ++++++++++++++++++++ test/functional/licenses_controller_test.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+), 0 deletions(-) create mode 100644 app/controllers/admin/licenses_controller.rb create mode 100644 app/views/licenses/_form.html.erb create mode 100644 app/views/licenses/create.html.erb create mode 100644 app/views/licenses/edit.html.erb create mode 100644 app/views/licenses/index.html.erb create mode 100644 test/functional/licenses_controller_test.rb diff --git a/app/controllers/admin/licenses_controller.rb b/app/controllers/admin/licenses_controller.rb new file mode 100644 index 0000000..49212c5 --- /dev/null +++ b/app/controllers/admin/licenses_controller.rb @@ -0,0 +1,46 @@ +class LicensesController < AdminController + protect 'manage_environment_licenses', :environment + + def index + @licenses = environment.licenses + end + + def create + @license = License.new(params[:license]) + if request.post? + begin + @license.environment = environment + @license.save! + session[:notice] = _('License created') + redirect_to :action => 'index' + rescue + session[:notice] = _('License could not be created') + end + end + end + + def edit + @license = License.find(params[:license_id]) + if request.post? + begin + @license.update_attributes!(params[:license]) + session[:notice] = _('License updated') + redirect_to :action => 'index' + rescue + session[:notice] = _('License could not be updated') + end + end + end + + def remove + @license = License.find(params[:license_id]) + begin + @license.destroy + session[:notice] = _('Licese removed') + rescue + session[:notice] = _('Licese could not be removed') + end + redirect_to :action => 'index' + end + +end diff --git a/app/views/admin_panel/index.rhtml b/app/views/admin_panel/index.rhtml index f5641dc..2753627 100644 --- a/app/views/admin_panel/index.rhtml +++ b/app/views/admin_panel/index.rhtml @@ -15,6 +15,7 @@ <%= link_to _('Edit Templates'), :action => 'edit_templates' %> <%= link_to _('Manage Fields'), :controller => 'features', :action => 'manage_fields' %> <%= link_to _('Set Portal'), :action => 'set_portal_community' %> + <%= link_to _('Manage Licenses'), :controller =>'licenses' %> <% @plugins.dispatch(:admin_panel_links).each do |link| %> <%= link_to link[:title], link[:url] %> <% end %> diff --git a/app/views/licenses/_form.html.erb b/app/views/licenses/_form.html.erb new file mode 100644 index 0000000..ac4f53f --- /dev/null +++ b/app/views/licenses/_form.html.erb @@ -0,0 +1,12 @@ +<%= error_messages_for :license %> + +<% form_for :license, @license do |f| %> + <%= hidden_field_tag(:license_id, params[:license_id]) %> + <%= required labelled_form_field(_('Name'), f.text_field(:name)) %> + <%= labelled_form_field(_('License url'), f.text_field(:url)) %> + + <% button_bar do %> + <%= submit_button('save', _('Save'))%> + <%= button('cancel', _('Cancel'), {:action => 'index'})%> + <% end %> +<% end %> diff --git a/app/views/licenses/create.html.erb b/app/views/licenses/create.html.erb new file mode 100644 index 0000000..a13ed33 --- /dev/null +++ b/app/views/licenses/create.html.erb @@ -0,0 +1,2 @@ +

<%= _('New license') %>

+<%= render :partial => 'form' %> diff --git a/app/views/licenses/edit.html.erb b/app/views/licenses/edit.html.erb new file mode 100644 index 0000000..9cd0e69 --- /dev/null +++ b/app/views/licenses/edit.html.erb @@ -0,0 +1,2 @@ +

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

+<%= render :partial => 'form' %> diff --git a/app/views/licenses/index.html.erb b/app/views/licenses/index.html.erb new file mode 100644 index 0000000..e6d1878 --- /dev/null +++ b/app/views/licenses/index.html.erb @@ -0,0 +1,20 @@ +

<%= _('Manage licenses') %>

+ + + + + + + <% @licenses.each do |license| %> + + + + + + <% end %> + + + +
<%= _('Name') %><%= _('Url reference') %><%= _('Actions') %>
<%= truncate(license.name, 19) %><%= link_to(truncate(license.url, 60), license.url, :target => '_blank') %> + <%= button_without_text :edit, _('Edit'), :action => 'edit', :license_id => license.id %> + <%= button_without_text :remove, _('Remove'), {:action => 'remove', :license_id => license.id}, :confirm => _('Are you sure you want to remove this license?') %>
<%= link_to(_('NEW LICENSE'), :action => 'create')%>
diff --git a/test/functional/licenses_controller_test.rb b/test/functional/licenses_controller_test.rb new file mode 100644 index 0000000..9e51048 --- /dev/null +++ b/test/functional/licenses_controller_test.rb @@ -0,0 +1,46 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'licenses_controller' + +# Re-raise errors caught by the controller. +class LIcensesController; def rescue_action(e) raise e end; end + +class LicensesControllerTest < ActionController::TestCase + + def setup + @controller = LicensesController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @environment = Environment.default + login_as(create_admin_user(@environment)) + end + + attr_accessor :environment + + should 'list environment licenses' do + l1 = License.create!(:name => 'GPLv3', :environment => environment) + l2 = License.create!(:name => 'AGPL', :environment => environment) + + get :index + + assert_includes assigns(:licenses), l1 + assert_includes assigns(:licenses), l2 + end + + should 'create a new license' do + assert_difference License, :count, 1 do + post :create, :license => {:name => 'GPLv3'} + end + end + + should 'edit a license' do + license = License.create!(:name => 'GPLv2', :environment => environment) + post :edit, :license_id => license.id, :license => {:name => 'GPLv3'} + assert_equal 'GPLv3', License.last.name + end + + should 'remove a license' do + license = License.create!(:name => 'GPLv3', :environment => environment) + post :remove, :license_id => license.id + assert_raise(ActiveRecord::RecordNotFound) {License.find(license.id)} + end +end -- libgit2 0.21.2