Skip to content

Instantly share code, notes, and snippets.

@Awea
Created December 5, 2012 01:11
Show Gist options
  • Save Awea/4211010 to your computer and use it in GitHub Desktop.
Save Awea/4211010 to your computer and use it in GitHub Desktop.
Scope refactor
class Project < ActiveRecord::Base
# Scopes
scope :current_application, lambda { |application|
joins(:event).
where(["events.application_id = ?", application.id])
}
scope :ordered, lambda {
order('end ASC')
}
scope :unfinished, lambda {
where("projects.end > ?", Time.now) # auto wrap Time.now with '' thanks AR
}
scope :search, lambda { |params|
joins(:manifolds).
where(
'(title LIKE ? OR manifolds.firstname LIKE ? OR manifolds.lastname LIKE ?)',
"%#{params[:search]}%", "%#{params[:search]}%", "%#{params[:search]}%"
)
}
scope :localize, lambda {
where("projects.locale LIKE ?", I18n.locale.to_s)
}
end
class Front::ProjectsController < FrontController
def index
@projects = @current_application.projects.search(params).unfinished.ordered.localize
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment