Advertisement
Guest User

Untitled

a guest
Feb 19th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. class Users::RegistrationsController < Devise::RegistrationsController
  2. include ApplicationHelper
  3.  
  4. def create
  5. super
  6. end
  7.  
  8. def new
  9. super
  10. end
  11.  
  12. def edit
  13. super
  14. end
  15.  
  16. protected
  17.  
  18. def update_resource(resource, params)
  19. resource.update_without_password(params)
  20. end
  21.  
  22. class User < ActiveRecord::Base
  23. devise :database_authenticatable, :registerable,
  24. :recoverable, :rememberable, :trackable, :validatable
  25. devise :omniauthable, omniauth_providers: [:facebook]
  26.  
  27. acts_as_voter
  28. mount_uploader :avatar, AvatarUploader
  29.  
  30. def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
  31. user = User.where(provider: auth.provider, uid: auth.uid).first
  32.  
  33. # The User was found in our database
  34. return user if user
  35.  
  36. # Check if the User is already registered without Facebook
  37. user = User.where(email: auth.info.email).first
  38.  
  39. return user if user
  40.  
  41. # The User was not found and we need to create them
  42. User.create(real_name: auth.extra.raw_info.name,
  43. provider: auth.provider,
  44. uid: auth.uid,
  45. location: auth.info.location,
  46. language: auth.info.language,
  47. gender: auth.extra.raw_info.gender,
  48. email: auth.info.email,
  49. password: Devise.friendly_token[0,20],
  50. remote_avatar_url: auth.info.image)
  51. end
  52.  
  53.  
  54. attr_accessor :login
  55.  
  56. def self.find_first_by_auth_conditions(warden_conditions)
  57. conditions = warden_conditions.dup
  58. if login = conditions.delete(:login)
  59. where(conditions).where(["lower(username) = :value OR lower(email) = :value", {value: login.downcase}]).first
  60. else
  61. where(conditions).first
  62. end
  63. end
  64.  
  65.  
  66. devise authentication_keys: [:login]
  67. end
  68.  
  69. class ApplicationController < ActionController::Base
  70. protect_from_forgery with: :exception
  71.  
  72. before_filter :configure_permitted_parameters, if: :devise_controller?
  73.  
  74. protected
  75.  
  76. def configure_permitted_parameters
  77. devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me, :real_name, :gender, :language, :location, :age, :avatar, :avatar_cache) }
  78. devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
  79. devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password, :real_name, :gender, :language, :location, :age, :avatar, :avatar_cache, :remove_avatar) }
  80. end
  81.  
  82.  
  83. private
  84.  
  85. def require_user_signed_in
  86. unless user_signed_in?
  87.  
  88. # If the user came from a page, we can send them back. Otherwise, send
  89. # them to the root path.
  90. if request.env['HTTP_REFERER']
  91. fallback_redirect = :back
  92. elsif defined?(root_path)
  93. fallback_redirect = root_path
  94. else
  95. fallback_redirect = "/"
  96. end
  97.  
  98. redirect_to fallback_redirect, flash: {error: "You must be signed in to view this page."}
  99. end
  100. end
  101.  
  102. end
  103.  
  104. root 'posts#index'
  105.  
  106. devise_for :users, controllers: {registrations: "users/registrations", sessions: "users/sessions", passwords: "users/passwords", omniauth_callbacks: "users/omniauth_callbacks"}, skip: [:sessions, :registrations]
  107.  
  108. devise_scope :user do
  109. get "login" => "users/sessions#new", as: :new_user_session
  110. post "login" => "users/sessions#create", as: :user_session
  111. delete "signout" => "users/sessions#destroy", as: :destroy_user_session
  112.  
  113. get "signup" => "users/registrations#new", as: :new_user_registration
  114. post "signup" => "users/registrations#create", as: :user_registration
  115. put "signup" => "users/registrations#update", as: :update_user_registration
  116. get "account" => "users/registrations#edit", as: :edit_user_registration
  117. end
  118.  
  119. <%= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => {:multipart => true}) do |f| %>
  120. <%= devise_error_messages! %>
  121.  
  122.  
  123. <div class="form-group">
  124. <%= f.label :username, class: "control-label col-md-4" %>
  125. <div class="col-md-8">
  126. <%= f.text_field :username, class: "text-field form-control", html: {spellcheck: "false"} %>
  127. </div>
  128. </div>
  129.  
  130. <div class="form-group">
  131. <%= f.label :avatar, class: "control-label col-md-4" %>
  132. <div class="col-md-8">
  133. <%= f.file_field :avatar %>
  134. </div>
  135. </div>
  136.  
  137.  
  138. <div class="form-group">
  139. <%= f.label :email, class: "control-label col-md-4" %>
  140. <div class="col-md-8">
  141. <%= f.email_field :email, class: "text-field form-control", html: {spellcheck: "false"} %>
  142. </div>
  143. </div>
  144.  
  145. <div class="form-group">
  146. <%= f.label :password, class: "control-label col-md-4" %>
  147. <div class="col-md-8">
  148. <%= f.password_field :password, class: "text-field form-control", html: {autocomplete: "off"} %>
  149. </div>
  150. </div>
  151.  
  152. <div class="form-group">
  153. <%= f.label :password_confirmation, "Password Confirmation", class: "control-label col-md-4" %>
  154. <div class="col-md-8">
  155. <%= f.password_field :password_confirmation, class: "text-field form-control", html: {autocomplete: "off"} %>
  156. </div>
  157. </div>
  158.  
  159. <div class="form-group">
  160. <div class="col-md-offset-4 col-md-8">
  161. <%= f.submit "Sign Up", class: "btn btn-primary" %>
  162. </div>
  163. </div>
  164.  
  165. <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), :html => {:multipart => true}) do |f| %>
  166.  
  167. <div class="form-group">
  168. <strong>Existing Avatar</strong><BR>
  169. <% if current_user.avatar.url.present? %>
  170. <%= image_tag @current_user.avatar_url(:large).to_s %>
  171. <%= f.label :remove_avatar do %>
  172. <%= f.check_box :remove_avatar, class: 'form-control' %>
  173. <% end %>
  174. <% end %>
  175. </div>
  176. <div class="form-group">
  177. <%= f.label :avatar, class: "control-label col-md-4" %>
  178. <%= f.file_field :avatar %>
  179. <%= f.hidden_field :avatar_cache %>
  180. </div>
  181. <div class="form-actions">
  182. <%= f.submit 'Update', :class => 'button right' %>
  183.  
  184. </div>
  185.  
  186. class AvatarUploader < CarrierWave::Uploader::Base
  187. include CarrierWave::RMagick
  188. storage :file
  189.  
  190. def store_dir
  191. "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  192. end
  193.  
  194. def cache_dir
  195. "/tmp/avatarTmp"
  196. end
  197.  
  198.  
  199. process :scale => [200, 300]
  200.  
  201.  
  202. version :tiny do
  203. process :resize_to_fit => [20, 20]
  204. end
  205.  
  206. version :thumb do
  207. process :resize_to_fit => [50, 50]
  208. end
  209.  
  210. version :large do
  211. process :resize_to_fit => [200, 200]
  212. end
  213.  
  214. def extension_white_list
  215. %w(jpg jpeg gif png)
  216. end
  217.  
  218. def filename
  219. "avatar.jpg" if original_filename
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement