Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. $ = jQuery
  2.  
  3. $(document).on "ready page:load", ->
  4. $('#follow_btn').on "click", ->
  5. friend = $(this).data("friend")
  6. boton = $(this)
  7. $.ajax "/usuario/follow",
  8. type: "POST"
  9. dataType: "JSON"
  10. data: {usuario: { friend_id: friend }}
  11. success: (data)->
  12. console.log data
  13. boton.slideUp()
  14. alert friend
  15. error: (err)->
  16. console.log err
  17. alert "No hemos podido crear la amistad"
  18.  
  19. class UsuarioController < ApplicationController
  20. skip_before_action :verify_authenticity_token
  21.  
  22. def show
  23. @usuario = Usuario.find(params[:id])
  24. end
  25.  
  26. def follow
  27. respond_to do |format|
  28. if current_usuario.follow!(post_params)
  29. format.json {head :no_content}
  30. else
  31. format.json {render json: "Se encontraron errores"}
  32. end
  33. end
  34. end
  35.  
  36. private
  37. def post_params
  38. params.require(:usuario).permit(:friend_id)
  39. end
  40. end
  41.  
  42. def follow!(amigo_id)
  43. friendships.create(friend_id = amigo_id)
  44. end
  45.  
  46. current_usuario.follow!(3)
  47.  
  48. class Usuario < ApplicationRecord
  49. # Include default devise modules. Others available are:
  50. # :confirmable, :lockable, :timeoutable and :omniauthable
  51. devise :database_authenticatable, :registerable,
  52. :recoverable, :rememberable, :trackable, :validatable
  53. devise :omniauthable, omniauth_providers: [:facebook, :twitter]
  54.  
  55. has_many :posts
  56. has_many :friendships
  57.  
  58. has_many :follows, through: :friendships, source: :friend
  59.  
  60. has_many :followers_friendships, class_name: "Friendship",
  61. foreign_key: "friend_id"
  62.  
  63. has_many :followers, through: :followers_friendships, source:
  64. :usuario
  65.  
  66. def follow!(amigo_id)
  67. friendships.create!(friend_id: amigo_id)
  68. end
  69.  
  70. def can_follow?(amigo_id)
  71. not amigo_id == self.id or friendships.where(friend_id:
  72. amigo_id).size > 0
  73. end
  74.  
  75. def email_required?
  76. false
  77. end
  78.  
  79. validates :username, presence: true, uniqueness: true,
  80. length: {in:5..20, too_short: "Al menos 5 caracteres", too_long:
  81. "Maximo 20 caracteres"}
  82.  
  83. def self.find_or_create_by_omniauth(auth)
  84. usuario = self.find_or_create_by(provider: auth[:provider], uid:
  85. auth[:uid]) do |user|
  86. user.nombre = auth[:name]
  87. user.apellido = auth[:last_name]
  88. user.username = auth[:username]
  89. user.email = auth[:email]
  90. user.uid = auth[:uid]
  91. user.provider = auth[:provider]
  92. user.password = Devise.friendly_token[0,20]
  93. end
  94. end
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement