Guest User

Untitled

a guest
Jan 11th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. <%= form_with(model: current_user, local: true, html: {multipart: true}) do |form| %>
  2. <div class="field">
  3. <%= form.label :profile_pic %>
  4. <%= form.file_field :profile_pic, id: :profile_pic %>
  5. </div>
  6. <div class="actions">
  7. <%= form.submit %>
  8. </div>
  9. <% end %>
  10.  
  11. def update
  12. @user = current_user
  13. respond_to do |format|
  14. if @user.update(user_params)
  15. format.html { redirect_to @user, notice: 'User was successfully updated.' }
  16. format.json { render :show, status: :ok, location: @user }
  17. else
  18. render action: :edit
  19. end
  20. end
  21. end
  22.  
  23. respond_to do |format|
  24.  
  25. require 'digest'
  26. class User < ApplicationRecord
  27. mount_uploader :profile_pic, ProfilePicUploader
  28. attr_accessor :password
  29. before_save :encrypt_new_password
  30. after_create :build_profile
  31. has_one :profile
  32. has_many :trips
  33. has_many :comments, through: :trips, source: :comments
  34. has_many :posts, through: :trips, source: :posts
  35.  
  36. scope :recent_comments, ->{where("comments.created_at > ? AND user_id = ?", [6.months.ago, self.id]).limit(3)}
  37. #friends
  38. has_many :users
  39.  
  40. validates :email, uniqueness: {case_sensitive: false, message: 'El correo debe ser Ășnico'}, length: {in: 6..50, too_short: "debe tener al menos %{count} caracteres"}, format: {multiline: true,with: /^.+@.+$/, message: "formato de correo no valido"}
  41.  
  42. validates :password, confirmation: true, length: {within: 4..20}, presence: {if: :password_required?}
  43. validates :password_confirmation, presence: true
  44.  
  45. def self.authenticate(email,password)
  46. user = find_by_email(email)
  47. return user if user && user.authenticated?(password)
  48. end
  49.  
  50. def authenticated?(password)
  51. self.hashed_password == encrypt(password)
  52. end
  53.  
  54. protected
  55. def encrypt_new_password
  56. return if password.blank?
  57. self.hashed_password = encrypt(password)
  58. end
  59. def password_required?
  60. hashed_password.blank? || password.present?
  61. end
  62. def encrypt(string)
  63. Digest::SHA1.hexdigest(string)
  64. end
  65. def build_profile
  66. Profile.create(user: self, name: self.name, bio:"Im using Tripper!")
  67. end
  68. end
Add Comment
Please, Sign In to add comment