Guest User

Untitled

a guest
May 24th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. class ManagerSearch < ActiveRecord::Base
  2. belongs_to :account
  3. has_many :selected_managers
  4.  
  5.  
  6. before_save :format_postcode
  7. before_update :format_postcode
  8.  
  9.  
  10. validates_as_uk_postcode :full_postcode
  11. validates_presence_of :distance
  12.  
  13.  
  14. def format_postcode
  15. self.full_postcode = self.full_postcode.delete(" ").upcase.insert(-4, ' ')
  16. end
  17.  
  18.  
  19. def self.managers(search)
  20. @managers ||= find_managers(search)
  21. end
  22.  
  23.  
  24. def self.managers_for_vacancy(search)
  25. @managers ||= find_managers(search)
  26. end
  27.  
  28.  
  29. def self.find_managers(search)
  30. distance = search.distance if !search.distance.nil?
  31. grid = self.postcode_grid(search.full_postcode)
  32.  
  33. Account.managers_only.in_range(grid[:x], grid[:y], distance).find(:all,
  34. :order => "( ROUND( ( SQRT( ( ABS( postcodes.gridx - " + grid[:x].to_s + " ) * ABS( postcodes.gridx - " + grid[:x].to_s + " ) ) + ( ABS( postcodes.gridy - " + grid[:y].to_s + " ) * ABS( postcodes.gridy - " + grid[:y].to_s + " ) ) ) ) / 1609.344 ) )",
  35. :conditions => conditions(search),
  36. :include => [ :managers_info, :personal_info ])
  37. end
  38.  
  39. def self.postcode_grid(postcode)
  40. Postcode.get_grids(postcode)
  41. end
  42.  
  43.  
  44. # puts educational conditions togethere
  45. def self.education_conditions(search)
  46.  
  47. conditions = Array.new
  48. values = Array.new
  49.  
  50. a_levels = search.a_levels if search.methods.include?("a_levels")
  51. nvq3 = search.nvq3 if search.methods.include?("nvq3")
  52. nvq4 = search.nvq4 if search.methods.include?("nvq4")
  53. rma = search.rma if search.methods.include?("rma")
  54. rma_in_progress = search.rma_in_progress if search.methods.include?("rma_in_progress")
  55. degree = search.degree if search.methods.include?("degree")
  56.  
  57. conditions.push("managers_infos.a_levels = 1 OR managers_infos.nvq3 = 1") if a_levels
  58. conditions.push("managers_infos.nvq3 = 1") if nvq3
  59. conditions.push("managers_infos.nvq4 = 1 OR managers_infos.rma = 1") if nvq4
  60. conditions.push("managers_infos.rma = 1") if rma
  61. conditions.push("managers_infos.rma_in_progress = 1") if rma_in_progress
  62. conditions.push("managers_infos.degree <> \"\"") if degree
  63.  
  64. self.puts_partial_conditions_together(conditions," OR ")
  65. end
  66.  
  67.  
  68. def self.current_status_conditions(search)
  69.  
  70. conditions = Array.new
  71. values = Array.new
  72.  
  73. regional_manager = search.regional_manager if search.methods.include?("regional_manager")
  74. deputy_manager = search.deputy_manager if search.methods.include?("deputy_manager")
  75. manager = search.manager if search.methods.include?("manager")
  76. other = search.other if search.methods.include?("other")
  77. conditions.push("managers_infos.current_status = " + regional_manager.to_s) if regional_manager != 0 && !regional_manager.nil?
  78. conditions.push("managers_infos.current_status = " + deputy_manager.to_s) if deputy_manager != 0 && !regional_manager.nil?
  79. conditions.push("managers_infos.current_status = " + manager.to_s) if manager != 0 && !regional_manager.nil?
  80. conditions.push("managers_infos.current_status = " + other.to_s) if other != 0 && !regional_manager.nil?
  81.  
  82. self.puts_partial_conditions_together(conditions," OR ")
  83. end
  84.  
  85.  
  86. # pusts professional qualifications conditions together
  87. def self.prof_qualif_conditions(search)
  88. conditions = Array.new
  89. values = Array.new
  90.  
  91. rgn = search.rgn if search.methods.include?("rgn")
  92. rmn = search.rmn if search.methods.include?("rmn")
  93. dual_reg = search.dual_reg if search.methods.include?("dual_reg")
  94. non_nurse_residential = search.non_nurse_residential if search.methods.include?("non_nurse_residential")
  95. non_nurse_ld = search.non_nurse_ld if search.methods.include?("non_nurse_ld")
  96.  
  97. conditions.push("managers_infos.professional_qualification = " + rgn.to_s) if rgn != 0
  98. conditions.push("managers_infos.professional_qualification = " + rmn.to_s) if rmn != 0
  99. conditions.push("managers_infos.professional_qualification = " + dual_reg.to_s) if dual_reg != 0
  100. conditions.push("managers_infos.professional_qualification = " + non_nurse_residential.to_s) if non_nurse_residential != 0
  101. conditions.push("managers_infos.professional_qualification = " + non_nurse_ld.to_s) if non_nurse_ld != 0
  102.  
  103. self.puts_partial_conditions_together(conditions," OR ")
  104. end
  105.  
  106.  
  107. def self.length_of_time_in_current_role_condition(search)
  108. conditions = Array.new
  109. years_in_current_role = search.years_in_current_role if search.methods.include?("years_in_current_role")
  110. conditions.push("managers_infos.years_in_current_role >= " + years_in_current_role.to_s) if years_in_current_role != nil
  111. self.puts_partial_conditions_together(conditions, " OR ")
  112. end
  113.  
  114.  
  115. def self.notice_period_condition(search)
  116. conditions = Array.new
  117. notice_period = search.notice_period if search.methods.include?("notice_period")
  118. conditions.push("managers_infos.notice_period <= " + notice_period.to_s) if notice_period
  119. self.puts_partial_conditions_together(conditions, " OR ")
  120. end
  121.  
  122.  
  123. def self.expected_salary_condition(search)
  124. conditions = Array.new
  125. expected_salary = search.expected_sallary if search.methods.include?("expected_sallary")
  126. expected_salary = search.salary if search.methods.include?("salary")
  127. conditions.push("(managers_infos.expected_sallary <= " + expected_salary.to_s + " OR managers_infos.expected_sallary IS NULL)") if expected_salary
  128. self.puts_partial_conditions_together(conditions, " OR ")
  129. end
  130.  
  131.  
  132. def self.expected_bonus_condition(search)
  133. conditions = Array.new
  134. expected_bonus = search.expected_bonus if search.methods.include?("expected_bonus")
  135. expected_bonus = search.bonus if search.methods.include?("bonus")
  136. conditions.push("(managers_infos.expected_bonus <= " + expected_bonus.to_s + " OR managers_infos.expected_bonus IS NULL)") if expected_bonus
  137. self.puts_partial_conditions_together(conditions, " OR ")
  138. end
  139.  
  140.  
  141. # puts all conditions together
  142. def self.conditions(search)
  143. conditions = Array.new
  144.  
  145. conditions.push("(" + self.education_conditions(search) + ")") if self.education_conditions(search)
  146. conditions.push("(" + self.prof_qualif_conditions(search) + ")") if self.prof_qualif_conditions(search)
  147. conditions.push("(" + self.current_status_conditions(search) + ")") if self.current_status_conditions(search)
  148. conditions.push("(" + self.length_of_time_in_current_role_condition(search) + ")") if self.length_of_time_in_current_role_condition(search)
  149. conditions.push("(" + self.notice_period_condition(search) + ")") if self.notice_period_condition(search)
  150. conditions.push("(" + self.expected_salary_condition(search) + ")") if self.expected_salary_condition(search)
  151. conditions.push("(" + self.expected_bonus_condition(search) + ")") if self.expected_bonus_condition(search)
  152.  
  153. self.puts_partial_conditions_together(conditions," AND ")
  154. end
  155.  
  156.  
  157.  
  158. def self.puts_partial_conditions_together(cond_array,cond_join)
  159. new_condition = ""
  160. cond_array.reverse.each do |cond|
  161. or_join = ""
  162. or_join = cond_join if new_condition != ""
  163. new_condition = cond + or_join + new_condition
  164. end
  165.  
  166. new_condition = nil if new_condition == ""
  167. new_condition
  168. end
  169. end
Add Comment
Please, Sign In to add comment