Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #31

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Dev #31

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'
Expand Down
13 changes: 10 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -227,19 +232,21 @@ DEPENDENCIES
jbuilder (~> 2.0)
jquery-rails
launchy
mysql2
pundit
quiet_assets
rails (= 4.2.5)
rails_layout
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
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
18 changes: 18 additions & 0 deletions app/assets/stylesheets/styles.scss
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
76 changes: 76 additions & 0 deletions app/controllers/labels_controller.rb
Original file line number Diff line number Diff line change
@@ -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('<br>').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('<br>').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
72 changes: 72 additions & 0 deletions app/controllers/user_labels_controller.rb
Original file line number Diff line number Diff line change
@@ -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('<br>').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('<br>').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
2 changes: 2 additions & 0 deletions app/controllers/visitors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class VisitorsController < ApplicationController
def index
end
end
2 changes: 2 additions & 0 deletions app/helpers/labels_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LabelsHelper
end
7 changes: 7 additions & 0 deletions app/models/label.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions app/models/user_label.rb
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/policies/label_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class LabelPolicy < ApplicationPolicy

end
3 changes: 3 additions & 0 deletions app/policies/user_label_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class UserLabelPolicy < ApplicationPolicy

end
3 changes: 3 additions & 0 deletions app/views/errors/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span class="text-red">
<%= flash[:error] %>
</span>
12 changes: 12 additions & 0 deletions app/views/labels/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= simple_form_for @label, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :name %>
<div class="form-group string required label_colour">
<%= f.label 'Colour' %>
<%= f.color_field :colour %>
</div>


<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to 'Cancel',
labels_path, :class => 'btn btn-default' %>
<% end %>
Loading