Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## routes.rb
- ActionController::Routing::Routes.draw do |map|
- map.resources :countries, :collection => {:auto_complete_for_country_name => :get}
- map.resources :users, :collection => {:auto_complete_for_user_login => :get}
- ...
- ## country.rb
- class Country < ActiveRecord::Base
- end
- ## countries_controller.rb
- class CountriesController < ApplicationController
- auto_complete_for :country, :name
- def index
- @countries = Country.find(:all)
- end
- end
- ## index.html.erb - countries
- <div class="content">
- <% for c in @countries %>
- <p><%= c.name %></p>
- <% end %>
- <p><%= text_field_with_auto_complete :country, :name, {}, {:method => :get} %></p>
- </div>
- ## user.rb
- require 'digest/sha1'
- class User < ActiveRecord::Base
- has_many :images
- # Virtual attribute for the unencrypted password
- attr_accessor :password
- validates_presence_of :login, :email, :email_confirmation
- # :gender, :birthdate, :country, :hair,
- # :eye_color, :height, :weight, :body_type,
- # :fitness, :ethnicity, :faith, :level_of_faith,
- # :education, :occupation, :smoking, :drinking,
- # :relationship_status, :family, :pets,
- # :headline, :description
- #
- validates_presence_of :password, :if => :password_required?
- validates_presence_of :password_confirmation, :if => :password_required?
- validates_confirmation_of :password, :if => :password_required?
- validates_confirmation_of :email
- validates_length_of :password, :within => 8..40, :if => :password_required?
- validates_length_of :login, :within => 5..40
- validates_length_of :email, :within => 6..100
- #validates_uniqueness_of :login, :email, :case_sensitive => false
- before_save :encrypt_password
- before_create :make_activation_code
- # prevents a user from submitting a crafted form that bypasses activation
- # anything else you want your user to change should be added here.
- attr_accessible :login, :password,
- :password_confirmation, :email,
- :email_confirmation, :gender_id,
- :attitude_id, :birthdate,
- :country_id, :hair_type_id,
- :eye_color_id, :height_id, :weight_id,
- :body_type_id, :education_id,
- :occupation_id, :smoking_id, :drinking_id,
- :relationship_status_id, :headline, :description
- # Activates the user in the database.
- def activate
- @activated = true
- self.activated_at = Time.now.utc
- self.activation_code = nil
- save(false)
- end
- def active?
- # the existence of an activation code means they have not activated yet
- activation_code.nil?
- end
- # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
- def self.authenticate(login, password)
- u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login] # need to get the salt
- u && u.authenticated?(password) ? u : nil
- end
- # Encrypts some data with the salt.
- def self.encrypt(password, salt)
- Digest::SHA1.hexdigest("--#{salt}--#{password}--")
- end
- # Encrypts the password with the user salt
- def encrypt(password)
- self.class.encrypt(password, salt)
- end
- def authenticated?(password)
- crypted_password == encrypt(password)
- end
- def remember_token?
- remember_token_expires_at && Time.now.utc < remember_token_expires_at
- end
- # These create and unset the fields required for remembering users between browser closes
- def remember_me
- remember_me_for 2.weeks
- end
- def remember_me_for(time)
- remember_me_until time.from_now.utc
- end
- def remember_me_until(time)
- self.remember_token_expires_at = time
- self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
- save(false)
- end
- def forget_me
- self.remember_token_expires_at = nil
- self.remember_token = nil
- save(false)
- end
- # Returns true if the user has just been activated.
- def recently_activated?
- @activated
- end
- protected
- # before filter
- def encrypt_password
- return if password.blank?
- self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
- self.crypted_password = encrypt(password)
- end
- def password_required?
- crypted_password.blank? || !password.blank?
- end
- def make_activation_code
- self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
- end
- end
- ##users_controller.rb
- class UsersController < ApplicationController
- auto_complete_for :user, :login
- # before_filter :login_required
- def index
- @users = User.find(:all)
- end
- def new
- @user = User.new
- end
- def create
- cookies.delete :auth_token
- # protects against session fixation attacks, wreaks havoc with
- # request forgery protection.
- # uncomment at your own risk
- # reset_session
- @user = User.new(params[:user])
- @user.save
- if @user.errors.empty?
- self.current_user = @user
- redirect_back_or_default('/')
- flash[:notice] = "Thanks for signing up!"
- else
- render :action => 'new'
- end
- end
- def activate
- self.current_user = params[:activation_code].blank? ? false : User.find_by_activation_code(params[:activation_code])
- if logged_in? && !current_user.active?
- current_user.activate
- flash[:notice] = "Signup complete!"
- end
- redirect_back_or_default('/')
- end
- end
- ## new.html.erb - users
- <div class="content">
- <h2>Regisztráció</h2>
- <%= error_messages_for :user %>
- <% form_for(@user, :html => {:multipart => true}) do |f| -%>
- <h3>Személyes adataim</h3>
- <p><%= f.label :login, 'Login név' %><br/>
- <%= f.text_field :login %></p>
- <p><%= f.label :password, 'Jelszó' %><br/>
- <%= f.password_field :password %></p>
- <p><%= f.label :password_confirmation, 'Jelszó megerősítése' %><br/>
- <%= f.password_field :password_confirmation %></p>
- <p><%= f.label :email, 'E-mail cím' %><br/>
- <%= f.text_field :email %></p>
- <p><%= f.label :email_confirmation, 'E-mail cím megerősítése' %><br/>
- <%= f.text_field :email_confirmation %></p>
- <h3>Jellemzőim</h3>
- <p><%= f.label :gender_id, 'Nem' %><br/>
- <%= collection_select(:user,:gender_id, Gender.find(:all), :id, :name) %></p>
- <p><%= f.label :attitude_id, 'Beállítottság' %><br/>
- <%= collection_select(:user, :attitude_id, Attitude.find(:all), :id, :name) %></p>
- <p><%= f.label :birthdate, 'Születési dátum' %><br/>
- <%= f.date_select :birthdate, :start_year => 1900, :end_year => Time.now.year %></p>
- #######################################################
- <p><%= text_field_with_auto_complete :user, :login, {}, {:method => :get} %></p>
- #######################################################
- <p><%= f.label :hair_type_id, 'Hajszín' %><br/>
- <%= collection_select(:user,:hair_type_id, HairType.find(:all), :id, :name) %></p>
- <p><%= f.label :eye_color_id, 'Szemszín' %><br/>
- <%= collection_select(:user,:eye_color_id, EyeColor.find(:all), :id, :name) %></p>
- <p><%= f.label :height_id, 'Magasság' %><br/>
- <%= collection_select(:user,:height_id, Height.find(:all), :id, :name) %></p>
- <p><%= f.label :weight_id, 'Súly' %><br/>
- <%= collection_select(:user,:weight_id, Weight.find(:all), :id, :name) %></p>
- <p><%= f.label :body_type_id, 'Testalkat' %><br/>
- <%= collection_select(:user,:body_type_id, BodyType.find(:all), :id, :name) %></p>
- <p><%= f.label :education_id, 'Végzettség' %><br/>
- <%= collection_select(:user,:education_id, Education.find(:all), :id, :name) %></p>
- <p><%= f.label :occupation_id, 'Foglalkozás' %><br/>
- <%= collection_select(:user,:occupation_id, Occupation.find(:all), :id, :name) %></p>
- <p><%= f.label :smoking_id, 'Dohányzás' %><br/>
- <%= collection_select(:user,:smoking_id, Smoking.find(:all), :id, :name) %></p>
- <p><%= f.label :drinking_id, 'Alkoholfogyasztás' %><br/>
- <%= collection_select(:user,:drinking_id, Drinking.find(:all), :id, :name) %></p>
- <p><%= f.label :relationship_status_id, 'Családi állapot' %><br/>
- <%= collection_select(:user,:relationship_status_id, RelationshipStatus.find(:all), :id, :name) %></p>
- <h3>Rólam</h3>
- <p><%= f.label :headline %><br/>
- <%= f.text_field :headline %></p>
- <p><%= f.label :description %><br/>
- <%= f.text_area :description %></p>
- <p><%= submit_tag 'Regisztráció' %></p>
- <% end -%>
- </div>
Add Comment
Please, Sign In to add comment