Guest User

Untitled

a guest
Jun 6th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. #user.rb
  2. require 'digest/sha1'
  3.  
  4. class User < ActiveRecord::Base
  5. include Authentication
  6. include Authentication::ByPassword
  7. include Authentication::ByCookieToken
  8.  
  9. has_one :model
  10. validates_associated :model
  11.  
  12. belongs_to :country
  13. validates_associated :country
  14.  
  15. belongs_to :region
  16. validates_associated :region
  17.  
  18. belongs_to :city
  19. validates_associated :city
  20.  
  21. validates_format_of :first_name, :with => Authentication.name_regex, :message => Authentication.bad_name_message, :allow_nil => true
  22. validates_length_of :first_name, :maximum => 100
  23.  
  24. validates_format_of :last_name, :with => Authentication.name_regex, :message => Authentication.bad_name_message, :allow_nil => true
  25. validates_length_of :last_name, :maximum => 100
  26.  
  27. validates_presence_of :email
  28. validates_length_of :email, :within => 6..100 #r@a.wk
  29. validates_uniqueness_of :email
  30. validates_format_of :email, :with => Authentication.email_regex, :message => Authentication.bad_email_message
  31.  
  32.  
  33.  
  34. # HACK HACK HACK -- how to do attr_accessible from here?
  35. # prevents a user from submitting a crafted form that bypasses activation
  36. # anything else you want your user to change should be added here.
  37. attr_accessible :email, :first_name, :last_name, :birth_day, :address, :country_id, :region_id, :input_mobile_phone, :password, :password_confirmation
  38.  
  39. def self.authenticate(email, password)
  40. return nil if email.blank? || password.blank?
  41. u = find_by_email(email.downcase) # need to get the salt
  42. u && u.authenticated?(password) ? u : nil
  43. end
  44.  
  45.  
  46. def email=(value)
  47. write_attribute :email, (value ? value.downcase : nil)
  48. end
  49.  
  50. protected
  51.  
  52.  
  53.  
  54. end
  55.  
  56.  
  57. # model.rb (as in "runway model")
  58.  
  59. class Model < ActiveRecord::Base
  60. belongs_to :user
  61.  
  62. belongs_to :city
  63. validates_associated :city
  64.  
  65. belongs_to :region
  66. belongs_to :country
  67. belongs_to :body_type
  68. belongs_to :skin_color
  69.  
  70. has_many :pics
  71.  
  72. def sorted_pics
  73. self.pics.sort{ |p1, p2| p1.order <=> p2.order }
  74. end
  75.  
  76. has_many :job_type_preferences, :dependent => :destroy
  77. has_many :job_types, :through => :job_type_preferences
  78.  
  79. has_many :region_preferences, :dependent => :destroy
  80. has_many :regions, :through => :region_preferences
  81.  
  82. has_many :language_preferences, :dependent => :destroy
  83. has_many :languages, :through => :language_preferences
  84.  
  85. attr_accessor :job_type_preference_ids # This is an array of job type ids as strings coming from the form of checkboxes.
  86. after_save :update_job_type_preferences
  87.  
  88. def update_job_type_preferences
  89. unless job_type_preference_ids.nil?
  90. self.job_types.each do |m|
  91. # Take out the job types we're not interested in anymore.
  92. m.destroy unless job_type_preference_ids.include?(m.id.to_s)
  93. # We don't have to add it if it's already there in the database.
  94. job_type_preference_ids.delete(m.id.to_s)
  95. end
  96. # Go through the remaining ones that are in the array, but not yet in the database.
  97. job_type_preference_ids.each do |g|
  98. self.job_type_preferences.create(:job_type_id => g) unless g.blank?
  99. end
  100. reload
  101. self.job_type_preference_ids = nil
  102. end
  103. end
  104.  
  105. attr_accessor :region_preference_ids # This is an array of job type ids as strings coming from the form of checkboxes.
  106. after_save :update_region_preferences
  107.  
  108. def update_region_preferences
  109. unless region_preference_ids.nil?
  110. self.regions.each do |m|
  111. # Take out the job types we're not interested in anymore.
  112. m.destroy unless region_preference_ids.include?(m.id.to_s)
  113. # We don't have to add it if it's already there in the database.
  114. region_preference_ids.delete(m.id.to_s)
  115. end
  116. # Go through the remaining ones that are in the array, but not yet in the database.
  117. region_preference_ids.each do |g|
  118. self.region_preferences.create(:region_id => g) unless g.blank?
  119. end
  120. reload
  121. self.region_preference_ids = nil
  122. end
  123. end
  124.  
  125. attr_accessor :language_preference_ids # This is an array of job type ids as strings coming from the form of checkboxes.
  126. after_save :update_language_preferences
  127. def update_language_preferences
  128. unless language_preference_ids.nil?
  129. self.languages.each do |m|
  130. # Take out the job types we're not interested in anymore.
  131. m.destroy unless language_preference_ids.include?(m.id.to_s)
  132. # We don't have to add it if it's already there in the database.
  133. language_preference_ids.delete(m.id.to_s)
  134. end
  135. # Go through the remaining ones that are in the array, but not yet in the database.
  136. language_preference_ids.each do |g|
  137. self.language_preferences.create(:language_id => g) unless g.blank?
  138. end
  139. reload
  140. self.language_preference_ids = nil
  141. end
  142. end
  143.  
  144.  
  145. #models_controller.rb
  146. class ModelsController < ResourceController::Base
  147.  
  148. create.response do |wants|
  149. wants.html{
  150. @user = User.new( params[ :user ] )
  151.  
  152. @country = Country.find( params[ :user ][ :country_id ] ) if ! params[:user][:country_id].blank?
  153. @user.country = @country
  154.  
  155. @region = Region.find( params[ :user ][ :region_id ] ) if ! params[:user][:region_id].blank?
  156. @user.region = @region
  157.  
  158. @city = City.find( params[ :user ][ :city_id ] ) if ! params[:user][:city_id].blank?
  159. @user.city = @city
  160.  
  161. @model = Model.new( params[ :model ] )
  162. @model.user = @user
  163.  
  164.  
  165. if @user.save && @model.errors.blank? && @model.errors.blank?
  166. flash[:notice] = "Thanks for registering!"
  167. @object = @model
  168. render :action => :show
  169. else
  170. flash[:notice] = "There were errors."
  171. flash[:errors] = @model.errors
  172. redirect_to new_model_path
  173. end
  174.  
  175. }
  176. end
  177.  
  178.  
  179. # /views/model/new.html.haml
  180. .registration_format
  181. .error= error_messages_for :model, :user
  182.  
  183. - form_for(:model, :url => collection_url) do |f|
  184. = render :partial => "form", :locals => { :f => f }
  185. %p= submit_tag "Register", :id => 'register_submit_button'
  186.  
  187.  
  188. # _form.html.haml
  189.  
  190. %h3 General Info:
  191. %table
  192. %tr
  193. %td
  194. %label{:for => "first_name"} First Name:
  195. %td
  196. =text_field :user, :first_name
  197. %tr
  198. %td
  199. %label{:for => "last_name"} Last Name:
  200. %td
  201. =text_field :user, :last_name
  202. %tr
  203. %td
  204. %label{:for => "user_input_mobile_phone"} Mobile phone:
  205. %td
  206. =text_field :user, :input_mobile_phone
  207. %tr
  208. %td
  209. %label{:for => "email"} Email:
  210. %td
  211. =text_field :user, :email
  212. .white_note * Note: This email will be used as your login id.
  213.  
  214. - if !logged_in?
  215. %tr
  216. %td
  217. %label{:for => "password"} Password:
  218. %td
  219. =password_field :user, :password
  220. %tr
  221. %td
  222. %label{:for => "password_confirmation"} Confirm Password:
  223. %td
  224. =password_field :user, :password_confirmation
  225.  
  226. %tr
  227. %td
  228. %label{:for => "country"} Country:
  229. %td
  230. = select 'user', 'country_id', Country.find(:all).collect { |c| [c.name, c.id] }
  231.  
  232. %tr
  233. %td
  234. %label{:for => "region"} Region:
  235. %td
  236. = select 'user', 'region_id', Region.find(:all).collect {|r| [r.name, r.id] }, { :include_blank => true }
  237.  
  238. %tr
  239. %td
  240. %label{:for => "city_id"} City:
  241. %td
  242. = select 'user', 'city_id', City.find( :all, :order => :name ).collect{ |c| [c.name, c.id] }, { :include_blank => true }
  243.  
  244. %tr
  245. %td
  246. %label{:for => "address"} Address:
  247. %td
  248. = text_area :user, :address, :cols => 30, :rows => 5
  249.  
  250. %tr
  251. %td
  252. %label{:for => "user_birth_day"} Birth Date:
  253. %td
  254. = date_select 'user', 'birth_day', :start_year => 1950, :end_year => 2005, :order => [:month, :day, :year]
  255.  
  256. %br/
  257. %hr{ :width => '50%'}/
  258. %h3 Modeling Info:
  259. %table
  260. %tr
  261. %td
  262. %label{:for => "model_height"} Height:
  263. %td
  264. = select 'model', 'height_feet', (4..7).to_a, { :include_blank => true }
  265. '
  266. = select 'model', 'height_inches', (0..11).to_a, { :include_blank => true }
  267. "
  268. %tr
  269. %td
  270. %label{:for => "model_weight_pounds"} Weight:
  271. %td
  272. = select 'model', 'weight_pounds', (80..200).to_a, { :include_blank => true }
  273. lbs
  274. %tr
  275. %td
  276. %label{:for => "model_body_type_id"} Body Type:
  277. %td= select 'model', 'body_type_id', BodyType.find(:all).collect {|bt| [bt.name, bt.id] }, { :include_blank => true }
  278.  
  279. %tr
  280. %td
  281. %label{:for => "model_skin_color_id"} Skin Complexion:
  282. %td= select 'model', 'skin_color_id', SkinColor.find(:all).collect {|s| [s.name, s.id] }, { :include_blank => true }
  283.  
  284. %tr
  285. %td
  286. %label{:for => "model_minimum_pay"} Minimum pay:
  287. %td
  288. = select 'model', 'minimum_pay', {'Any' => 0, '1,000' => 1000, '1,500' => 1500, '2,000' => 2000, '2,500' => 2500, '3,000' => 3000, '3,500' => 3500}.sort
  289. pesos
  290.  
  291. %br/
  292. %hr{ :width => '50%'}/
  293. %h3 Job Preferences:
  294. %table
  295. %tr
  296. %td{ :colspan => 2 }
  297.  
  298. %tr
  299. %td{ :colspan => 2 }
  300. Modeling Job Types:
  301. = preference_table( JobType, @model )
  302.  
  303. %tr
  304. %td{ :colspan => 2 }
  305. Area(s) where you are willing to work:
  306. = preference_table( Region, @model, 3 )
  307.  
  308. %tr
  309. %td{ :colspan => 2 }
  310. Language(s) that you are fluent in:
  311. = preference_table( Language, @model, 4 )
  312.  
  313.  
  314. %br/
  315. %br/
Add Comment
Please, Sign In to add comment