Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ManagerSearch < ActiveRecord::Base
- belongs_to :account
- has_many :selected_managers
- before_save :format_postcode
- before_update :format_postcode
- validates_as_uk_postcode :full_postcode
- validates_presence_of :distance
- def format_postcode
- self.full_postcode = self.full_postcode.delete(" ").upcase.insert(-4, ' ')
- end
- def self.managers(search)
- @managers ||= find_managers(search)
- end
- def self.managers_for_vacancy(search)
- @managers ||= find_managers(search)
- end
- def self.find_managers(search)
- distance = search.distance if !search.distance.nil?
- grid = self.postcode_grid(search.full_postcode)
- Account.managers_only.in_range(grid[:x], grid[:y], distance).find(:all,
- :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 ) )",
- :conditions => conditions(search),
- :include => [ :managers_info, :personal_info ])
- end
- def self.postcode_grid(postcode)
- Postcode.get_grids(postcode)
- end
- # puts educational conditions togethere
- def self.education_conditions(search)
- conditions = Array.new
- values = Array.new
- a_levels = search.a_levels if search.methods.include?("a_levels")
- nvq3 = search.nvq3 if search.methods.include?("nvq3")
- nvq4 = search.nvq4 if search.methods.include?("nvq4")
- rma = search.rma if search.methods.include?("rma")
- rma_in_progress = search.rma_in_progress if search.methods.include?("rma_in_progress")
- degree = search.degree if search.methods.include?("degree")
- conditions.push("managers_infos.a_levels = 1 OR managers_infos.nvq3 = 1") if a_levels
- conditions.push("managers_infos.nvq3 = 1") if nvq3
- conditions.push("managers_infos.nvq4 = 1 OR managers_infos.rma = 1") if nvq4
- conditions.push("managers_infos.rma = 1") if rma
- conditions.push("managers_infos.rma_in_progress = 1") if rma_in_progress
- conditions.push("managers_infos.degree <> \"\"") if degree
- self.puts_partial_conditions_together(conditions," OR ")
- end
- def self.current_status_conditions(search)
- conditions = Array.new
- values = Array.new
- regional_manager = search.regional_manager if search.methods.include?("regional_manager")
- deputy_manager = search.deputy_manager if search.methods.include?("deputy_manager")
- manager = search.manager if search.methods.include?("manager")
- other = search.other if search.methods.include?("other")
- conditions.push("managers_infos.current_status = " + regional_manager.to_s) if regional_manager != 0 && !regional_manager.nil?
- conditions.push("managers_infos.current_status = " + deputy_manager.to_s) if deputy_manager != 0 && !regional_manager.nil?
- conditions.push("managers_infos.current_status = " + manager.to_s) if manager != 0 && !regional_manager.nil?
- conditions.push("managers_infos.current_status = " + other.to_s) if other != 0 && !regional_manager.nil?
- self.puts_partial_conditions_together(conditions," OR ")
- end
- # pusts professional qualifications conditions together
- def self.prof_qualif_conditions(search)
- conditions = Array.new
- values = Array.new
- rgn = search.rgn if search.methods.include?("rgn")
- rmn = search.rmn if search.methods.include?("rmn")
- dual_reg = search.dual_reg if search.methods.include?("dual_reg")
- non_nurse_residential = search.non_nurse_residential if search.methods.include?("non_nurse_residential")
- non_nurse_ld = search.non_nurse_ld if search.methods.include?("non_nurse_ld")
- conditions.push("managers_infos.professional_qualification = " + rgn.to_s) if rgn != 0
- conditions.push("managers_infos.professional_qualification = " + rmn.to_s) if rmn != 0
- conditions.push("managers_infos.professional_qualification = " + dual_reg.to_s) if dual_reg != 0
- conditions.push("managers_infos.professional_qualification = " + non_nurse_residential.to_s) if non_nurse_residential != 0
- conditions.push("managers_infos.professional_qualification = " + non_nurse_ld.to_s) if non_nurse_ld != 0
- self.puts_partial_conditions_together(conditions," OR ")
- end
- def self.length_of_time_in_current_role_condition(search)
- conditions = Array.new
- years_in_current_role = search.years_in_current_role if search.methods.include?("years_in_current_role")
- conditions.push("managers_infos.years_in_current_role >= " + years_in_current_role.to_s) if years_in_current_role != nil
- self.puts_partial_conditions_together(conditions, " OR ")
- end
- def self.notice_period_condition(search)
- conditions = Array.new
- notice_period = search.notice_period if search.methods.include?("notice_period")
- conditions.push("managers_infos.notice_period <= " + notice_period.to_s) if notice_period
- self.puts_partial_conditions_together(conditions, " OR ")
- end
- def self.expected_salary_condition(search)
- conditions = Array.new
- expected_salary = search.expected_sallary if search.methods.include?("expected_sallary")
- expected_salary = search.salary if search.methods.include?("salary")
- conditions.push("(managers_infos.expected_sallary <= " + expected_salary.to_s + " OR managers_infos.expected_sallary IS NULL)") if expected_salary
- self.puts_partial_conditions_together(conditions, " OR ")
- end
- def self.expected_bonus_condition(search)
- conditions = Array.new
- expected_bonus = search.expected_bonus if search.methods.include?("expected_bonus")
- expected_bonus = search.bonus if search.methods.include?("bonus")
- conditions.push("(managers_infos.expected_bonus <= " + expected_bonus.to_s + " OR managers_infos.expected_bonus IS NULL)") if expected_bonus
- self.puts_partial_conditions_together(conditions, " OR ")
- end
- # puts all conditions together
- def self.conditions(search)
- conditions = Array.new
- conditions.push("(" + self.education_conditions(search) + ")") if self.education_conditions(search)
- conditions.push("(" + self.prof_qualif_conditions(search) + ")") if self.prof_qualif_conditions(search)
- conditions.push("(" + self.current_status_conditions(search) + ")") if self.current_status_conditions(search)
- conditions.push("(" + self.length_of_time_in_current_role_condition(search) + ")") if self.length_of_time_in_current_role_condition(search)
- conditions.push("(" + self.notice_period_condition(search) + ")") if self.notice_period_condition(search)
- conditions.push("(" + self.expected_salary_condition(search) + ")") if self.expected_salary_condition(search)
- conditions.push("(" + self.expected_bonus_condition(search) + ")") if self.expected_bonus_condition(search)
- self.puts_partial_conditions_together(conditions," AND ")
- end
- def self.puts_partial_conditions_together(cond_array,cond_join)
- new_condition = ""
- cond_array.reverse.each do |cond|
- or_join = ""
- or_join = cond_join if new_condition != ""
- new_condition = cond + or_join + new_condition
- end
- new_condition = nil if new_condition == ""
- new_condition
- end
- end
Add Comment
Please, Sign In to add comment