From 16c75b2d0f555226c25ee206107e23ee4a28b951 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Thu, 10 Apr 2014 20:10:04 -0700 Subject: [PATCH] Fixing issue #19 for realsies (oriented by tracking) --- app/controllers/contracts_controller.rb | 2 +- app/controllers/users_controller.rb | 7 +++++++ app/models/contract.rb | 1 + app/models/user.rb | 1 + app/views/contracts/show.html.erb | 7 ++++++- app/views/users/show.html.erb | 3 +++ db/migrate/20140411022819_add_created_by_to_contract.rb | 2 +- db/migrate/20140411024809_add_oriented_by_to_users.rb | 5 +++++ db/schema.rb | 5 +++-- 9 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20140411024809_add_oriented_by_to_users.rb diff --git a/app/controllers/contracts_controller.rb b/app/controllers/contracts_controller.rb index 02c8104..350a48b 100644 --- a/app/controllers/contracts_controller.rb +++ b/app/controllers/contracts_controller.rb @@ -18,7 +18,7 @@ class ContractsController < ApplicationController def create Rails.logger.info "CONTRACT" Rails.logger.info @contract.inspect - @contract.created_by = current_user.id + @contract.created_by = current_user respond_to do |format| if @contract.save format.html { redirect_to @contract, :notice => 'Contract was successfully created.' } diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 278cea9..31b18f0 100755 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -124,6 +124,9 @@ class UsersController < ApplicationController # POST /users # POST /users.json def create + # update oriented_by only if orientation has been set + @user.oriented_by_id = current_user.id unless @user.orientation.blank? + respond_to do |format| if @user.save format.html { redirect_to @user, :notice => 'User was successfully created.' } @@ -138,6 +141,10 @@ class UsersController < ApplicationController # PUT /users/1 # PUT /users/1.json def update + # update oriented_by only if it's blank but the (new) orientation isn't blank + # gotta test the params because they don't get applied til below. + @user.oriented_by_id = current_user.id if @user.oriented_by.blank? && (!params[:user]["orientation(1i)"].blank?) + respond_to do |format| if @user.update_attributes(params[:user]) format.html { redirect_to @user, :notice => 'User was successfully updated.' } diff --git a/app/models/contract.rb b/app/models/contract.rb index 5df4b68..69fd92a 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -1,5 +1,6 @@ class Contract < ActiveRecord::Base belongs_to :user + belongs_to :created_by, :foreign_key => "created_by_id", :class_name => "User" attr_accessible :user_id, :first_name, :last_name, :cosigner, :signed_at, :document, :document_file_name, :document_content_type, :document_file_size, :document_updated_at diff --git a/app/models/user.rb b/app/models/user.rb index 1829249..d1f2131 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,6 +11,7 @@ class User < ActiveRecord::Base # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :admin, :instructor, :member, :emergency_name, :emergency_phone, :current_skills, :desired_skills, :waiver, :emergency_email, :phone, :payment_method, :orientation, :member_level, :certifications, :hidden, :marketing_source, :payee, :accountant, :exit_reason, :twitter_url, :facebook_url, :github_url, :website_url, :email_visible, :phone_visible, :postal_code #TODO: make admin/instructor/member/etc not accessible + belongs_to :oriented_by, :foreign_key => "oriented_by_id", :class_name => "User" has_many :cards has_many :user_certifications has_many :certifications, :through => :user_certifications diff --git a/app/views/contracts/show.html.erb b/app/views/contracts/show.html.erb index be4755d..bfd6183 100644 --- a/app/views/contracts/show.html.erb +++ b/app/views/contracts/show.html.erb @@ -18,9 +18,14 @@ <%= @contract.signed_at.to_date.to_s(:long) %> +<% unless @contract.created_by.blank? %> +

+ Created by <%= @contract.created_by.name %> +

+<% end %> <% if @contract.document.blank? %> - No document uploaded +

No document uploaded

<% else %>

<%= link_to "Download Contract", @contract.document.url %>

diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index b969896..186c29b 100755 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -36,6 +36,9 @@

Orientation? <%= @user.orientation.strftime("%B %d %Y") unless @user.orientation.blank? %> + <% unless @user.oriented_by.blank? %> + (recorded by <%= link_to @user.oriented_by.name, @user.oriented_by %>) + <% end %>

Emergency Name: diff --git a/db/migrate/20140411022819_add_created_by_to_contract.rb b/db/migrate/20140411022819_add_created_by_to_contract.rb index ee0dd62..21918bf 100644 --- a/db/migrate/20140411022819_add_created_by_to_contract.rb +++ b/db/migrate/20140411022819_add_created_by_to_contract.rb @@ -1,5 +1,5 @@ class AddCreatedByToContract < ActiveRecord::Migration def change - add_column :contracts, :created_by, :integer + add_column :contracts, :created_by_id, :integer end end diff --git a/db/migrate/20140411024809_add_oriented_by_to_users.rb b/db/migrate/20140411024809_add_oriented_by_to_users.rb new file mode 100644 index 0000000..71216a4 --- /dev/null +++ b/db/migrate/20140411024809_add_oriented_by_to_users.rb @@ -0,0 +1,5 @@ +class AddOrientedByToUsers < ActiveRecord::Migration + def change + add_column :users, :oriented_by_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 98cde8d..1f13e16 100755 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140411022819) do +ActiveRecord::Schema.define(:version => 20140411024809) do create_table "cards", :force => true do |t| t.string "card_number" @@ -42,7 +42,7 @@ ActiveRecord::Schema.define(:version => 20140411022819) do t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.string "cosigner" - t.integer "created_by" + t.integer "created_by_id" end create_table "door_logs", :force => true do |t| @@ -252,6 +252,7 @@ ActiveRecord::Schema.define(:version => 20140411022819) do t.boolean "email_visible" t.boolean "phone_visible" t.string "postal_code" + t.integer "oriented_by_id" end add_index "users", ["email"], :name => "index_users_on_email", :unique => true