Skip to content

Commit

Permalink
Merge pull request #4 from christinalcole/feature-position-mgmt
Browse files Browse the repository at this point in the history
Feature position mgmt
  • Loading branch information
christinalcole authored Mar 24, 2018
2 parents d33b423 + c4a1168 commit 850686e
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 27 deletions.
30 changes: 30 additions & 0 deletions app/controllers/users/position_management_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Users::PositionManagementController < ApplicationController
# def edit
# positions_users = current_user.positions_users
# Position.find_each do |position|
# unless positions_users.detect {|pu| pu.position_id == position.id}
# current_user.positions_users.build(position_id: position.id)#, skill_level: 0)
# end
# end
# end

def edit
@positions = Position.all
@positions_users = current_user.positions_users
end

def update
if current_user.update(user_position_params)
redirect_to user_positions_path(current_user)
else
render 'edit'
end
end

private
def user_position_params
params.require(:user).permit(:user, positions_users_attributes: [:position_id, :skill_level])
end
end

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"7ygP/H3za91bM2UrP5TbeyN1EAhbPYCCCnvml14Yur5MuWqMXhxLpdh0Np+SYHDAjV7IdeFJ61+tVZVjJ3QAqA==", "user"=>{"positions_users_attributes"=>{"0"=>{"position_id"=>"1", "skill_level"=>"4"}, "1"=>{"skill_level"=>""}}}, "commit"=>"Update User", "controller"=>"users", "action"=>"update", "id"=>"1"}
21 changes: 21 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class User < ApplicationRecord

validates :first_name, :last_name, :email, :phone_number, :weight, presence: true

accepts_nested_attributes_for :positions_users

def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.id).first_or_create do |user|
user.email = auth.info.email
Expand Down Expand Up @@ -53,4 +55,23 @@ def name
# def positions=(position)
# self.positions << Position.find_by(name: position[:name])
# end

def positions_users_attributes=(pu_attributes)
pu_attributes.keep_if {|i, j| j[:position_id].to_i > 0}
self.positions_users.destroy_all
pu_attributes.each do |i, attributes|
# binding.pry
self.positions_users.build(attributes)
# self.positions_users.find_or_initialize_by(attributes).update(attributes)
# self.positions_users.find_or_initialize_by(attributes).assign_attributes(attributes)
end
end

def has_positions?(position)
self.position_ids.include?(position.id)
end

def skill_level(position)
self.positions_users.where("position_id = ?", position.id).first.skill_level
end
end
19 changes: 19 additions & 0 deletions app/views/users/position_management/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<hr>
user::position_management#edit


<h2>Manage the positions on your profile:</h2>



<hr></br>
<%= form_for current_user, url: user_position_management_path do |f| %>
<% @positions.each do |position| %>
<%= f.fields_for :positions_users, PositionsUser.new do |pu_fields| %>
<%= pu_fields.check_box :position_id, {checked: current_user.has_positions?(position), id: "position_#{position.id}"}, position.id, nil %>
<%= pu_fields.label :name, position.name, for: "position_#{position.id}" %>
<%= pu_fields.text_field :skill_level, value: current_user.has_positions?(position) ? current_user.skill_level(position) : '' %><br>
<% end %>
<% end %>
<%= f.submit "Manage Position(s)" %>
<% end %>
8 changes: 4 additions & 4 deletions app/views/users/positions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ user::positions#index

<h2><%= current_user.name %>'s positions</h2>
<ul>
<% current_user.positions.each do |position| %>
<li><%= position.name %> </li>
<% current_user.positions_users.each do |pu| %>
<li><%= pu.position.name %> with a skill level of <%= pu.skill_level %></li>
<% end %>
</ul>


<%= link_to "Add Positions", new_user_position_path(current_user) %>
<%= link_to "Manage Positions", edit_user_path(current_user) %>
<%= link_to "Manage Positions", user_position_management_path(current_user) %>
<!-- <%= link_to "Manage Positions", edit_user_path(current_user) %> -->
7 changes: 2 additions & 5 deletions app/views/users/positions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ user::positions#new

<%= form_for current_user do |f| %>
<%= f.fields_for :positions_users do |pu| %>
<%= pu.check_box :position_id, {id: "position_id#{pu.object.position.id}"}, pu.object.position.id, nil %>
<%= pu.label :name, pu.object.position.name %>
<%= check_box_tag "user[position_ids][]", :position_id, current_user.positions.include?(pu.object.position) %>
<%= pu.object.position.name %>
<%= pu.label :skill_level %>
<%= pu.number_field :skill_level %><br>
<% end %>
<%= f.submit "Add Position(s)" %>
<% end %>


<!-- need to remove checkboxes? form class shows as edit, not new -->
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

resources :users do
scope module: :users do
resources :positions, only: [:index, :create, :new]
get '/position-management', to: 'position_management#edit' # Gives /users/:user_id/position-management(.:format) users/position_management#edit
patch '/position-management', to: 'position_management#update'
put '/position-management', to: 'position_management#update'
resources :positions, only: [:index]
resources :boats
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveNotNullConstraintOnPositionsUsers < ActiveRecord::Migration[5.0]
def change
change_column_null :positions_users, :skill_level, true
end
end
11 changes: 11 additions & 0 deletions db/migrate/20180320230855_add_timestamps_to_positions_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddTimestampsToPositionsUsers < ActiveRecord::Migration[5.0]
def change
add_timestamps :positions_users, null: true

long_ago = DateTime.new(2003, 8, 13)
PositionsUser.update_all(created_at: long_ago, updated_at: long_ago)

change_column_null :positions_users, :created_at, false
change_column_null :positions_users, :updated_at, false
end
end
10 changes: 6 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180217214429) do
ActiveRecord::Schema.define(version: 20180320230855) do

create_table "boats", force: :cascade do |t|
t.string "name"
Expand All @@ -32,9 +32,11 @@
end

create_table "positions_users", force: :cascade do |t|
t.integer "position_id"
t.integer "user_id"
t.integer "skill_level", null: false
t.integer "position_id"
t.integer "user_id"
t.integer "skill_level"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["position_id"], name: "index_positions_users_on_position_id"
t.index ["user_id"], name: "index_positions_users_on_user_id"
end
Expand Down
56 changes: 43 additions & 13 deletions spec/features/position_features_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
user = create(:user)

visit user_positions_path(user.id)

expect(page.status_code).to eq(200)
end
end

context 'user wants to add positions to their profile' do
xcontext 'user wants to add positions to their profile' do
scenario 'there is a link to add new positions' do
user = create(:user)
signin(user.email, user.password)

visit user_positions_path(user.id)

expect(page).to have_link("Add Positions")
end

Expand All @@ -25,6 +27,7 @@

visit user_positions_path(user.id)
click_link("Add Positions")

expect(page).to have_css("form#new_user_#{user.id}")
end

Expand All @@ -50,57 +53,84 @@
signin(user.email, user.password)

visit user_positions_path(user.id)

expect(page).to have_link("Manage Positions")
end

xscenario 'there is a form to update positions associated with user profile' do
scenario 'there is a form to update positions associated with user profile' do
user = create(:user)
signin(user.email, user.password)

visit user_positions_path(user.id)
click_link("Manage Positions")

expect(page).to have_css("form#edit_user_#{user.id}")
end

xscenario 'the form should display existing positions in the db' do
scenario 'the form should display existing positions in the db' do
position1 = create(:position)
position2 = create(:position)
user = create(:user)

PositionsUser.create(user_id: user.id, position_id: position1.id, skill_level: 3)
binding.pry
signin(user.email, user.password)
visit edit_user_path(user.id)
visit user_position_management_path(user.id)

expect(page).to have_content(position1.name)
expect(page).to have_content(position2.name)
end

xscenario 'the form should indicate which position(s) are already listed in the user profile' do
scenario 'the form should display checkboxes for the existing positions in the db' do
position1 = create(:position)
position2 = create(:position)
user = create(:user)

signin(user.email, user.password)
visit user_position_management_path(user.id)

expect(page).to have_css("input[type=\"checkbox\"]", count: 2)
end

scenario 'the form should indicate which position(s) are already listed in the user profile' do
position1 = create(:position)
position2 = create(:position)
user = create(:user)
user.positions << position1

signin(user.email, user.password)
visit edit_user_path(user.id)
visit user_position_management_path(user.id)

expect(page).to have_field("user_position_ids_#{position1.id}", checked: true)
expect(page).to have_field("user_position_ids_#{position2.id}", checked: false)
expect(page).to have_field("position_#{position1.id}", checked: true)
expect(page).to have_field("position_#{position2.id}", checked: false)
end

xscenario 'the list of positions for a user should update after the form is submitted' do
scenario 'the list of positions for a user should update after the form is submitted' do
position1 = create(:position)
position2 = create(:position)
user = create(:user)
user.positions << position1

signin(user.email, user.password)
visit edit_user_path(user.id)
visit user_position_management_path(user.id)

page.check(position2.name)
click_button("Update Position(s)")
click_button("Manage Position(s)")

expect(user.positions).to include(position1, position2)
end

scenario 'form submission updates an existing profile position and does not create a new one' do
position1 = create(:position)
user = create(:user)
user.positions_users << PositionsUser.create(user_id: user.id, position_id: position1.id, skill_level: 2)

signin(user.email, user.password)
visit user_position_management_path(user.id)
fill_in 'user[positions_users_attributes][0][skill_level]', with: 5
click_button("Manage Position(s)")

expect(user.positions.count).to eq(1)
expect(user.positions_users.count).to eq(1)
expect(user.positions_users.first.skill_level).to eq(5)
end
end
end

0 comments on commit 850686e

Please sign in to comment.