From 321d0ea7166558b7cb94a908458749e6091a1fbe Mon Sep 17 00:00:00 2001 From: benji Date: Wed, 9 Sep 2015 11:24:45 +0200 Subject: [PATCH] Add ClientAbility --- Gemfile | 2 ++ Gemfile.lock | 4 ++++ app/controllers/application_controller.rb | 8 ++++++++ app/models/client_ability.rb | 8 ++++++++ app/views/transactions/new.html.haml | 6 +++--- 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 app/models/client_ability.rb diff --git a/Gemfile b/Gemfile index ab0fe33..85db2d0 100644 --- a/Gemfile +++ b/Gemfile @@ -82,3 +82,5 @@ group :production do end gem 'high_voltage', '~> 2.4.0' + +gem 'simple_form' diff --git a/Gemfile.lock b/Gemfile.lock index 4ffca3f..3a644d4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -217,6 +217,9 @@ GEM select2-rails (3.5.9.3) thor (~> 0.14) sexp_processor (4.6.0) + simple_form (3.1.1) + actionpack (~> 4.0) + activemodel (~> 4.0) spring (1.3.6) sprockets (3.3.4) rack (~> 1.0) @@ -276,6 +279,7 @@ DEPENDENCIES sass-rails (~> 5.0) sdoc (~> 0.4.0) select2-rails + simple_form spring sqlite3 turbolinks diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3ea39cf..6ee6fac 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,4 +6,12 @@ class ApplicationController < ActionController::Base rescue_from CanCan::AccessDenied do |exception| redirect_to root_url, alert: exception.message end + + def current_ability + if current_user + @current_ability ||= Ability.new(current_user) + elsif current_client + @current_ability ||= ClientAbility.new(current_account) + end + end end diff --git a/app/models/client_ability.rb b/app/models/client_ability.rb new file mode 100644 index 0000000..1f804eb --- /dev/null +++ b/app/models/client_ability.rb @@ -0,0 +1,8 @@ +class Ability + include CanCan::Ability + + def initialize(client) + client ||= Client.new # guest user (not logged in) + can :manage, :all + end +end diff --git a/app/views/transactions/new.html.haml b/app/views/transactions/new.html.haml index d65c6d5..68d35f0 100644 --- a/app/views/transactions/new.html.haml +++ b/app/views/transactions/new.html.haml @@ -1,6 +1,6 @@ = @transaction.errors.full_messages.join(", ") -= form_for @transaction do |f| += simple_form_for @transaction do |f| = f.collection_select :creditor_id, User.all, :id, :name, {}, { class: 'select2-selector' } - = f.number_field :amount - = f.text_field :message, required: true + = f.input :amount + = f.input :message, required: true = f.submit "Send it!"