1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  def search 
    @states = Community.states 
    if request.post? 
      conditions = [] 
      if params[:community][:approval_status] == "pending"
       conditions << "approval_status = 'pending'"
      end
      if !params[:community][:id].blank?
        conditions << "id = #{params[:community][:id]}"
      elsif !params[:community][:zip].blank?
        conditions << "zip = '#{params[:community][:zip]}'" 
      else
        conditions << "state = '#{params[:community][:state]}'" unless params[:community][:state].blank?
        conditions << "city = '#{params[:community][:city]}'" unless params[:community][:city].blank?
        conditions << "name like '%#{params[:community][:name]}%'" unless params[:community][:name].blank?
        conditions << "id = '#{params[:community][:community]}'" unless params[:community][:community].blank?
      end

      if conditions.any?
        @communities = if !params[:community][:zip].blank? && (params[:community][:radius] rescue 0).to_i > 0
          Community.find_within_radius(params[:community][:radius], params[:community][:zip], :conditions => ["approval_status = approved"])
        else
          if params[:community][:approval_status] == "approved"
            conditions << "approval_status = 'approved'"
          end
          Community.paginate :per_page => 10, :page => params[:page], :conditions => conditions.join(' and ')
        end
        # display the results
        render(:file => "/communities/show.html.erb", :use_full_path => true, :layout => "application")
        
      else
        flash[:error] = "You must specify your search criteria."
        render :action => :search
      end

    else 
      respond_to do |format| 
        format.html # show.html.erb 
        format.xml  { render :xml => @communities } 
      end 
    end 
  end