Skip to content

Commit

Permalink
Merge pull request #12 from meyerhoferc/user-groups
Browse files Browse the repository at this point in the history
User groups
  • Loading branch information
meyerhoferc authored Oct 28, 2018
2 parents 19246d4 + ec61222 commit ae745f3
Show file tree
Hide file tree
Showing 27 changed files with 343 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0
2.3.1
122 changes: 61 additions & 61 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (5.2.0)
actionpack (= 5.2.0)
actioncable (5.2.1)
actionpack (= 5.2.1)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailer (5.2.0)
actionpack (= 5.2.0)
actionview (= 5.2.0)
activejob (= 5.2.0)
actionmailer (5.2.1)
actionpack (= 5.2.1)
actionview (= 5.2.1)
activejob (= 5.2.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.2.0)
actionview (= 5.2.0)
activesupport (= 5.2.0)
actionpack (5.2.1)
actionview (= 5.2.1)
activesupport (= 5.2.1)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.0)
activesupport (= 5.2.0)
actionview (5.2.1)
activesupport (= 5.2.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.2.0)
activesupport (= 5.2.0)
activejob (5.2.1)
activesupport (= 5.2.1)
globalid (>= 0.3.6)
activemodel (5.2.0)
activesupport (= 5.2.0)
activerecord (5.2.0)
activemodel (= 5.2.0)
activesupport (= 5.2.0)
activemodel (5.2.1)
activesupport (= 5.2.1)
activerecord (5.2.1)
activemodel (= 5.2.1)
activesupport (= 5.2.1)
arel (>= 9.0)
activestorage (5.2.0)
actionpack (= 5.2.0)
activerecord (= 5.2.0)
activestorage (5.2.1)
actionpack (= 5.2.1)
activerecord (= 5.2.1)
marcel (~> 0.3.1)
activesupport (5.2.0)
activesupport (5.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
Expand All @@ -47,11 +47,11 @@ GEM
arel (9.0.0)
bcrypt (3.1.12)
bindex (0.5.0)
bootsnap (1.3.1)
bootsnap (1.3.2)
msgpack (~> 1.0)
builder (3.2.3)
byebug (10.0.2)
capybara (3.5.1)
capybara (3.8.2)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
Expand All @@ -75,7 +75,7 @@ GEM
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (1.0.1)
i18n (1.1.0)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
Expand All @@ -91,71 +91,71 @@ GEM
nokogiri (>= 1.5.9)
mail (2.7.0)
mini_mime (>= 0.1.1)
marcel (0.3.2)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.0)
mimemagic (0.3.2)
mini_mime (1.0.0)
mini_mime (1.0.1)
mini_portile2 (2.3.0)
minitest (5.11.3)
msgpack (1.2.4)
multi_json (1.13.1)
nio4r (2.3.1)
nokogiri (1.8.4)
mini_portile2 (~> 2.3.0)
pg (1.0.0)
public_suffix (3.0.2)
pg (1.1.3)
public_suffix (3.0.3)
puma (3.12.0)
rack (2.0.5)
rack-test (1.0.0)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.0)
actioncable (= 5.2.0)
actionmailer (= 5.2.0)
actionpack (= 5.2.0)
actionview (= 5.2.0)
activejob (= 5.2.0)
activemodel (= 5.2.0)
activerecord (= 5.2.0)
activestorage (= 5.2.0)
activesupport (= 5.2.0)
rails (5.2.1)
actioncable (= 5.2.1)
actionmailer (= 5.2.1)
actionpack (= 5.2.1)
actionview (= 5.2.1)
activejob (= 5.2.1)
activemodel (= 5.2.1)
activerecord (= 5.2.1)
activestorage (= 5.2.1)
activesupport (= 5.2.1)
bundler (>= 1.3.0)
railties (= 5.2.0)
railties (= 5.2.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.2.0)
actionpack (= 5.2.0)
activesupport (= 5.2.0)
railties (5.2.1)
actionpack (= 5.2.1)
activesupport (= 5.2.1)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
thor (>= 0.19.0, < 2.0)
rake (12.3.1)
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-rails (3.7.2)
rspec-support (~> 3.8.0)
rspec-rails (3.8.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
ruby_dep (1.5.0)
sass (3.5.7)
sass (3.6.0)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
Expand All @@ -178,9 +178,9 @@ GEM
tilt (2.0.8)
tzinfo (1.2.5)
thread_safe (~> 0.1)
uglifier (4.1.15)
uglifier (4.1.19)
execjs (>= 0.3.0, < 3)
web-console (3.6.2)
web-console (3.7.0)
actionview (>= 5.0)
activemodel (>= 5.0)
bindex (>= 0.4.0)
Expand Down Expand Up @@ -217,4 +217,4 @@ RUBY VERSION
ruby 2.3.1p112

BUNDLED WITH
1.14.6
1.16.3
6 changes: 5 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
class ApplicationController < ActionController::Base
helper_method :current_user
helper_method :current_user, :authorized_user

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end

def authorized_user(other_user)
current_user.id == other_user.id
end
end
8 changes: 8 additions & 0 deletions app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class DashboardController < ApplicationController
def show
if !current_user
redirect_to root_path
flash[:warning] = 'You must be logged in first.'
end
end
end
53 changes: 53 additions & 0 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class GroupsController < ApplicationController
before_action :set_group, only: [:show, :edit, :update, :destroy]

def index
@groups = Group.all
end

def new
@group = Group.new
end

def create
@group = Group.new(group_params)
@group.user_ids= current_user.id
if @group.save
redirect_to dashboard_path
else
render 'new'
end
end

def show
end

def edit
end

def update
if @group.update(group_params)
flash[:notice] = "Group '#{@group.name}' updated!"
redirect_to group_path(@group)
else
flash[:warning] = 'An error occurred, please try again.'
render 'edit'
end
end

def destroy
@group.destroy
flash[:notice] = 'Group Deleted!'
redirect_to dashboard_path
end

private

def set_group
@group = Group.find(params[:id])
end

def group_params
params.require(:group).permit(:name, :description)
end
end
39 changes: 34 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class UsersController < ApplicationController
before_action :set_user, only: [:edit, :update]
def new
@user = User.new
if current_user
redirect_to root_path
redirect_to dashboard_path
else
render 'new'
end
Expand All @@ -11,24 +12,52 @@ def new
def create
@user = User.new(user_params)
if @user.save
flash[:notice] = "Account successfully created."
redirect_to login_path
else
render 'new'
end
end

def show
if current_user
@user = User.find(session[:user_id])
render 'show'
else
@user = params[:id] ? User.find(params[:id]) : current_user
if !current_user
redirect_to root_url
flash[:warning] = 'You must be logged in first.'
end
end

def edit
end

def update
segment_string = params[:commit].split(' ')[1]
if @user.update(validate_params(segment_string))
flash[:notice] = "#{segment_string} successfully updated."
redirect_to profile_path
else
flash[:warning] = "An error occurred, please try again."
render 'edit'
end
end

private

def set_user
@user = current_user
end

def validate_params(string)
case string
when 'Name'
return params.require(:user).permit(:first_name, :last_name)
when 'Email'
return params.require(:user).permit(:email)
when 'Password'
return params.require(:user).permit(:password, :password_confirmation)
end
end

def user_params
params.require(:user).permit(:email, :first_name, :last_name, :password, :password_confirmation)
end
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class WelcomeController < ApplicationController
def index
if current_user
redirect_to dashboard_path
end
end
end
4 changes: 4 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Group < ApplicationRecord
has_many :user_groups
has_many :users, through: :user_groups
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class User < ApplicationRecord
has_many :user_groups
has_many :groups, through: :user_groups
has_secure_password
validates_uniqueness_of :email
validates :first_name, presence: true
Expand Down
4 changes: 4 additions & 0 deletions app/models/user_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserGroup < ApplicationRecord
belongs_to :user
belongs_to :group
end
14 changes: 14 additions & 0 deletions app/views/dashboard/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% if current_user %>
<h1><%= "Welcome, #{current_user.first_name || current_user.email}." %></h1>
<%= link_to 'Profile', profile_path %>
<%= link_to 'Sign Out', logout_path, method: :delete %>

<h2>My Groups</h2>
<%= link_to "Create a Group", new_group_path %>
<br>
<% if !current_user.groups.empty? %>
<%= render partial: 'users/groups', collection: current_user.groups %>
<% else %>
You are not part of any groups.
<% end %>
<% end %>
Loading

0 comments on commit ae745f3

Please sign in to comment.