Merge pull request #75 from ZeusWPI/feature/69/add-undo-button

Fix #69
This commit is contained in:
benji 2015-10-26 19:08:00 +01:00
commit ffda2a4b26
10 changed files with 37 additions and 10 deletions

View file

@ -0,0 +1,11 @@
ready = function() {
$('[data-remove]').each(function() {
var button = $(this);
setTimeout(function() {
$(button).remove();
}, $(button).data("remove") * 1000);
});
}
$(document).ready(ready);
$(document).on('page:load', ready);

View file

@ -1,6 +1,6 @@
class OrdersController < ApplicationController
load_resource :user
load_and_authorize_resource :order, through: :user, shallow: true
load_and_authorize_resource :order, through: :user
def new
@products = Product.all.for_sale.order(:name)

View file

@ -29,7 +29,7 @@ class UsersController < ApplicationController
order = @user.orders.build
order.order_items.build(count: 1, product: @user.dagschotel)
if order.save
flash[:success] = "Quick pay succeeded. #{view_context.link_to("Undo", [@user, order], method: :delete)}."
flash[:success] = "Quick pay succeeded."
else
flash[:error] = order.errors.full_messages.first
end

View file

@ -10,7 +10,11 @@ module ApplicationHelper
end
def euro_from_cents(f)
euro(f / 100.0)
if f
euro(f / 100.0)
else
"undefined"
end
end
def euro(f)

View file

@ -19,8 +19,8 @@ class Ability
can :create, Order do |order|
order.try(:user) == user
end
can :delete, Order do |order|
order.try(:user) == user && order.created_at > Rails.application.config.call_api_after.ago
can :destroy, Order do |order|
order.try(:user) == user && order.deletable
end
end
end

View file

@ -33,6 +33,14 @@ class Order < ActiveRecord::Base
}.to_sentence
end
def deletable
self.created_at > Rails.application.config.call_api_after.ago
end
def sec_until_remove
self.created_at.to_i - (Time.now - Rails.application.config.call_api_after).to_i
end
private
def calculate_price

View file

@ -51,9 +51,8 @@ class User < ActiveRecord::Base
if result.code == 200
JSON.parse(result.body)["balance"]
else
0
end
rescue
end
end
end

View file

@ -8,7 +8,7 @@
.alert.alert-success.alert-dismissable
%button.close{"aria-hidden" => "true", "data-dismiss" => "alert", type: "button"} ×
%strong Success!
= raw flash[:success]
= flash[:success]
- if flash[:notice]
.alert.alert-info.alert-dismissable
%button.close{"aria-hidden" => "true", "data-dismiss" => "alert", type: "button"} ×

View file

@ -6,3 +6,8 @@
= order.to_sentence
%td
= euro_from_cents(order.price_cents)
%td
- if order.deletable
= button_to "Cancel order (until #{(order.created_at + Rails.application.config.call_api_after).strftime("%H:%M")})", user_order_path(@user, order), method: :delete,
class: "btn btn-danger", data: { remove: order.sec_until_remove }

View file

@ -14,13 +14,13 @@ module Tab002
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
config.time_zone = 'Brussels'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :delayed_job
config.call_api_after = 5.minutes
config.call_api_after = 35.minutes
end
end