Commit 0b9c84c9 authored by mafolz's avatar mafolz

implement cancel on redemptions and earnings

parent bd78788b
......@@ -32,10 +32,10 @@ gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
gem 'rails-erd'
end
group :development do
gem 'rails-erd'
gem 'ffaker'
gem 'factory_girl_rails'
gem 'rspec-rails'
......
class ChroniclesController < ApplicationController
before_filter :set_chronicle, :only => [:show]
def show
end
protected
def set_chronicle
@chronicle = (Earning.all + Redemption.all).sort do |a,b|
a.created_at <=> b.created_at
end
end
end
class EarningsController < ApplicationController
before_filter :set_earning, :only => [:cancel]
# TODO user should be set
def cancel
@earning.cancel!
redirect_to :back
end
protected
def set_earning
@earning = Earning.find(params[:id])
end
end
class RedemptionsController < ApplicationController
before_filter :set_redemption, :only => [:cancel]
def cancel
@redemption.cancel!
redirect_to :back
end
protected
def set_redemption
@redemption = Redemption.find(params[:id])
end
end
......@@ -18,4 +18,12 @@ class Earning < ActiveRecord::Base
has_one(:cancel, lambda { where(:subject_type => 'Earning') }, {
:foreign_key => :subject_id
})
def cancel!
self.create_cancel({
:subject => self,
:chronicle_text => "Korrektur: #{self.chronicle_text}"
})
end
end
......@@ -19,4 +19,12 @@ class Redemption < ActiveRecord::Base
has_one(:cancel, lambda { where(:subject_type => 'Redemption') }, {
:foreign_key => :subject_id
})
def cancel!
self.create_cancel({
:subject => self,
:chronicle_text => "Korrektur: #{self.chronicle_text}"
})
end
end
<div class="list-group-item row list-group-item-success">
<span class="col-md-2">
+<%= entry.points %>
</span>
<span class="col-md-2">
<%= l(entry.created_at, :format => :short) %>
</span>
<span class="col-md-4">
<%= entry.chronicle_text %>
</span>
<% if entry.cancel.blank? %>
<span class="btn btn-danger col-md-2">
<%= link_to 'Storno', cancel_earning_path(entry), :method => :post %>
</span>
<% end %>
</div>
<% if entry.cancel.present? %>
<%= render({
:partial => 'hackers/chronicle/earning_cancel',
:locals => {:entry => entry.cancel}
}) %>
<% end %>
<div class="list-group-item list-group-item-warning row">
<span class="col-md-1"></span>
<span class="col-md-1">
+<%= entry.points %>
</span>
<span class="col-md-2">
<%= l(entry.created_at, :format => :short) %>
</span>
<span class="col-md-4">
<%= entry.chronicle_text %>
</span>
<% if entry.cancel.blank? %>
<span class="btn btn-danger col-md-2">
<%= link_to 'Storno', cancel_redemption_path(entry), :method => :post %>
</span>
<% end %>
</div>
<% if entry.cancel.present? %>
<%= render({
:partial => 'hackers/chronicle/redemption_cancel',
:locals => {:entry => entry.cancel}
}) %>
<% end %>
<%= render :partial => 'hackers/chronicle', :locals => {:chronicle => @chronicle} %>
<div class="list-group">
<% chronicle.each do |entry| %>
<% if entry.kind_of?(Earning) %>
<div class="list-group-item row list-group-item-success">
<span class="col-md-2">
+<%= entry.points %>
</span>
<span class="col-md-2">
<%= l(entry.created_at, :format => :short) %>
</span>
<span class="col-md-4">
<%= entry.chronicle_text %>
</span>
<% if entry.cancel.blank? %>
<span class="btn btn-danger col-md-2">
<%= link_to 'Storno', '#' %>
</span>
<% end %>
</div>
<% if entry.cancel.present? %>
<%= render({
:partial => 'hackers/chronicle/earning_cancel',
:locals => {:entry => entry.cancel}
}) %>
<% end %>
<%= render :partial => 'chronicles/earning', :locals => {:entry => entry} %>
<% elsif entry.kind_of?(Redemption) %>
<div class="list-group-item list-group-item-warning row">
<span class="col-md-1"></span>
<span class="col-md-1">
+<%= entry.points %>
</span>
<span class="col-md-2">
<%= l(entry.created_at, :format => :short) %>
</span>
<span class="col-md-4">
<%= entry.chronicle_text %>
</span>
<% if entry.cancel.blank? %>
<span class="btn btn-danger col-md-2">
<%= link_to 'Storno', '#' %>
</span>
<% end %>
</div>
<% if entry.cancel.present? %>
<%= render({
:partial => 'hackers/chronicle/redemption_cancel',
:locals => {:entry => entry.cancel}
}) %>
<% end %>
<%= render :partial => 'chronicles/redemption', :locals => {:entry => entry} %>
<% end %>
<% end %>
</div>
......@@ -11,10 +11,9 @@
<a class="navbar-brand" href="/">SpacePoints</a>
</nav>
<div class="container">
<%= yield %>
<%= yield %>
</div>
<%= login_or_username %>
<%= login_or_username %> | <%= link_to 'Historie', chronicle_path %>
</body>
</html>
......@@ -5,6 +5,23 @@ SpacePunkte::Application.routes.draw do
resources :hackers
resource :chronicle, :only => [:show]
resources :earnings, :only => [] do
member do
post :cancel
end
end
resources :redemptions, :only => [] do
member do
post :cancel
end
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
......
File added
FactoryGirl.define do
factory :earning do
points { [1,10,50,150].shuffle.first}
association :hacker, :factory => :hacker
association :user, :factory => :hacker
association :quest, :factory => :quest
end
end
FactoryGirl.define do
factory :quest do
points {[10,20,50,150].shuffle.first}
title {Faker::Lorem.word }
description {Faker::Lorem.sentence }
end
end
FactoryGirl.define do
factory :redemption do
points { [1,10,50,150].shuffle.first}
association :hacker, :factory => :hacker
association :user, :factory => :hacker
association :reward, :factory => :reward
end
end
require 'spec_helper'
describe Earning do
pending "add some examples to (or delete) #{__FILE__}"
let(:earning) do
FactoryGirl.create(:earning, {
})
end
describe '#cancel!' do
it 'should create a cancel Object' do
earning.cancel.should be_blank
earning.cancel!.should be_true
earning.cancel.should be_present
end
end # #cancel!
end
......@@ -12,6 +12,9 @@ describe Redemption do
Redemption.new
end
let(:normal_redemption) do
FactoryGirl.create(:redemption)
end
context 'associations' do
it 'should have a user which is instance of hacker' do
redemption.build_user.should be_kind_of(Hacker)
......@@ -24,4 +27,13 @@ describe Redemption do
negative_points_redemption.errors[:points].should be_present
end
end # validations
describe '#cancel!' do
it 'should produce a cancel object' do
normal_redemption.cancel.should be_blank
normal_redemption.cancel!.should be_true
normal_redemption.cancel.should be_present
end
end # #cancel!
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment