Guest User

Untitled

a guest
May 25th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. ## routes.rb
  2. ActionController::Routing::Routes.draw do |map|
  3. map.resources :countries, :collection => {:auto_complete_for_country_name => :get}
  4. map.resources :users, :collection => {:auto_complete_for_user_login => :get}
  5. ...
  6.  
  7. ## country.rb
  8. class Country < ActiveRecord::Base
  9. end
  10.  
  11.  
  12. ## countries_controller.rb
  13. class CountriesController < ApplicationController
  14. auto_complete_for :country, :name
  15. def index
  16. @countries = Country.find(:all)
  17. end
  18. end
  19.  
  20.  
  21. ## index.html.erb - countries
  22. <div class="content">
  23. <% for c in @countries %>
  24. <p><%= c.name %></p>
  25. <% end %>
  26.  
  27. <p><%= text_field_with_auto_complete :country, :name, {}, {:method => :get} %></p>
  28. </div>
  29.  
  30.  
  31. ## user.rb
  32. require 'digest/sha1'
  33. class User < ActiveRecord::Base
  34. has_many :images
  35.  
  36. # Virtual attribute for the unencrypted password
  37. attr_accessor :password
  38.  
  39. validates_presence_of :login, :email, :email_confirmation
  40. # :gender, :birthdate, :country, :hair,
  41. # :eye_color, :height, :weight, :body_type,
  42. # :fitness, :ethnicity, :faith, :level_of_faith,
  43. # :education, :occupation, :smoking, :drinking,
  44. # :relationship_status, :family, :pets,
  45. # :headline, :description
  46. #
  47.  
  48. validates_presence_of :password, :if => :password_required?
  49. validates_presence_of :password_confirmation, :if => :password_required?
  50.  
  51. validates_confirmation_of :password, :if => :password_required?
  52. validates_confirmation_of :email
  53.  
  54. validates_length_of :password, :within => 8..40, :if => :password_required?
  55. validates_length_of :login, :within => 5..40
  56. validates_length_of :email, :within => 6..100
  57.  
  58. #validates_uniqueness_of :login, :email, :case_sensitive => false
  59.  
  60. before_save :encrypt_password
  61. before_create :make_activation_code
  62.  
  63. # prevents a user from submitting a crafted form that bypasses activation
  64. # anything else you want your user to change should be added here.
  65. attr_accessible :login, :password,
  66. :password_confirmation, :email,
  67. :email_confirmation, :gender_id,
  68. :attitude_id, :birthdate,
  69.  
  70. :country_id, :hair_type_id,
  71. :eye_color_id, :height_id, :weight_id,
  72. :body_type_id, :education_id,
  73. :occupation_id, :smoking_id, :drinking_id,
  74. :relationship_status_id, :headline, :description
  75.  
  76. # Activates the user in the database.
  77. def activate
  78. @activated = true
  79. self.activated_at = Time.now.utc
  80. self.activation_code = nil
  81. save(false)
  82. end
  83.  
  84. def active?
  85. # the existence of an activation code means they have not activated yet
  86. activation_code.nil?
  87. end
  88.  
  89. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  90. def self.authenticate(login, password)
  91. u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login] # need to get the salt
  92. u && u.authenticated?(password) ? u : nil
  93. end
  94.  
  95. # Encrypts some data with the salt.
  96. def self.encrypt(password, salt)
  97. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  98. end
  99.  
  100. # Encrypts the password with the user salt
  101. def encrypt(password)
  102. self.class.encrypt(password, salt)
  103. end
  104.  
  105. def authenticated?(password)
  106. crypted_password == encrypt(password)
  107. end
  108.  
  109. def remember_token?
  110. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  111. end
  112.  
  113. # These create and unset the fields required for remembering users between browser closes
  114. def remember_me
  115. remember_me_for 2.weeks
  116. end
  117.  
  118. def remember_me_for(time)
  119. remember_me_until time.from_now.utc
  120. end
  121.  
  122. def remember_me_until(time)
  123. self.remember_token_expires_at = time
  124. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  125. save(false)
  126. end
  127.  
  128. def forget_me
  129. self.remember_token_expires_at = nil
  130. self.remember_token = nil
  131. save(false)
  132. end
  133.  
  134. # Returns true if the user has just been activated.
  135. def recently_activated?
  136. @activated
  137. end
  138.  
  139. protected
  140. # before filter
  141. def encrypt_password
  142. return if password.blank?
  143. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  144. self.crypted_password = encrypt(password)
  145. end
  146.  
  147. def password_required?
  148. crypted_password.blank? || !password.blank?
  149. end
  150.  
  151. def make_activation_code
  152.  
  153. self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  154. end
  155. end
  156.  
  157.  
  158. ##users_controller.rb
  159. class UsersController < ApplicationController
  160.  
  161. auto_complete_for :user, :login
  162.  
  163. # before_filter :login_required
  164. def index
  165. @users = User.find(:all)
  166. end
  167.  
  168. def new
  169. @user = User.new
  170. end
  171.  
  172. def create
  173. cookies.delete :auth_token
  174. # protects against session fixation attacks, wreaks havoc with
  175. # request forgery protection.
  176. # uncomment at your own risk
  177. # reset_session
  178. @user = User.new(params[:user])
  179. @user.save
  180. if @user.errors.empty?
  181. self.current_user = @user
  182. redirect_back_or_default('/')
  183. flash[:notice] = "Thanks for signing up!"
  184. else
  185. render :action => 'new'
  186. end
  187. end
  188.  
  189. def activate
  190. self.current_user = params[:activation_code].blank? ? false : User.find_by_activation_code(params[:activation_code])
  191. if logged_in? && !current_user.active?
  192. current_user.activate
  193. flash[:notice] = "Signup complete!"
  194. end
  195. redirect_back_or_default('/')
  196. end
  197. end
  198.  
  199.  
  200. ## new.html.erb - users
  201. <div class="content">
  202. <h2>Regisztráció</h2>
  203.  
  204. <%= error_messages_for :user %>
  205.  
  206. <% form_for(@user, :html => {:multipart => true}) do |f| -%>
  207. <h3>Személyes adataim</h3>
  208. <p><%= f.label :login, 'Login név' %><br/>
  209. <%= f.text_field :login %></p>
  210.  
  211. <p><%= f.label :password, 'Jelszó' %><br/>
  212. <%= f.password_field :password %></p>
  213.  
  214. <p><%= f.label :password_confirmation, 'Jelszó megerősítése' %><br/>
  215. <%= f.password_field :password_confirmation %></p>
  216.  
  217. <p><%= f.label :email, 'E-mail cím' %><br/>
  218. <%= f.text_field :email %></p>
  219.  
  220. <p><%= f.label :email_confirmation, 'E-mail cím megerősítése' %><br/>
  221. <%= f.text_field :email_confirmation %></p>
  222.  
  223. <h3>Jellemzőim</h3>
  224. <p><%= f.label :gender_id, 'Nem' %><br/>
  225. <%= collection_select(:user,:gender_id, Gender.find(:all), :id, :name) %></p>
  226.  
  227. <p><%= f.label :attitude_id, 'Beállítottság' %><br/>
  228. <%= collection_select(:user, :attitude_id, Attitude.find(:all), :id, :name) %></p>
  229.  
  230. <p><%= f.label :birthdate, 'Születési dátum' %><br/>
  231. <%= f.date_select :birthdate, :start_year => 1900, :end_year => Time.now.year %></p>
  232.  
  233.  
  234.  
  235. #######################################################
  236. <p><%= text_field_with_auto_complete :user, :login, {}, {:method => :get} %></p>
  237. #######################################################
  238.  
  239.  
  240. <p><%= f.label :hair_type_id, 'Hajszín' %><br/>
  241. <%= collection_select(:user,:hair_type_id, HairType.find(:all), :id, :name) %></p>
  242.  
  243. <p><%= f.label :eye_color_id, 'Szemszín' %><br/>
  244. <%= collection_select(:user,:eye_color_id, EyeColor.find(:all), :id, :name) %></p>
  245.  
  246. <p><%= f.label :height_id, 'Magasság' %><br/>
  247. <%= collection_select(:user,:height_id, Height.find(:all), :id, :name) %></p>
  248.  
  249. <p><%= f.label :weight_id, 'Súly' %><br/>
  250. <%= collection_select(:user,:weight_id, Weight.find(:all), :id, :name) %></p>
  251.  
  252. <p><%= f.label :body_type_id, 'Testalkat' %><br/>
  253. <%= collection_select(:user,:body_type_id, BodyType.find(:all), :id, :name) %></p>
  254.  
  255. <p><%= f.label :education_id, 'Végzettség' %><br/>
  256. <%= collection_select(:user,:education_id, Education.find(:all), :id, :name) %></p>
  257.  
  258. <p><%= f.label :occupation_id, 'Foglalkozás' %><br/>
  259. <%= collection_select(:user,:occupation_id, Occupation.find(:all), :id, :name) %></p>
  260.  
  261. <p><%= f.label :smoking_id, 'Dohányzás' %><br/>
  262. <%= collection_select(:user,:smoking_id, Smoking.find(:all), :id, :name) %></p>
  263.  
  264. <p><%= f.label :drinking_id, 'Alkoholfogyasztás' %><br/>
  265. <%= collection_select(:user,:drinking_id, Drinking.find(:all), :id, :name) %></p>
  266.  
  267. <p><%= f.label :relationship_status_id, 'Családi állapot' %><br/>
  268. <%= collection_select(:user,:relationship_status_id, RelationshipStatus.find(:all), :id, :name) %></p>
  269.  
  270. <h3>Rólam</h3>
  271. <p><%= f.label :headline %><br/>
  272. <%= f.text_field :headline %></p>
  273.  
  274. <p><%= f.label :description %><br/>
  275. <%= f.text_area :description %></p>
  276.  
  277. <p><%= submit_tag 'Regisztráció' %></p>
  278. <% end -%>
  279. </div>
Add Comment
Please, Sign In to add comment