Created
May 31, 2011 20:55
-
-
Save jonpaul/1001273 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before_filter :set_current_user | |
def new | |
set_tab :sell | |
@listing = VehicleListing.new | |
@vehicle_makes = VehicleMake.all | |
@vehicle_models = VehicleModel.where(:vehicle_make_id => 1) | |
@product = Product.find_by_id(params[:product_id]) | |
if @listing.vehicle_listing_images.empty? | |
@images = VehicleListingImage.where(:cart_id => @cart.id) | |
else | |
@images = @listing.vehicle_listing_images | |
end | |
end | |
def create | |
@product = Product.find_by_id(params[:product_id]) | |
@listing = VehicleListing.new(params[:vehicle_listing]) | |
@listing.product_id = @product | |
@listing.user_id = current_user.id if !current_user.blank? && current_user.is_dealer? | |
if @listing.save | |
unless current_user && !current_user.user_type.blank? && current_user.user_type.name == 'dealer' | |
@cart.add(@product, @product.price, 1, @listing, false) | |
VehicleListingImage.update_listing_id(@cart.id, @cart.items.last.item_id) | |
redirect_to new_order_path | |
else | |
VehicleListingImage.update_listing_id(@cart.id, current_user.vehicle_listings.last.id) | |
puts params[:add_another] | |
unless params[:add_another] == 'true' | |
redirect_to dashboard_path | |
else | |
redirect_to new_vehicle_listing_path(:product_id => 5), :notice => 'Succesfully added a new Vehicle Listing' | |
end | |
end | |
new_params = {} | |
new_params[:opportunity_type] = 'vehicle listing' | |
new_params[:user] = {} | |
unless current_user | |
new_params[:user] = params[:user] | |
else | |
new_params[:user] = 'not yet signed up' | |
end | |
new_params[:vehicle_listing] = params[:vehicle_listing] | |
new_params[:vehicle_listing][:vehicle_make_id] = VehicleMake.find(params[:vehicle_listing][:vehicle_make_id]).name | |
new_params[:vehicle_listing][:vehicle_model_id] = VehicleModel.find(params[:vehicle_listing][:vehicle_model_id]).name | |
InvestorsController.post('http://investyourcar.com:3000/opportunities/create', :body => new_params) | |
else | |
if @listing.vehicle_listing_images.empty? | |
@images = VehicleListingImage.where(:cart_id => @cart.id) | |
else | |
@images = @listing.vehicle_listing_images | |
end | |
@vehicle_makes = VehicleMake.all | |
@vehicle_models = VehicleModel.where(:vehicle_make_id => @listing.vehicle_make_id) | |
render :new | |
end | |
end | |
def edit | |
@listing = VehicleListing.find(params[:id]) | |
@product = @listing.product | |
@vehicle_makes = VehicleMake.all | |
@vehicle_models = VehicleModel.where(:vehicle_make_id => @listing.vehicle_make_id) | |
if @listing.vehicle_listing_images.empty? | |
@images = VehicleListingImage.where(:cart_id => @cart.id) | |
else | |
@images = @listing.vehicle_listing_images | |
end | |
end | |
def update | |
@listing = VehicleListing.find(params[:id]) | |
if @listing.update_attributes(params[:vehicle_listing]) | |
if params[:order] == 'true' | |
redirect_to new_order_path | |
else | |
redirect_to dashboard_path, :notice => 'Succesfully updated your Vehicle Listing' | |
end | |
else | |
@vehicle_makes = VehicleMake.all | |
@vehicle_models = VehicleModel.where(:vehicle_make_id => 1) | |
render :edit | |
end | |
end | |
def destroy | |
@listing = VehicleListing.find(params[:id]) | |
if @listing.destroy | |
@cart.remove(@listing, 1) | |
if request.referer.include?('order') | |
redirect_to new_order_path, :notice => 'Successfully delete Vehicle Listing' | |
else | |
redirect_to dashboard_path, :notice => 'Successfully delete Vehicle Listing' | |
end | |
end | |
end | |
def show | |
@listing = VehicleListing.find(params[:id]) | |
begin | |
@primary_photo = VehicleListingImage.find(@listing.primary_photo_id) unless @listing.primary_photo_id.blank? || @listing.primary_photo_id == 0 | |
rescue ActiveRecord::RecordNotFound | |
@primary_photo = '' | |
end | |
end | |
def search | |
params[:search][:zipcode_distance] = nil if params[:search][:zipcode_within].blank? | |
unless params[:search][:zipcode_distance].blank? | |
@search = VehicleListing.includes(:dealer).zipcode_distance(params[:search][:zipcode_distance]).search(params[:search]) | |
else | |
@search = VehicleListing.search(params[:search]) | |
end | |
@search_total = @search.all | |
if params[:page].blank? | |
params[:page] = 1 | |
offset = false | |
end | |
@listings = WillPaginate::Collection.create(params[:page], 25) do |pager| | |
listings = VehicleListing.limit(25) | |
listings = listings.offset(params[:page]) unless offset == false | |
listings = listings.search(params[:search]).all | |
pager.replace(listings) | |
pager.total_entries = @search_total.count | |
end | |
end | |
def compare | |
@listings = VehicleListing.find(params[:vehicle_id]) | |
respond_to do |format| | |
format.html { render :layout => false } | |
end | |
end | |
def get_vin_data | |
json = VinQuery.get_vin_data(params[:vin_number]) | |
unless json == false | |
render :json => { :status => 'success', :data => json } | |
else | |
render :json => { :status => 'failure' } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment