Created
June 9, 2018 14:28
-
-
Save CodePint/b057b2e4e29c6534f2a153d4214c3fe5 to your computer and use it in GitHub Desktop.
route.rb
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
<div class="route-selectors"> | |
TBA - selectors to narrow down search results of routes | |
<br> | |
--------------------------------------------------------------- | |
<br> | |
By gym: - | |
<br> | |
By setter: - | |
<br> | |
posted in last: 1/12/24/48/Week | |
<br> | |
grade matching: V1-V10 | |
<br> | |
Enjoy rating: 1-10 | |
<br> | |
Most sent: ASC | |
<br> | |
Most unsent: | |
<br> | |
Tagging system? | |
</div> | |
<div class="climb-advanced-search-form"> | |
<div class="climb-search-title"> | |
<h1>Advanced Search</h1> | |
</div> | |
<%= form_for :route, url: { action: "search" }, method: 'get' do |f| %> | |
<div class="field"> | |
<%= f.label :identifier%><br> | |
<%= f.text_field :identifier %> | |
</div> | |
<div class="field"> | |
<%= f.label :gym_name %><br> | |
<%= f.text_field :gym_name %> | |
</div> | |
<div class="field"> | |
<%= f.label :route_setter %><br> | |
<%= f.text_field :route_setter %> | |
</div> | |
<div class="field"> | |
Created within: <br> | |
<%= f.label :days %><br> | |
<%= f.text_field :time_days, size: 4 %> | |
</div> | |
<div class="field"> | |
<%= f.label :hours %><br> | |
<%= f.text_field :time_hours, size: 4%> | |
</div> | |
<div class="field"> | |
<%= f.label :grade_rating%><br> | |
<%= f.label :min_and_max%><br> | |
<%= f.text_field :grade_rating_min, size: 2 %> | |
- | |
<%= f.text_field :grade_rating_max, size: 2 %> | |
</div> | |
<div class="field"> | |
<%= f.label :grade_sort %><br> | |
<%= f.select(:grade_..orsort, Route::SORT_TYPE) %> | |
</div> | |
<div class="actions"> | |
<%= f.submit "Search" %> | |
</div> | |
<% end %> | |
</div> | |
<br> | |
<div class="route_list"> | |
<% @routes.each do |route| %> | |
<div class="each_route"> | |
<%= link_to "#{route.identifier}", route_path(route) %> | |
<p>Setter: <%= route.user.user_name %></p> | |
<p>Gym: <%= route.gym.gym_name %></p> | |
<p>V: <%= route.grade %></p> | |
</div> | |
<% end %> | |
</div> |
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 Route < ApplicationRecord | |
belongs_to :gym | |
belongs_to :user | |
has_many :climbs | |
has_many :comments | |
SORT_TYPE = ["ASC", "DESC"] | |
def self.search(params) | |
## extracting the nested params hash | |
params = params[:route] | |
## setting the var routes to an array of all the routes in the database | |
routes = Route.all | |
## finding the route on the identifier | |
if params[:identifier].present? | |
routes = routes.where("identifier LIKE ?", "%#{params[:identifier]}") | |
end | |
## finding routes on gym_name | |
# finding all gyms matching the gym_name (and taking the first ones id) | |
if params[:gym_name].present? | |
gyms = Gym.where(gym_name: params[:gym_name]) | |
gym_id = gyms.first.id if gyms.present? | |
# finding the routes matching the gyms foreign key | |
routes = routes.where(gym_id: gym_id) | |
end | |
## finding all routes set by a particular route_setter (user_name) | |
# finding all users matching the user_name (and taking the first ones id) | |
if params[:route_setter].present? | |
users = User.where(user_name: params[:route_setter]) | |
user_id = users.first.id if users.present? | |
# finding the routes matching the route_setter foreign key | |
routes = routes.where(user_id: user_id) | |
end | |
## finding all routes set between current time and specified time | |
#finding the total of hours+days and deducting it from current time | |
if params[:time_days].present? || params[:time_hours].present? | |
days = params[:time_days].to_i.day | |
hours = params[:time_hours].to_i.hours | |
total_time = (days + hours) | |
search_time = (Time.now - total_time) | |
#finding the routes whos creation_date is greater than search_time | |
routes = routes.where("created_at > ?", search_time) | |
end | |
## finding all routes which fall between/above/below the given values | |
# if both minimum and maximum values are given | |
if params[:grade_rating_min].present? && params[:grade_rating_max].present? | |
minimum = params[:grade_rating_min] | |
maximum = params[:grade_rating_max] | |
routes = routes.where("grade >= ? AND grade <= ?", minimum, maximum) | |
end | |
# if just the minimum value us given | |
if params[:grade_rating_min].present? && !params[:grade_rating_max].present? | |
minimum = params[:grade_rating_min] | |
routes = routes.where("grade >= ?", minimum, ) | |
end | |
# if just the maximum value is given | |
if params[:grade_rating_max].present? && !params[:grade_rating_min].present? | |
maximum = params[:grade_rating_max] | |
routes = routes.where("grade <= ?", maximum) | |
end | |
## sorts on bouldering grade (ASC or DSC) | |
if params[:grade_sort] | |
routes = routes.order(grade: params[:grade_sort]) | |
end | |
return routes | |
end | |
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 RoutesController < ApplicationController | |
def index | |
if params[:gym_id] | |
@gym = Gym.find(params[:gym_id]) | |
@routes = Route.where("gym_id = #{@gym.id}") | |
elsif params[:user_id] | |
@user = User.find(params[:user_id]) | |
@routes = Route.where("user_id = #{@user.id}") | |
else | |
@routes = Route.all | |
end | |
end | |
def show | |
@route = Route.find(params[:id]) | |
end | |
def new | |
@user = current_user | |
@gym = Gym.find(params[:gym_id]) | |
if Membership.where(user_id: @user.id, gym_id: @gym.id, route_setter: true) != [] || current_user.admin | |
@route = Route.new | |
else | |
flash[:error]="you do not have correct permmisions to set routes for this gym" | |
puts "not saved" | |
redirect_to gym_path(@gym) | |
end | |
end | |
def create | |
@user = current_user | |
@gym = Gym.find(params[:gym_id]) | |
if Membership.where(user_id: @user.id, gym_id: @gym.id, route_setter: true) != [] || current_user.admin | |
@gym = Gym.find(params[:route][:gym_id]) | |
@route = @gym.routes.new(route_params) | |
@route.user = current_user | |
#binding.pry | |
if @route.save | |
puts "route saved" | |
flash[:success]="New route #{@route.identifier} successfully added." | |
redirect_to gym_path(params[:route][:gym_id]) | |
else | |
flash[:error]= @climb.errors | |
end | |
else | |
flash[:error]="you do not have correct permmisions to set routes for this gym" | |
puts "not saved" | |
redirect_to gym_path(@gym) | |
end | |
end | |
def search | |
@routes = Route.search(params) | |
render :index | |
end | |
def route_params | |
params.require(:route).permit(:identifier, :route_setter_comments, :grade, :gym_id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment