Skip to content

Instantly share code, notes, and snippets.

@kwerle
Forked from nerdyworm/app_controller.rb
Created January 3, 2012 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwerle/1555587 to your computer and use it in GitHub Desktop.
Save kwerle/1555587 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
protect_from_forgery
def current_search
Search.find(session[:search_id])
end
def current_search?
session[:search_id].present?
end
def source?
params[:source_id].present?
end
def source
Source.find(params[:source_id])
end
end
class MixesController < ApplicationController
respond_to :html, :json
def index
@mixes = mixes.published.page(params[:page]).per(50)
respond_with @mixes
end
private
def mixes
if source?
source.mixes
elsif current_search?
current_search.mixes
else
Mix
end
end
end
class Search < ActiveRecord::Base
def mixes
mixes = Mix.published
mixes = mixes.genre(genres) if genres.present?
mixes.limit(50)
end
end
class SearchesController < ApplicationController
respond_to :html, :json
def show
@search = Search.find(params[:id])
respond_with @search
end
def update
@search = Search.find(params[:id])
@search.update_attributes(params[:search])
respond_with @search
end
def create
@search = Search.create(params[:search])
session[:search_id] = @search.id
respond_with @search
end
end
class Source < ActiveRecord::Base
has_many :mixes, :dependent => :destroy
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment