Created
December 5, 2012 01:11
-
-
Save Awea/4211010 to your computer and use it in GitHub Desktop.
Scope refactor
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
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 |
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
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