Commit 3c1d3d845f90bac4956f1ee0334b37c35a5b7bf2

Authored by Victor Costa
1 parent 7aa7a2f4

Manage badges

controllers/admin/gamification_plugin_badges_controller.rb 0 → 100644
@@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
  1 +class GamificationPluginBadgesController < PluginAdminController
  2 +
  3 + def index
  4 + @gamification_plugin_badges = environment.gamification_plugin_badges
  5 + end
  6 +
  7 + def show
  8 + @gamification_plugin_badge = environment.gamification_plugin_badges.find(params[:id])
  9 + end
  10 +
  11 + def new
  12 + @gamification_plugin_badge = GamificationPlugin::Badge.new
  13 + end
  14 +
  15 + def edit
  16 + @gamification_plugin_badge = environment.gamification_plugin_badges.find(params[:id])
  17 + end
  18 +
  19 + def create
  20 + @gamification_plugin_badge = GamificationPlugin::Badge.new(params[:gamification_plugin_badge])
  21 + @gamification_plugin_badge.owner = environment
  22 +
  23 + if @gamification_plugin_badge.save
  24 + session[:notice] = _('Badge was successfully created.')
  25 + redirect_to :action => :index
  26 + else
  27 + render action: "new"
  28 + end
  29 + end
  30 +
  31 + def update
  32 + @gamification_plugin_badge = environment.gamification_plugin_badges.find(params[:id])
  33 +
  34 + if @gamification_plugin_badge.update_attributes(params[:gamification_plugin_badge])
  35 + session[:notice] = _('Badge was successfully updated.')
  36 + redirect_to :action => :index
  37 + else
  38 + render action: "edit"
  39 + end
  40 + end
  41 +
  42 + def destroy
  43 + @gamification_plugin_badge = environment.gamification_plugin_badges.find(params[:id])
  44 + @gamification_plugin_badge.destroy
  45 +
  46 + redirect_to :action => :index
  47 + end
  48 +end
controllers/gamification_plugin_admin_controller.rb
@@ -13,4 +13,16 @@ class GamificationPluginAdminController &lt; PluginAdminController @@ -13,4 +13,16 @@ class GamificationPluginAdminController &lt; PluginAdminController
13 end 13 end
14 end 14 end
15 15
  16 + def new_badge
  17 + if request.post?
  18 + badge = GamificationPlugin::Badge.new(params[:badge])
  19 + badge.owner = environment
  20 + badge.save!
  21 + session[:notice] = 'Settings succefully saved.'
  22 + redirect_to :action => 'index'
  23 + else
  24 + render :file => 'gamification_plugin_admin/new_badge'
  25 + end
  26 + end
  27 +
16 end 28 end
lib/gamification_plugin.rb
@@ -29,12 +29,14 @@ class GamificationPlugin &lt; Noosfero::Plugin @@ -29,12 +29,14 @@ class GamificationPlugin &lt; Noosfero::Plugin
29 }] 29 }]
30 end 30 end
31 31
32 - Merit.setup do |config|  
33 - config.checks_on_each_request = false  
34 - config.user_model_name = 'Profile'  
35 - config.current_user_method = 'current_person'  
36 - end 32 + ActionDispatch::Reloader.to_prepare do
  33 + Merit.setup do |config|
  34 + config.checks_on_each_request = false
  35 + config.user_model_name = 'Profile'
  36 + config.current_user_method = 'current_person'
  37 + end
37 38
38 - require 'merit_ext' 39 + require_dependency 'merit_ext'
  40 + end
39 41
40 end 42 end
lib/gamification_plugin/badge.rb
@@ -6,4 +6,8 @@ class GamificationPlugin::Badge &lt; Noosfero::Plugin::ActiveRecord @@ -6,4 +6,8 @@ class GamificationPlugin::Badge &lt; Noosfero::Plugin::ActiveRecord
6 6
7 serialize :custom_fields 7 serialize :custom_fields
8 8
  9 + def threshold
  10 + (custom_fields || {}).fetch(:threshold, '')
  11 + end
  12 +
9 end 13 end
test/functional/gamification_plugin_badges_controller_test.rb 0 → 100644
@@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
  1 +require 'test_helper'
  2 +
  3 +class GamificationPluginBadgesControllerTest < ActionController::TestCase
  4 +
  5 + setup do
  6 + @environment = Environment.default
  7 + @gamification_plugin_badge = GamificationPlugin::Badge.create!(:name => 'sample_badge', :owner => @environment)
  8 + login_as(create_admin_user(@environment))
  9 + end
  10 +
  11 + should "should get index" do
  12 + get :index
  13 + assert_response :success
  14 + assert_not_nil assigns(:gamification_plugin_badges)
  15 + end
  16 +
  17 + should "should get new" do
  18 + get :new
  19 + assert_response :success
  20 + end
  21 +
  22 + should "should create gamification_plugin_badge" do
  23 + assert_difference('GamificationPlugin::Badge.count') do
  24 + post :create, gamification_plugin_badge: { description: @gamification_plugin_badge.description, level: @gamification_plugin_badge.level, name: @gamification_plugin_badge.name, custom_fields: {threshold: @gamification_plugin_badge.threshold} }
  25 + end
  26 + end
  27 +
  28 + should "should show gamification_plugin_badge" do
  29 + get :show, id: @gamification_plugin_badge
  30 + assert_response :success
  31 + end
  32 +
  33 + should "should get edit" do
  34 + get :edit, id: @gamification_plugin_badge
  35 + assert_response :success
  36 + end
  37 +
  38 + should "should update gamification_plugin_badge" do
  39 + put :update, id: @gamification_plugin_badge, gamification_plugin_badge: { description: @gamification_plugin_badge.description, level: @gamification_plugin_badge.level, name: @gamification_plugin_badge.name, custom_fields: {threshold: @gamification_plugin_badge.threshold} }
  40 + assert assigns(:gamification_plugin_badge)
  41 + end
  42 +
  43 + should "should destroy gamification_plugin_badge" do
  44 + assert_difference('GamificationPlugin::Badge.count', -1) do
  45 + delete :destroy, id: @gamification_plugin_badge
  46 + end
  47 + end
  48 +end
views/gamification_plugin_admin/index.html.erb
@@ -15,3 +15,5 @@ @@ -15,3 +15,5 @@
15 <% end %> 15 <% end %>
16 16
17 <% end %> 17 <% end %>
  18 +
  19 +<%= link_to _('Badges'), :controller => 'gamification_plugin_badges' %>
views/gamification_plugin_badges/_form.html.erb 0 → 100644
@@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
  1 +<%= form_for(@gamification_plugin_badge, :url => {:action => @gamification_plugin_badge.new_record? ? :create : :update}) do |f| %>
  2 + <% if @gamification_plugin_badge.errors.any? %>
  3 + <div id="error_explanation">
  4 + <h2><%= pluralize(@gamification_plugin_badge.errors.count, "error") %> prohibited this gamification_plugin_badge from being saved:</h2>
  5 +
  6 + <ul>
  7 + <% @gamification_plugin_badge.errors.full_messages.each do |msg| %>
  8 + <li><%= msg %></li>
  9 + <% end %>
  10 + </ul>
  11 + </div>
  12 + <% end %>
  13 +
  14 + <div class="field">
  15 + <%= f.label :name %><br />
  16 + <%= f.text_field :name %>
  17 + </div>
  18 + <div class="field">
  19 + <%= f.label :description %><br />
  20 + <%= f.text_field :description %>
  21 + </div>
  22 + <div class="field">
  23 + <%= f.label :level %><br />
  24 + <%= f.text_field :level %>
  25 + </div>
  26 + <%= f.fields_for :custom_fields do |c| %>
  27 + <div class="field">
  28 + <%= c.label :threshold %><br />
  29 + <%= c.text_field :threshold %>
  30 + </div>
  31 + <% end %>
  32 + <div class="actions">
  33 + <%= f.submit %>
  34 + </div>
  35 +<% end %>
views/gamification_plugin_badges/edit.html.erb 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +<h1><%= _('Gamification Plugin: Editing Badge') %></h1>
  2 +
  3 +<%= render 'form' %>
  4 +
  5 +<%= link_to 'Show', :action => :show, :id => @gamification_plugin_badge.id %> |
  6 +<%= link_to 'Back', :action => :index %>
views/gamification_plugin_badges/index.html.erb 0 → 100644
@@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
  1 +<h1><%= _('Gamification Plugin: Listing Badges') %></h1>
  2 +
  3 +<table>
  4 + <tr>
  5 + <th>Name</th>
  6 + <th>Description</th>
  7 + <th>Level</th>
  8 + <th>Threshold</th>
  9 + <th></th>
  10 + <th></th>
  11 + <th></th>
  12 + </tr>
  13 +
  14 +<% @gamification_plugin_badges.each do |gamification_plugin_badge| %>
  15 + <tr>
  16 + <td><%= gamification_plugin_badge.name %></td>
  17 + <td><%= gamification_plugin_badge.description %></td>
  18 + <td><%= gamification_plugin_badge.level %></td>
  19 + <td><%= gamification_plugin_badge.threshold %></td>
  20 + <td><%= link_to 'Show', :action => :show, :id => gamification_plugin_badge.id %></td>
  21 + <td><%= link_to 'Edit', :action => :edit, :id => gamification_plugin_badge.id %></td>
  22 + <td><%= link_to 'Destroy', :action => :destroy, :id => gamification_plugin_badge.id, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  23 + </tr>
  24 +<% end %>
  25 +</table>
  26 +
  27 +<br />
  28 +
  29 +<%= link_to 'New Badge', :action => :new %>
views/gamification_plugin_badges/new.html.erb 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +<h1><%= _('Gamification Plugin: New Badge') %></h1>
  2 +
  3 +<%= render 'form' %>
  4 +
  5 +<%= link_to 'Back', :action => :index %>
views/gamification_plugin_badges/show.html.erb 0 → 100644
@@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
  1 +<h1><%= _('Gamification Plugin: Show Badge') %></h1>
  2 +
  3 +<p id="notice"><%= notice %></p>
  4 +
  5 +<p>
  6 + <b>Name:</b>
  7 + <%= @gamification_plugin_badge.name %>
  8 +</p>
  9 +
  10 +<p>
  11 + <b>Description:</b>
  12 + <%= @gamification_plugin_badge.description %>
  13 +</p>
  14 +
  15 +<p>
  16 + <b>Level:</b>
  17 + <%= @gamification_plugin_badge.level %>
  18 +</p>
  19 +
  20 +<p>
  21 + <b>Threshold:</b>
  22 + <%= @gamification_plugin_badge.threshold %>
  23 +</p>
  24 +
  25 +
  26 +<%= link_to 'Edit', :action => :edit, :id => @gamification_plugin_badge.id %> |
  27 +<%= link_to 'Back', :action => :index %>