Skip to content
Open
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
1 change: 1 addition & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class PagesController < ApplicationController
# GET /pages
# GET /pages.json
def index
@wiki = Wiki.find(params[:wiki_id])
@pages = Page.all.where(wiki_id: params[:wiki_id])
end

Expand Down
7 changes: 6 additions & 1 deletion app/controllers/wikis_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ class WikisController < ApplicationController
def index
#@user = current_user
#@wikis = Wiki.where(user_id: params[:user_id])
@wikis = Wiki.where(user_id: current_user)
#@wikis = Wiki.where(user_id: current_user)
@wikis = policy_scope(Wiki)
end

# GET /wikis/1
# GET /wikis/1.json
def show
@wiki = Wiki.find(params[:id])
#@authorized_wiki = policy_scope(Wiki)
#if @authorized_wiki.include?(@wiki)
authorize @wiki
#end
end

# GET /wikis/new
Expand Down
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

has_many :collaborations
has_many :wikis, through: :collaborations
after_initialize :init

def role?(base_role)
role == base_role.to_s
end

def admin?
role == 'admin'
end
Expand All @@ -26,5 +31,9 @@ def init
def upgrade
self.role = 'premium'
end

def current_user?
current_user = self
end

end
1 change: 1 addition & 0 deletions app/models/wiki.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Wiki < ActiveRecord::Base
has_many :pages
belongs_to :user
has_many :collaborations
has_many :users, through: :collaborations

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

def show?
user.id == record.user_id or Collaboration.where(:user_id => user.id).length > 0
#scope.where(:id => record.id).exists?
#wiki_user = Wiki.find(record.id).user
#current_wiki = Wiki.find(params[:id])
#collaborations = Collaboration.where(:user_id => user)
#collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id))
#collab_wikis.include?(current_wiki)
end

def add_collaborators?
user.premium? and record.is_private?
end

class Scope
attr_reader :user, :scope

def initialize(user, scope)
@user = user
@scope = scope
end

def resolve
wikis = []
if user.role?(:admin)
wikis = scope.all # if the user is an admin, show them all the wikis
elsif user.role?(:premium)
collaborations = Collaboration.where(:user_id => user)
collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id))
all_wikis = scope.all
all_wikis.each do |wiki|
if !wiki.is_private || wiki.user_id == user.id || collab_wikis.include?(wiki)
wikis << wiki # if the user is premium, only show them public wikis, or that private wikis they created, or private wikis they are a collaborator on
end
end
else # this is the lowly standard user
collaborations = Collaboration.where(:user_id => user)
collab_wikis = Wiki.where(id: collaborations.pluck(:wiki_id))
all_wikis = scope.all
wikis = []
all_wikis.each do |wiki|
if !wiki.is_private? || wiki.user_id == user.id || collab_wikis.include?(wiki)
wikis << wiki # only show standard users public wikis and private wikis they are a collaborator on
end
end
end
wikis # return the wikis array we've built up
end
end

end
18 changes: 17 additions & 1 deletion app/views/collaborations/save.html.haml
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
= @collabs
%p You now have the following collaborators added.
= @collabs
%h1{:align => "center"} My Collaborations
%table
%thead
%tr
%th Name
%th Email

%tbody
- @collabs.each do |collabs|
%tr
%td= User.find(collabs).name
%td= User.find(collabs).email

%p= link_to 'Edit Collaborations', wiki_collaborations_path
%p= link_to 'Back to Wiki', wiki_pages_path
5 changes: 4 additions & 1 deletion app/views/pages/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@

= link_to 'New Page', new_wiki_page_path(params[:wiki_id])
= link_to 'My Wikis', wikis_path
= link_to 'Add Collaborators', wiki_collaborations_path(params[:wiki_id])
-#= debug(Pundit.policy(User.first, Wiki.first).add_collaborators?)
= link_to 'Add Collaborators', wiki_collaborations_path(params[:wiki_id]) if Pundit.policy(current_user, @wiki).add_collaborators?
-#- if @current.current_user?(Wiki.find(params[:wiki_id]).user)
-#= link_to 'Add Collaborators', wiki_collaborations_path(params[:wiki_id])
10 changes: 6 additions & 4 deletions app/views/wikis/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
.field
= f.label :user_id
= f.number_field :user_id
.field
= f.label :is_private
= f.check_box :is_private
- if current_user.role?(:admin) || current_user.role?(:premium)
.field
= f.label :is_private
= f.check_box :is_private

.actions
= f.submit 'Save'
= f.submit 'Save'
3 changes: 2 additions & 1 deletion app/views/wikis/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%th Title
%th User
%th Is private
%th
%th Wiki ID
%th
%th

Expand All @@ -14,6 +14,7 @@
%td= wiki.title
%td= wiki.user_id
%td= wiki.is_private
%td= wiki.id
%td= link_to 'Show', wiki_pages_path(wiki)
%td= link_to 'Edit', edit_wiki_path(wiki)
%td= link_to 'Destroy', wiki, :method => :delete, :data => { :confirm => 'Are you sure?' }
Expand Down