Created
May 6, 2011 16:14
-
-
Save matismasters/959263 to your computer and use it in GitHub Desktop.
Frecuency.rb
This file contains hidden or 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 Frecuency < ActiveRecord::Base | |
| belongs_to :company | |
| belongs_to :from_place, :class_name => "Place", :foreign_key => "from_place_id" | |
| belongs_to :to_place, :class_name => "Place", :foreign_key => "to_place_id" | |
| has_many :frecuency_lines | |
| def name | |
| "desde " + from_place.name + " hasta " + to_place.name + " a las " + from_time.strftime("%H:%M") | |
| end | |
| def short_name | |
| from_place.name + "->" + to_place.name + " " + from_time.strftime("%H:%M") | |
| end | |
| def available_for?(origin_id,destiny_id) | |
| if origin_id == from_place_id && destiny_id == to_place_id | |
| #If the origin and destiny that we are searching for match exactly from_place and to_place from | |
| #the frecuency ite means its available | |
| return true | |
| else | |
| if origin_id == from_place_id | |
| #if the origin is the same as the from_place and some of the mid stops is the destiny | |
| #the frecuency is available | |
| frecuency_lines.each { |fl| return true if fl.place_id == destiny_id} | |
| else | |
| #if the origin and destiny are found one after another within midstops | |
| #the frecuency is available | |
| origin_found_order = 0 | |
| frecuency_lines.each do |fl| | |
| origin_found_order = fl.order if fl.place_id == origin_id | |
| end | |
| origin_found_order = 0 if origin_found_order.nil? | |
| if origin_found_order > 0 | |
| frecuency_lines.each do |fl| | |
| return true if fl.place_id == destiny_id && fl.order > origin_found_order | |
| end | |
| end | |
| return true if origin_found_order > 0 && to_place_id == destiny_id | |
| # In case the mid stop is the origin and the destiny is the destiny of the Frecuency | |
| end | |
| end | |
| false | |
| end | |
| #Returns the time of the place | |
| def time_of_place(place_id,format="%H:%M %p") | |
| if self.from_place_id == place_id | |
| return from_time.strftime(format) | |
| end | |
| if self.to_place_id == place_id | |
| return to_time.strftime(format) | |
| end | |
| self.frecuency_lines.each do |fl| | |
| return fl.estimated_time.strftime(format) if fl.place_id == place_id | |
| end | |
| end | |
| #Returns all the css classes for the HTML in the table | |
| def row_html_classes | |
| "" | |
| end | |
| #CLASS METHODS | |
| #================================================================== | |
| #Returns the possible frecuencies for a given origin and destiny sorted and filtered by departure hour | |
| def self.frecuencies_available_hourly_table(origin_id,destiny_id) | |
| available = self.frecuencies_available(origin_id,destiny_id) | |
| hourly_table = {} | |
| available.each do |frecuency| | |
| hour = frecuency.time_of_place(origin_id,"%H:00 %p") | |
| hourly_table[hour] = [] if hourly_table[hour].nil? | |
| hourly_table[hour] << frecuency | |
| end | |
| hourly_table | |
| end | |
| #Returns the possible frecuencies for a given origin and destiny ID | |
| def self.frecuencies_available(origin_id,destiny_id) | |
| involved = self.frecuencies_involved(origin_id,destiny_id) | |
| available = [] | |
| logger.debug("from_place_id==>"+involved[0].from_place_id.to_s) | |
| logger.debug("to_place_id==>"+involved[0].to_place_id.to_s) | |
| involved.each { |frec| available << frec if frec.available_for?(origin_id,destiny_id) } | |
| available | |
| end | |
| #Returns the possible frecuencies for a given origin | |
| def self.frecuencies_for_origin(origin_id) | |
| self.frecuencies_involved(origin_id) | |
| end | |
| #Returns the frecuencies involved in an origin/destiny search | |
| def self.frecuencies_involved(origin_id,destiny_id=0) | |
| sql_where = "frecuencies.from_place_id = #{origin_id} OR frecuency_lines.place_id = #{origin_id}" | |
| sql_where += " OR frecuencies.to_place_id = #{destiny_id} OR frecuency_lines.place_id = #{destiny_id}" if destiny_id > 0 | |
| self.includes(:frecuency_lines).where(sql_where) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment