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 16a8765..0bc2cd0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,10 +11,19 @@ class ApplicationController < ActionController::Base @current_client ||= identify_client end + def current_ability + if current_user + @current_ability ||= Ability.new(current_user) + elsif current_client + @current_ability ||= ClientAbility.new(current_account) + end + end + private def identify_client key = request.headers["X-API-KEY"] Client.find_by key: key if key 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!"