2013-01-08 22:47:42 +01:00

44 lines
1.5 KiB
Ruby

# -*- encoding : utf-8 -*-
class Sheet < ActiveRecord::Base
has_many :people, :dependent => :destroy
has_many :sheet_years, :order => "year DESC", :dependent => :destroy
has_many :donates, :order => "paid_at DESC", :dependent => :destroy
has_one :last_year_m, :order => "year DESC", :class_name => "SheetYear"
has_one :first_year_m, :order => "year", :class_name => "SheetYear"
belongs_to :person, :class_name => "Person", :foreign_key => :first_people
belongs_to :first_people, :class_name => "Person", :foreign_key => :first_people
validates :sheet_type, :presence => true
attr_accessible :sheet_type, :people_attributes, :corporate, :address, :address2, :address3, :pc, :city, :country, :skills
accepts_nested_attributes_for :people
SHEET_TYPE = {1 => "adherent",2 => "sympathisant"}
def self.search_by_params(input)
requette = ""
requette += "(last_year IN(?))" if input[:sheet_year]
requette += " AND " if input[:sheet_year] and input[:sheet_type]
requette += "(sheet_type IN(?))" if input[:sheet_type]
requette += " AND " if (input[:sheet_year] or input[:sheet_type]) and input[:sheet_city]
requette += "(city IN(?))" if input[:sheet_city]
conditions = []
conditions << requette
conditions << input[:sheet_year] if input[:sheet_year]
conditions << input[:sheet_type] if input[:sheet_type]
conditions << input[:sheet_city] if input[:sheet_city]
return Sheet.all(:conditions => conditions,:include => :person)
end
end