Class: SportDb::Model::Team
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SportDb::Model::Team
- Defined in:
- lib/sportdb/models/team_compat.rb,
lib/sportdb/models/team.rb,
lib/sportdb/models/forward.rb
Overview
collect depreciated or methods for future removal here
-
keep for now for compatibility (for old code)
Class Method Summary collapse
- .create_from_ary!(teams, more_values = {}) ⇒ Object
- .create_or_update_from_values(new_attributes, values) ⇒ Object
Instance Method Summary collapse
-
#games ⇒ Object
fix!!! - how to do it with has_many macro? use finder_sql? finder_sql is depreciated in Rails 4!!! use -> { where() } etc.
-
#key ⇒ Object
todo/fix: must be 3 or more letters (plus allow digits e.g. salzburgii, muenchen1980, etc.) - why? why not??.
-
#name ⇒ Object
fix/todo: change title to name; title2 to name2 etc.
- #past_games ⇒ Object
- #upcoming_games ⇒ Object
Class Method Details
.create_from_ary!(teams, more_values = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/sportdb/models/team_compat.rb', line 13 def self.create_from_ary!( teams, more_values={} ) teams.each do |values| ## key & title required attr = { key: values[0] } ## title (split of optional synonyms) # e.g. FC Bayern Muenchen|Bayern Muenchen|Bayern titles = values[1].split('|') attr[ :title ] = titles[0] ## add optional synonyms attr[ :synonyms ] = titles[1..-1].join('|') if titles.size > 1 attr = attr.merge( more_values ) ## check for optional values values[2..-1].each do |value| if value.is_a? Country attr[ :country_id ] = value.id elsif value.is_a? City attr[ :city_id ] = value.id elsif value =~ /#{TEAM_CODE_PATTERN}/ ## assume its three letter code (e.g. ITA or S04 etc.) attr[ :code ] = value elsif value =~ /^city:/ ## city: value_city_key = value[5..-1] ## cut off city: prefix value_city = City.find_by_key!( value_city_key ) attr[ :city_id ] = value_city.id else attr[ :title2 ] = value end end ## check if exists team = Team.find_by_key( values[0] ) if team.present? puts "*** warning team with key '#{values[0]}' exists; skipping create" else Team.create!( attr ) end end # each team end |
.create_or_update_from_values(new_attributes, values) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/sportdb/models/team.rb', line 59 def self.create_or_update_from_values( new_attributes, values ) ## fix: add/configure logger for ActiveRecord!!! logger = LogUtils::Logger.root ## check optional values values.each_with_index do |value, index| if value =~ /^city:/ ## city: value_city_key = value[5..-1] ## cut off city: prefix value_city = City.find_by_key( value_city_key ) if value_city.present? new_attributes[ :city_id ] = value_city.id else ## todo/fix: add strict mode flag - fail w/ exit 1 in strict mode logger.warn "city with key #{value_city_key} missing" ## todo: log errors to db log??? end elsif value =~ /^(18|19|20)[0-9]{2}$/ ## assume founding year -- allow 18|19|20 ## logger.info " founding/opening year #{value}" new_attributes[ :since ] = value.to_i elsif value =~ /\/{2}/ # assume it's an address line e.g. xx // xx ## logger.info " found address line #{value}" new_attributes[ :address ] = value elsif value =~ /^(?:[a-z]{2}\.)?wikipedia:/ # assume it's wikipedia e.g. [es.]wikipedia: logger.info " found wikipedia line #{value}; skipping for now" elsif value =~ /(^www\.)|(\.com$)/ # FIX: !!!! use a better matcher not just www. and .com new_attributes[ :web ] = value elsif value =~ /^[A-Z][A-Z0-9][A-Z0-9_]?$/ ## assume two or three-letter code e.g. FCB, RBS, etc. new_attributes[ :code ] = value elsif value =~ /^[a-z]{2,3}$/ ## assume two or three-letter country key e.g. at,de,mx, or eng,sco,wal,nir etc. ## fix: if country does NOT match / NOT found - just continue w/ next match!!!! # - just issue an error/warn do NOT crash value_country = Country.find_by_key!( value ) new_attributes[ :country_id ] = value_country.id else ## todo: assume title2 ?? # issue warning: unknown type for value logger.warn "unknown type for value >#{value}< - key #{new_attributes[:key]}" end end rec = Team.find_by_key( new_attributes[ :key ] ) if rec.present? logger.debug "update Team #{rec.id}-#{rec.key}:" else logger.debug "create Team:" rec = Team.new end logger.debug new_attributes.to_json rec.update_attributes!( new_attributes ) end |
Instance Method Details
#games ⇒ Object
fix!!! - how to do it with has_many macro? use finder_sql?
finder_sql is depreciated in Rails 4!!!
use -> { where() } etc. -- try it if it works
keep as is! best solution ??
a discussion here -> https://github.com/rails/rails/issues/9726
a discussion here (not really helpful) -> http://stackoverflow.com/questions/2125440/activerecord-has-many-where-two-columns-in-table-a-are-primary-keys-in-table-b
36 37 38 |
# File 'lib/sportdb/models/team.rb', line 36 def games Game.where( 'team1_id = ? or team2_id = ?', id, id ).order( 'play_at' ) end |
#key ⇒ Object
todo/fix: must be 3 or more letters (plus allow digits e.g. salzburgii, muenchen1980, etc.) - why? why not??
20 |
# File 'lib/sportdb/models/team.rb', line 20 validates :key, format: { with: /#{TEAM_KEY_PATTERN}/, message: TEAM_KEY_PATTERN_MESSAGE } |
#name ⇒ Object
fix/todo: change title to name; title2 to name2 etc.
56 |
# File 'lib/sportdb/models/team.rb', line 56 def name() title; end |
#past_games ⇒ Object
44 45 46 |
# File 'lib/sportdb/models/team.rb', line 44 def past_games Game.where( 'team1_id = ? or team2_id = ?', id, id ).where( 'play_at < ?', Time.now ).order( 'play_at desc' ) end |
#upcoming_games ⇒ Object
40 41 42 |
# File 'lib/sportdb/models/team.rb', line 40 def upcoming_games Game.where( 'team1_id = ? or team2_id = ?', id, id ).where( 'play_at > ?', Time.now ).order( 'play_at' ) end |