diff --git a/Gemfile b/Gemfile
index 30dc851..b9ff15f 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,13 +1,20 @@
source 'https://rubygems.org'
-ruby '2.2.3'
+# ruby '2.2.3'
gem 'rails', '4.2.5'
-gem 'sqlite3'
+# gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
+gem 'mysql2'
+gem 'simple_form'
+
+
+
+
+
group :development, :test do
gem 'byebug'
end
@@ -29,6 +36,8 @@ group :development, :test do
gem 'factory_girl_rails'
gem 'faker'
gem 'rspec-rails'
+ gem 'shoulda-matchers', '~> 3.1'
+
end
group :test do
gem 'capybara'
diff --git a/Gemfile.lock b/Gemfile.lock
index eaf49ee..2e9c165 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -111,6 +111,7 @@ GEM
mini_portile (0.6.2)
minitest (5.8.2)
multi_json (1.11.2)
+ mysql2 (0.4.4)
nokogiri (1.6.6.3)
mini_portile (~> 0.6.0)
orm_adapter (0.5.0)
@@ -179,6 +180,11 @@ GEM
multi_json (~> 1.0)
rubyzip (~> 1.0)
websocket (~> 1.0)
+ shoulda-matchers (3.1.1)
+ activesupport (>= 4.0.0)
+ simple_form (3.2.1)
+ actionpack (> 4, < 5.1)
+ activemodel (> 4, < 5.1)
spring (1.4.3)
spring-commands-rspec (1.0.4)
spring (>= 0.9.1)
@@ -188,7 +194,6 @@ GEM
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
- sqlite3 (1.3.11)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.1)
@@ -227,6 +232,7 @@ DEPENDENCIES
jbuilder (~> 2.0)
jquery-rails
launchy
+ mysql2
pundit
quiet_assets
rails (= 4.2.5)
@@ -234,12 +240,13 @@ DEPENDENCIES
rspec-rails
sass-rails (~> 5.0)
selenium-webdriver
+ shoulda-matchers (~> 3.1)
+ simple_form
spring
spring-commands-rspec
- sqlite3
turbolinks
uglifier (>= 1.3.0)
web-console (~> 2.0)
BUNDLED WITH
- 1.10.6
+ 1.12.5
diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss
index a443db3..88dc3f7 100644
--- a/app/assets/stylesheets/application.css.scss
+++ b/app/assets/stylesheets/application.css.scss
@@ -10,6 +10,7 @@
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
+ *= require styles
*= require_tree .
*= require_self
*/
diff --git a/app/assets/stylesheets/styles.scss b/app/assets/stylesheets/styles.scss
new file mode 100644
index 0000000..904ae87
--- /dev/null
+++ b/app/assets/stylesheets/styles.scss
@@ -0,0 +1,18 @@
+.v-spacer1 {
+ height: 1em;
+}
+
+.text-red {
+ color: red;
+}
+
+.label-colour-display {
+ width: 100px;
+ height: 15px;
+ vertical-align: bottom;
+ margin-left: 10px;
+}
+
+span.label {
+ padding: 5px 30px;
+}
\ No newline at end of file
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index d83690e..a75b747 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,5 +1,15 @@
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
+ include Pundit
protect_from_forgery with: :exception
+
+ rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
+
+ private
+
+ def user_not_authorized
+ flash[:alert] = "Access Denied. You are not authorized to perform this action."
+ redirect_to(request.referrer || root_path)
+ end
end
diff --git a/app/controllers/labels_controller.rb b/app/controllers/labels_controller.rb
new file mode 100644
index 0000000..7abe8a1
--- /dev/null
+++ b/app/controllers/labels_controller.rb
@@ -0,0 +1,76 @@
+class LabelsController < ApplicationController
+ before_action :authenticate_user!
+ after_action :verify_authorized
+
+ before_action :set_label, :only => [:edit, :update, :destroy]
+
+
+ def index
+ authorize Label
+ @labels = Label.all
+ end
+
+ def new
+ authorize Label
+ @label = Label.new
+ end
+
+ def edit
+ authorize Label
+
+ end
+
+ def create
+ @label = Label.new(permitted_label_params)
+ authorize Label
+
+ if @label.save
+ flash[:success] = 'Label successfully created!'
+ @labels = Label.all
+ render 'labels/index'
+ else
+ flash[:error] = @label.errors.full_messages.join('
').html_safe
+ render 'labels/new'
+ end
+ end
+
+ def update
+ authorize Label
+ if @label.update(permitted_label_params)
+ flash[:success] = 'Label successfully updated!'
+ @labels = Label.all
+ render 'labels/index'
+ else
+ flash[:error] = @label.errors.full_messages.join('
').html_safe
+ render 'labels/edit'
+
+ end
+
+
+ end
+
+ def destroy
+ authorize Label
+
+ @label.destroy
+ flash[:success] = 'Label Successfully destroyed'
+
+ @labels = Label.all
+ render 'labels/index'
+ end
+
+ private
+ def set_label
+ begin
+ @label = Label.find(params[:id])
+ rescue ActiveRecord::RecordNotFound => e
+ flash[:error] = 'Label not found'
+ render 'errors/index'
+ end
+ end
+
+ def permitted_label_params
+ params.require(:label).permit(:name, :colour)
+ end
+
+end
diff --git a/app/controllers/user_labels_controller.rb b/app/controllers/user_labels_controller.rb
new file mode 100644
index 0000000..366ddda
--- /dev/null
+++ b/app/controllers/user_labels_controller.rb
@@ -0,0 +1,72 @@
+class UserLabelsController < ApplicationController
+ before_action :authenticate_user!
+ after_action :verify_authorized
+
+ before_action :set_user_label, :only => [:edit, :update, :destroy]
+
+ def index
+ authorize UserLabel
+ @user_labels = UserLabel.all
+ end
+
+ def new
+ authorize UserLabel
+ @user_label = UserLabel.new
+ end
+
+ def create
+ @user_label = UserLabel.new(permitted_user_label_params)
+ authorize UserLabel
+
+ if @user_label.save
+ flash[:success] = 'Label for user successfully created'
+ @user_labels = UserLabel.all
+ render 'user_labels/index'
+ else
+ flash[:error] = @user_label.errors.full_messages.join('
').html_safe
+ render 'user_labels/new'
+ end
+ end
+
+ def edit
+ authorize UserLabel
+
+ end
+
+ def update
+ authorize UserLabel
+ if @user_label.update(permitted_user_label_params)
+ flash[:success] = 'User Label successfully updated!'
+ @user_labels = UserLabel.all
+ render 'user_labels/index'
+ else
+ flash[:error] = @user_label.errors.full_messages.join('
').html_safe
+ render 'user_labels/edit'
+
+ end
+ end
+
+ def destroy
+ authorize UserLabel
+ @user_label.destroy
+ flash[:success] = 'User Label Successfully destroyed'
+
+ @user_labels = UserLabel.all
+ render 'user_labels/index'
+ end
+
+ private
+ def set_user_label
+ begin
+ @user_label = UserLabel.find(params[:id])
+ rescue ActiveRecord::RecordNotFound => e
+ flash[:error] = 'User Label not found'
+ render 'errors/index'
+ end
+ end
+ def permitted_user_label_params
+ params.require(:user_label).permit(:user_id, :label_id)
+ end
+
+
+end
\ No newline at end of file
diff --git a/app/controllers/visitors_controller.rb b/app/controllers/visitors_controller.rb
index ebe5fb6..9ef4052 100644
--- a/app/controllers/visitors_controller.rb
+++ b/app/controllers/visitors_controller.rb
@@ -1,2 +1,4 @@
class VisitorsController < ApplicationController
+ def index
+ end
end
diff --git a/app/helpers/labels_helper.rb b/app/helpers/labels_helper.rb
new file mode 100644
index 0000000..9802e31
--- /dev/null
+++ b/app/helpers/labels_helper.rb
@@ -0,0 +1,2 @@
+module LabelsHelper
+end
diff --git a/app/models/label.rb b/app/models/label.rb
new file mode 100644
index 0000000..cd28a6d
--- /dev/null
+++ b/app/models/label.rb
@@ -0,0 +1,7 @@
+class Label < ActiveRecord::Base
+ validates_presence_of :name, :colour
+ validates_uniqueness_of :colour, scope: :name, message: "- Label already has this colour"
+
+ has_many :user_labels, :dependent => :destroy
+
+end
\ No newline at end of file
diff --git a/app/models/user.rb b/app/models/user.rb
index 9e36969..df5306a 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -2,6 +2,8 @@ class User < ActiveRecord::Base
enum role: [:user, :vip, :admin]
after_initialize :set_default_role, :if => :new_record?
+ has_many :user_labels
+
def set_default_role
self.role ||= :user
end
diff --git a/app/models/user_label.rb b/app/models/user_label.rb
new file mode 100644
index 0000000..e1807d9
--- /dev/null
+++ b/app/models/user_label.rb
@@ -0,0 +1,9 @@
+class UserLabel < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :label
+
+ validates_presence_of :user_id, :label_id
+ validates_uniqueness_of :label_id, scope: :user_id, message: " already has this label."
+
+
+end
\ No newline at end of file
diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb
new file mode 100644
index 0000000..c4b9c4b
--- /dev/null
+++ b/app/policies/application_policy.rb
@@ -0,0 +1,53 @@
+class ApplicationPolicy
+ attr_reader :user, :record
+
+ def initialize(user, record)
+ @user = user
+ @record = record
+ end
+
+ def index?
+ @user.admin?
+ end
+
+ # def show?
+ # @user.admin?
+ # end
+
+ def create?
+ @user.admin?
+ end
+
+ def new?
+ @user.admin?
+ end
+
+ def update?
+ @user.admin?
+ end
+
+ def edit?
+ @user.admin?
+ end
+
+ def destroy?
+ @user.admin?
+ end
+
+ def scope
+ Pundit.policy_scope!(user, record.class)
+ end
+
+ class Scope
+ attr_reader :user, :scope
+
+ def initialize(user, scope)
+ @user = user
+ @scope = scope
+ end
+
+ def resolve
+ scope
+ end
+ end
+end
diff --git a/app/policies/label_policy.rb b/app/policies/label_policy.rb
new file mode 100644
index 0000000..d6bb415
--- /dev/null
+++ b/app/policies/label_policy.rb
@@ -0,0 +1,3 @@
+class LabelPolicy < ApplicationPolicy
+
+end
diff --git a/app/policies/user_label_policy.rb b/app/policies/user_label_policy.rb
new file mode 100644
index 0000000..54fb4e6
--- /dev/null
+++ b/app/policies/user_label_policy.rb
@@ -0,0 +1,3 @@
+class UserLabelPolicy < ApplicationPolicy
+
+end
diff --git a/app/views/errors/index.html.erb b/app/views/errors/index.html.erb
new file mode 100644
index 0000000..4e5e50c
--- /dev/null
+++ b/app/views/errors/index.html.erb
@@ -0,0 +1,3 @@
+
+ <%= flash[:error] %>
+
\ No newline at end of file
diff --git a/app/views/labels/_form.html.erb b/app/views/labels/_form.html.erb
new file mode 100644
index 0000000..8900835
--- /dev/null
+++ b/app/views/labels/_form.html.erb
@@ -0,0 +1,12 @@
+<%= simple_form_for @label, :html => { :class => 'form-horizontal' } do |f| %>
+ <%= f.input :name %>
+
<%= model_class.human_attribute_name(:id) %> | +<%= model_class.human_attribute_name(:name) %> | +<%= model_class.human_attribute_name(:colour) %> | +Edit/Delete | +
---|---|---|---|