Class: SportDb::Model::Assoc
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SportDb::Model::Assoc
- Defined in:
- lib/sportdb/models/forward.rb,
lib/sportdb/models/assoc.rb
Overview
nb: for now only team and league use worlddb tables
e.g. with belongs_to assoc (country,region)
Class Method Summary collapse
Instance Method Summary collapse
-
#all_assocs ⇒ Object
note: includes all member (sub assocs + national assocs) - rename to member_assocs?.
-
#member_assoc_assocs ⇒ Object
child_assocs - use child_assocs? - (direct) member/child assocs instead of member?.
-
#parent_assocs ⇒ Object
for now can have more than one (direct) parent assoc e.g.
-
#sub_assocs ⇒ Object
use zone/region as name instead of sub ( for confederatons,zones,etc.).
Class Method Details
.create_or_update_from_values(new_attributes, values) ⇒ Object
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 58 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 |
# File 'lib/sportdb/models/assoc.rb', line 33 def self.create_or_update_from_values( new_attributes, values ) ## fix: add/configure logger for ActiveRecord!!! logger = LogUtils::Logger.root assoc_keys = [] # by default no association (e.g. fifa,uefa,etc.) ## check optional values values.each_with_index do |value, index| if 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]{2}$/ ## assume two-letter country key e.g. at,de,mx,etc. ## ## fix: allow country letter with three e.g. eng,sco,wal,nir, etc. !!! ## value_country = Country.find_by_key!( value ) ## new_attributes[ :country_id ] = value_country.id elsif value =~ /^[a-z]{2}$/ ## assume two-letter country key e.g. at,de,mx,etc. ## fix: allow country letter with three e.g. eng,sco,wal,nir, etc. !!! ## fix: if country does NOT match / NOT found - just coninue 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 ## note: if country present - assume it's a national assoc, thus, set flag to true new_attributes[ :national ] = true elsif value =~ /^[a-z|]+$/ ## looks like a tag list e.g. fifa|uefa or fifa|caf or ocf? logger.info " trying adding assocs using keys >#{value}<" assoc_keys = value.split('|') else ## todo: assume title2 ?? # issue warning: unknown type for value logger.warn "unknown type for value >#{value}< - key #{new_attributes[:key]}" end end rec = Assoc.find_by_key( new_attributes[ :key ] ) if rec.present? logger.debug "update Assoc #{rec.id}-#{rec.key}:" else logger.debug "create Assoc:" rec = Assoc.new end logger.debug new_attributes.to_json rec.update_attributes!( new_attributes ) unless assoc_keys.empty? ## add team to assocs assoc_keys.each do |assoc_key| assoc = Assoc.find_by_key!( assoc_key ) logger.debug " adding assoc to assoc >#{assoc.title}<" ### todo/fix: how can we delete assoc_assocs? for now only update n create assoc_assoc = AssocAssoc.where( assoc1_id: assoc.id, assoc2_id: rec.id ).first if assoc_assoc.nil? ## create if does NOT exist yet AssocAssoc.create!( assoc1_id: assoc.id, assoc2_id: rec.id ) end end end end |
Instance Method Details
#all_assocs ⇒ Object
note: includes all member (sub assocs + national assocs) - rename to member_assocs?
19 |
# File 'lib/sportdb/models/assoc.rb', line 19 has_many :all_assocs, class_name: 'Assoc', :source => :assoc2, :through => :member_assoc_assocs |
#member_assoc_assocs ⇒ Object
child_assocs - use child_assocs? - (direct) member/child assocs instead of member?
8 |
# File 'lib/sportdb/models/assoc.rb', line 8 has_many :member_assoc_assocs, class_name: 'AssocAssoc', foreign_key: 'assoc1_id' |
#parent_assocs ⇒ Object
for now can have more than one (direct) parent assoc
e.g. Africa Fed and Arab League Fed
27 |
# File 'lib/sportdb/models/assoc.rb', line 27 has_many :parent_assocs, class_name: 'Assoc', :source => :assoc1, :through => :parent_assoc_assocs |
#sub_assocs ⇒ Object
use zone/region as name instead of sub ( for confederatons,zones,etc.)
21 |
# File 'lib/sportdb/models/assoc.rb', line 21 has_many :sub_assocs, -> { where( national: false ) }, class_name: 'Assoc', :source => :assoc2, :through => :member_assoc_assocs |