Skip to content

Instantly share code, notes, and snippets.

@jessenaiman
Last active December 28, 2015 08:18
Show Gist options
  • Save jessenaiman/7470145 to your computer and use it in GitHub Desktop.
Save jessenaiman/7470145 to your computer and use it in GitHub Desktop.
class User
include Mongoid::Document
include Mongoid::Timestamps
include User::AuthDefinitions
include User::Roles
include Geocoder::Model::Mongoid
mount_uploader :image, ImageUploader
field :name, type: String
field :image, type: String
field :username, type: String
field :first_name, type: String
field :last_name, type: String
field :gender, type: String
field :hometown, type: String
field :address, type: String
field :postal_code, type: String
field :city, type: String
field :phone_number, type: String
field :link, type: String
field :location, type: String
field :locale, type: String
field :relationship_status, type: String
field :significant_other, type: String
field :timezone, type: String
field :updated_time, type: DateTime
field :work, type: String
field :verified, type: Boolean
field :roles_mask, type: Integer
field :security_key, type: String
field :title, type: String
#field :riding_id, type: Integer
field :web_site, type: String
field :updated_time, type: DateTime
field :birthday, type: DateTime
validates_presence_of :email, :first_name, :postal_code, :address, :phone_number, :birthday
#address_riding
belongs_to :riding
#membership riding
has_many :identities
has_many :invitees, :class_name => self.name, :as => :invited_by
has_one :volunteer
has_one :profile
def full_name
"#{first_name} #{last_name}"
end
geocoded_by :postal_code
after_validation :geocode, :add_to_riding
field :coordinates, :type => Array
def add_to_riding
#check for postal code and find the nearest riding or add them to a central riding
if self.postal_code.nil?
self.riding = Riding.where(riding_id: 0).first
#this condition should only exist with new data. Consider removing it if the system is stable
if self.riding.web_site_manager.nil?
web_site_manager = WebSiteManager.new
web_site_manager.r_id = 9000
web_site_manager.r_str = 'olp'
web_site_manager.r_name_en = 'ontario'
self.riding.web_site_manager = web_site_manager
end
else
#check if the postal code is valid
unless Geocoder.search(self.postal_code).empty? do
self.riding = RidingAddress.near(self.postal_code, 10, :order => :distance).first.riding
if self.riding.web_site_manager.nil? || self.riding.web_site_manager.r_name_en == 'ontario'
self.riding.web_site_manager = WebSiteManager.where(r_id: self.riding.riding_id + 9000).first
end
end
end
end
end
# validates_numericality_of :age, :greater_than => 13, :message => "must be 13 or older"
# def age
# now = Time.now.utc.to_date
# now.year - birthday.year - (birthday.to_date.change(:year => now.year) > now ? 1 : 0)
# end
end
class Users::RegistrationsController < Devise::RegistrationsController
before_filter:configure_permitted_parameters
# def resource_params
# params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
# end
# private :resource_params
def create
super
end
def edit
super
end
def update
# For Rails 4
account_update_params = devise_parameter_sanitizer.for(:account_update)
# For Rails 3
# account_update_params = params[:user]
# required for settings form to submit when password is left blank
if account_update_params[:password].blank?
account_update_params.delete("password")
account_update_params.delete("password_confirmation")
end
@user = User.find(current_user.id)
if @user.update_attributes(account_update_params)
set_flash_message :notice, :updated
# Sign in the user bypassing validation in case his password changed
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:first_name, :last_name,
:email, :password, :password_confirmation,
:address, :postal_code, :city, :phone_number,
:birthday, :image)
end
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:first_name, :last_name,
:email, :password, :password_confirmation, :current_password,
:address, :postal_code, :city, :phone_number,
:birthday, :image)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment