Guest User

Untitled

a guest
Jun 22nd, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. ### params being posted
  2.  
  3.  
  4. {"county"=>"county down", "firstname"=>"chris", "existing_qualification_attributes"=>{}, "secondname"=>"mccabe"}
  5.  
  6.  
  7. ###
  8.  
  9. require 'digest/sha1'
  10.  
  11. class User < ActiveRecord::Base
  12. has_many :history
  13. has_many :education
  14. has_many :qualifications, :dependent => :destroy
  15.  
  16.  
  17. validates_presence_of :firstname, :secondname, :email
  18. validates_uniqueness_of :email
  19.  
  20. attr_accessor :password_confirmation
  21. validates_confirmation_of :password
  22.  
  23. validate :password_non_blank
  24.  
  25. def password
  26. @password
  27. end
  28.  
  29. def password=(pwd)
  30. @password = pwd
  31. return if pwd.blank?
  32. create_new_salt
  33. self.hashed_password = User.encrypted_password(self.password, self.salt)
  34. end
  35.  
  36. def self.autheticate(email, password)
  37. user = self.find_by_email(email)
  38. if user
  39. expected_password = encrypted_password(password, user.salt)
  40. if user.hashed_password != expected_password
  41. user = nil
  42. end
  43. end
  44. user
  45. end
  46.  
  47. def new_qualification_attributes=(qualification_attributes)
  48. qualification_attributes.each do |attributes|
  49. qualifications.build(attributes)
  50. end
  51. end
  52.  
  53. after_update :save_qualifications
  54.  
  55. def existing_qualification_attributes=(qualification_attributes)
  56. qualifications.reject(&:new_record?).each do |qualification|
  57. attributes = qualification_attributes[qualification.id.to_s]
  58. if attributes
  59. qualification.attributes = attributes
  60. else
  61. qualifications.delete(qualification)
  62. end
  63. end
  64. end
  65.  
  66. def save_qualifications
  67. qualifications.each do |qualification|
  68. qualification.save(false)
  69. end
  70. end
  71.  
  72.  
  73.  
  74. private
  75.  
  76. def password_non_blank
  77. errors.add(:password, "Missing password") if hashed_password.blank?
  78. end
  79.  
  80. def self.encrypted_password(password, salt)
  81. string_to_hash = password + "wibble" + salt
  82. Digest::SHA1.hexdigest(string_to_hash)
  83. end
  84.  
  85. def create_new_salt
  86. self.salt = self.object_id.to_s + rand.to_s
  87. end
  88.  
  89.  
  90. end
  91.  
  92.  
  93.  
  94. ####controller
  95. ####
  96.  
  97.  
  98. class UsersController < ApplicationController
  99. # GET /users
  100. # GET /users.xml
  101. def index
  102. @users = User.find(:all, :order => :secondname)
  103.  
  104. respond_to do |format|
  105. format.html # index.html.erb
  106. format.xml { render :xml => @users }
  107. end
  108. end
  109.  
  110. # GET /users/1
  111. # GET /users/1.xml
  112. def show
  113. @user = User.find(params[:id])
  114.  
  115. respond_to do |format|
  116. format.html # show.html.erb
  117. format.xml { render :xml => @user }
  118. end
  119. end
  120.  
  121. # GET /users/new
  122. # GET /users/new.xml
  123. def new
  124. @user = User.new
  125. @user.qualification.build
  126.  
  127. respond_to do |format|
  128. format.html # new.html.erb
  129. format.xml { render :xml => @user }
  130. end
  131. end
  132.  
  133. # GET /users/1/edit
  134. def edit
  135. @user = User.find(params[:id])
  136.  
  137. end
  138.  
  139. # POST /users
  140. # POST /users.xml
  141. def create
  142. @user = User.new(params[:user])
  143.  
  144. respond_to do |format|
  145. if @user.save
  146. flash[:notice] = 'User #{@user.email} was successfully created.'
  147. format.html { redirect_to(@user) }
  148. format.xml { render :xml => @user, :status => :created, :location => @user }
  149. else
  150. format.html { render :action => "new" }
  151. format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
  152. end
  153. end
  154. end
  155.  
  156. # PUT /users/1
  157. # PUT /users/1.xml
  158. def update
  159. params[:user][:existing_qualification_attributes] || = {}
  160. @user = User.find(params[:id])
  161.  
  162. respond_to do |format|
  163. if @user.update_attributes(params[:user])
  164. flash[:notice] = params[:user].inspect
  165. format.html { redirect_to(@user) }
  166. format.xml { head :ok }
  167. else
  168. format.html { render :action => "edit" }
  169. format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
  170. end
  171. end
  172. end
  173.  
  174. # DELETE /users/1
  175. # DELETE /users/1.xml
  176. def destroy
  177. @user = User.find(params[:id])
  178. @user.destroy
  179.  
  180. respond_to do |format|
  181. format.html { redirect_to(users_url) }
  182. format.xml { head :ok }
  183. end
  184. end
  185. end
Add Comment
Please, Sign In to add comment