From 2a72c1e9085bad0b12fc639d21054917aa9e71d3 Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Tue, 8 Dec 2015 16:36:34 -0500 Subject: [PATCH] Initial Submission Index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This handles authorization and views of the Index and Sword Viewing for Submissions. part of #2 Upcoming will be for some sort of status for users in the index view. I’m not sure yet if we’ll need “show” pages. --- Gemfile | 1 + Gemfile.lock | 2 + app/controllers/application_controller.rb | 4 ++ app/controllers/submissions_controller.rb | 15 +++++- app/models/ability.rb | 10 ++++ app/models/user.rb | 1 + app/views/layouts/application.html.erb | 1 + app/views/submissions/index.html.erb | 18 +++++++ config/routes.rb | 3 +- .../20151208173727_add_admin_to_user.rb | 5 ++ db/schema.rb | 11 +++-- .../submissions_controller_test.rb | 48 ++++++++++++++++++ ...est.rb => submission_create_pages_test.rb} | 25 ++++------ test/features/submission_index_pages_test.rb | 49 +++++++++++++++++++ test/fixtures/users.yml | 14 ++++-- test/models/user_test.rb | 1 + test/test_helper.rb | 21 +++++++- 17 files changed, 202 insertions(+), 27 deletions(-) create mode 100644 app/models/ability.rb create mode 100644 app/views/submissions/index.html.erb create mode 100644 db/migrate/20151208173727_add_admin_to_user.rb rename test/features/{submission_test.rb => submission_create_pages_test.rb} (81%) create mode 100644 test/features/submission_index_pages_test.rb diff --git a/Gemfile b/Gemfile index acf0d78..9a01326 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ ruby '2.2.3' gem 'rails', '4.2.5' gem 'bootstrap_form' +gem 'cancancan' gem 'carrierwave', github: 'carrierwaveuploader/carrierwave' gem 'devise' gem 'http_logger' diff --git a/Gemfile.lock b/Gemfile.lock index 2f382e7..e10418d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,6 +60,7 @@ GEM bootstrap_form (2.3.0) builder (3.2.2) byebug (8.2.1) + cancancan (1.13.1) capybara (2.5.0) mime-types (>= 1.16) nokogiri (>= 1.3.3) @@ -267,6 +268,7 @@ DEPENDENCIES annotate bootstrap_form byebug + cancancan carrierwave! coveralls devise diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1eeeef4..9c20a99 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,6 +3,10 @@ class ApplicationController < ActionController::Base # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + rescue_from CanCan::AccessDenied do |exception| + redirect_to root_url, alert: exception.message + end + def new_session_path(_scope) root_path end diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index 4ad15f1..a6285a1 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -1,5 +1,14 @@ class SubmissionsController < ApplicationController before_action :authenticate_user! + load_and_authorize_resource + + def index + if current_user.admin? + @submissions = Submission.all.order(created_at: :desc) + else + @submissions = current_user.submissions.order(created_at: :desc) + end + end def new @submission = Submission.new @@ -12,12 +21,16 @@ def create if @submission.save process_submission(@submission) flash.notice = 'Your Submission is now in progress.' - redirect_to root_path + redirect_to submissions_path else render 'new' end end + def package + send_file(Submission.find_by_id(params[:id]).sword_path) + end + private def process_submission(submission) diff --git a/app/models/ability.rb b/app/models/ability.rb new file mode 100644 index 0000000..45519fe --- /dev/null +++ b/app/models/ability.rb @@ -0,0 +1,10 @@ +class Ability + include CanCan::Ability + + def initialize(user) + can :manage, Submission if user.admin? + can [:create, :read], Submission, user: user + # See the wiki for details: + # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities + end +end diff --git a/app/models/user.rb b/app/models/user.rb index fdc0b96..364cdc6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,7 @@ # uid :string not null # created_at :datetime not null # updated_at :datetime not null +# admin :boolean # class User < ActiveRecord::Base diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 668c596..6950e4d 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -34,6 +34,7 @@