From 43d949dc1d21404cb77dd2b13c3b0ed64519a4a8 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Fri, 25 Jan 2013 03:12:25 -0700 Subject: [PATCH] Adding certifications; messed up adding certification_users so not staging those --- .../javascripts/certifications.js.coffee | 3 + .../stylesheets/certifications.css.scss | 3 + app/controllers/certifications_controller.rb | 77 +++++++++++++++++++ app/helpers/certifications_helper.rb | 2 + app/models/ability.rb | 10 ++- app/models/certification.rb | 4 + app/models/user.rb | 3 +- app/views/certifications/_form.html.erb | 29 +++++++ app/views/certifications/edit.html.erb | 6 ++ app/views/certifications/index.html.erb | 15 ++++ app/views/certifications/new.html.erb | 5 ++ app/views/certifications/show.html.erb | 20 +++++ app/views/layouts/application.html.erb | 1 + app/views/users/show.html.erb | 9 +++ config/routes.rb | 2 + .../20130125085218_create_certifications.rb | 10 +++ test/fixtures/certifications.yml | 9 +++ .../certifications_controller_test.rb | 49 ++++++++++++ test/unit/certification_test.rb | 7 ++ .../helpers/certifications_helper_test.rb | 4 + 20 files changed, 264 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/certifications.js.coffee create mode 100644 app/assets/stylesheets/certifications.css.scss create mode 100644 app/controllers/certifications_controller.rb create mode 100644 app/helpers/certifications_helper.rb create mode 100644 app/models/certification.rb create mode 100644 app/views/certifications/_form.html.erb create mode 100644 app/views/certifications/edit.html.erb create mode 100644 app/views/certifications/index.html.erb create mode 100644 app/views/certifications/new.html.erb create mode 100644 app/views/certifications/show.html.erb create mode 100644 db/migrate/20130125085218_create_certifications.rb create mode 100644 test/fixtures/certifications.yml create mode 100644 test/functional/certifications_controller_test.rb create mode 100644 test/unit/certification_test.rb create mode 100644 test/unit/helpers/certifications_helper_test.rb diff --git a/app/assets/javascripts/certifications.js.coffee b/app/assets/javascripts/certifications.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/certifications.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/certifications.css.scss b/app/assets/stylesheets/certifications.css.scss new file mode 100644 index 0000000..2e35715 --- /dev/null +++ b/app/assets/stylesheets/certifications.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Certifications controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/certifications_controller.rb b/app/controllers/certifications_controller.rb new file mode 100644 index 0000000..5cda4ad --- /dev/null +++ b/app/controllers/certifications_controller.rb @@ -0,0 +1,77 @@ +class CertificationsController < ApplicationController + load_and_authorize_resource :certification + load_and_authorize_resource :user, :through => :certification + before_filter :authenticate_user! + + # GET /certifications + # GET /certifications.json + def index + @certifications = @certifications.sort_by(&:name) + + respond_to do |format| + format.html # index.html.erb + format.json { render :json => @certifications } + end + end + + # GET /certifications/1 + # GET /certifications/1.json + def show + respond_to do |format| + format.html # show.html.erb + format.json { render :json => @certification } + end + end + + # GET /certifications/new + # GET /certifications/new.json + def new + respond_to do |format| + format.html # new.html.erb + format.json { render :json => @certification } + end + end + + # GET /certifications/1/edit + def edit + end + + # POST /certifications + # POST /certifications.json + def create + respond_to do |format| + if @certification.save + format.html { redirect_to Certification, :notice => 'Certification was successfully created.' } + format.json { render :json => @certification, :status => :created, :location => @certification } + else + format.html { render :action => "new" } + format.json { render :json => @certification.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /certifications/1 + # PUT /certifications/1.json + def update + respond_to do |format| + if @certification.update_attributes(params[:certification]) + format.html { redirect_to Certification, :notice => 'Certification was successfully updated.' } + format.json { head :no_content } + else + format.html { render :action => "edit" } + format.json { render :json => @certification.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /certifications/1 + # DELETE /certifications/1.json + def destroy + @certification.destroy + + respond_to do |format| + format.html { redirect_to certifications_url } + format.json { head :no_content } + end + end +end diff --git a/app/helpers/certifications_helper.rb b/app/helpers/certifications_helper.rb new file mode 100644 index 0000000..906dc2e --- /dev/null +++ b/app/helpers/certifications_helper.rb @@ -0,0 +1,2 @@ +module CertificationsHelper +end diff --git a/app/models/ability.rb b/app/models/ability.rb index 975bfa9..3201c8e 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -5,10 +5,14 @@ class Ability if !user.nil? if user.admin? can :manage, :all - else - can :read, User - can :read, Card, :user_id => user.id end + if user.instructor? + can :manage, Certification + end + + can :read, User + can :read, Certification + can :read, Card, :user_id => user.id end # Define abilities for the passed in user here. For example: # diff --git a/app/models/certification.rb b/app/models/certification.rb new file mode 100644 index 0000000..665688d --- /dev/null +++ b/app/models/certification.rb @@ -0,0 +1,4 @@ +class Certification < ActiveRecord::Base + attr_accessible :description, :name + has_many :users, :through => :certifications_users +end diff --git a/app/models/user.rb b/app/models/user.rb index 9c34875..a64b895 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,7 +6,8 @@ class User < ActiveRecord::Base :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model - attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :admin, :active, :emergency_name, :emergency_phone, :current_skills, :desired_skills, :waiver, :emergency_email, :phone, :payment_method, :orientation, :member_level + attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :admin, :instructor, :active, :emergency_name, :emergency_phone, :current_skills, :desired_skills, :waiver, :emergency_email, :phone, :payment_method, :orientation, :member_level has_many :cards + has_many :certifications, :through => :certifications_users end diff --git a/app/views/certifications/_form.html.erb b/app/views/certifications/_form.html.erb new file mode 100644 index 0000000..0eb73e4 --- /dev/null +++ b/app/views/certifications/_form.html.erb @@ -0,0 +1,29 @@ +<%= form_for(@certification) do |f| %> + <% if @certification.errors.any? %> +
+

<%= pluralize(@certification.errors.count, "error") %> prohibited this certification from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
+ <%= f.label :description %>
+ <%= f.text_area :description %> +
+ <% f.fields_for :users do |u| %> + <%= u.label :user %>
+ <%= collection_select(:certifications_users, :user_id, User.all.sort_by(&:name), :id, :name) %> + <% end %> +
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/certifications/edit.html.erb b/app/views/certifications/edit.html.erb new file mode 100644 index 0000000..d98491a --- /dev/null +++ b/app/views/certifications/edit.html.erb @@ -0,0 +1,6 @@ +

Editing certification

+ +<%= render 'form' %> + +<%= link_to 'Show', @certification %> | +<%= link_to 'Back', certifications_path %> diff --git a/app/views/certifications/index.html.erb b/app/views/certifications/index.html.erb new file mode 100644 index 0000000..5d584b4 --- /dev/null +++ b/app/views/certifications/index.html.erb @@ -0,0 +1,15 @@ +

Listing certifications

+ +<%= link_to 'New Certification', new_certification_path if can? :create, Certification %> + + + +
+ diff --git a/app/views/certifications/new.html.erb b/app/views/certifications/new.html.erb new file mode 100644 index 0000000..5bb992d --- /dev/null +++ b/app/views/certifications/new.html.erb @@ -0,0 +1,5 @@ +

New certification

+ +<%= render 'form' %> + +<%= link_to 'Back', certifications_path %> diff --git a/app/views/certifications/show.html.erb b/app/views/certifications/show.html.erb new file mode 100644 index 0000000..3b7bab1 --- /dev/null +++ b/app/views/certifications/show.html.erb @@ -0,0 +1,20 @@ +

+ Name: + <%= @certification.name %> +

+ +

+ Description: + <%= simple_format @certification.description %> +

+ +Certified Users: + + +<% if can? :update, @certification %><%= link_to 'Edit', edit_certification_path(@certification) %> |<% end %> +<%= link_to 'Back', certifications_path %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4065eb7..0afe6dc 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,6 +10,7 @@