diff --git a/app/assets/javascripts/remove_button.js b/app/assets/javascripts/remove_button.js new file mode 100644 index 0000000..2215e72 --- /dev/null +++ b/app/assets/javascripts/remove_button.js @@ -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); diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 8511919..c4b7ab3 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -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) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a81a85b..468d86c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7f24330..f34c7ef 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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) diff --git a/app/models/ability.rb b/app/models/ability.rb index 919eb34..416a8db 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -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 diff --git a/app/models/order.rb b/app/models/order.rb index 912f124..7132e8f 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 9c8f7bd..ddab635 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -51,9 +51,8 @@ class User < ActiveRecord::Base if result.code == 200 JSON.parse(result.body)["balance"] - else - 0 end + rescue end end end diff --git a/app/views/application/_flash.html.haml b/app/views/application/_flash.html.haml index 8f535e8..5cad042 100644 --- a/app/views/application/_flash.html.haml +++ b/app/views/application/_flash.html.haml @@ -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"} × diff --git a/app/views/orders/_order.html.haml b/app/views/orders/_order.html.haml index 79b6060..a7322f1 100644 --- a/app/views/orders/_order.html.haml +++ b/app/views/orders/_order.html.haml @@ -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 } + diff --git a/config/application.rb b/config/application.rb index 7163fc6..f8572a5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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