Sha256: 042927cbab713d20aa18a5261d645ec1a1669f8ba5608772b0f987f90a32e2b5

Contents?: true

Size: 1.7 KB

Versions: 4

Compression:

Stored size: 1.7 KB

Contents

module SportDb::Models


class Person < ActiveRecord::Base
  self.table_name = 'persons'  #  avoids possible "smart" plural inflection to people

  has_many :goals

  belongs_to :country, :class_name => 'WorldDb::Models::Country', :foreign_key => 'country_id'


  def title    # alias for name
    name
  end

  def title=(value)  # alias for name
    self.name = value
  end


  def self.create_or_update_from_values( new_attributes, values )

    ## fix: add/configure logger for ActiveRecord!!!
    logger = LogKernel::Logger.root

    ## check optional values
    values.each_with_index do |value, index|
      if value =~ /^[a-z]{2}$/  ## assume two-letter country key e.g. at,de,mx,etc.
        value_country = Country.find_by_key!( value )
        new_attributes[ :country_id ] = value_country.id
      else
        ## todo: assume title2 ??
        ## assume title2 if title2 is empty (not already in use)
        ##  and if it title2 contains at least two letter e.g. [a-zA-Z].*[a-zA-Z]
        # issue warning: unknown type for value
        logger.warn "unknown type for value >#{value}< - key #{new_attributes[:key]}"
      end
    end

    ## quick hack: set nationality_id if not present to country_id
    if new_attributes[ :nationality_id ].blank?
      new_attributes[ :nationality_id ] = new_attributes[ :country_id ]
    end

    rec = Person.find_by_key( new_attributes[ :key ] )
    if rec.present?
      logger.debug "update Person #{rec.id}-#{rec.key}:"
    else
      logger.debug "create Person:"
      rec = Person.new
    end
      
    logger.debug new_attributes.to_json
   
    rec.update_attributes!( new_attributes )
  end # create_or_update_from_values


end  # class Person


end # module SportDb::Models

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sportdb-1.6.12 lib/sportdb/models/person.rb
sportdb-1.6.11 lib/sportdb/models/person.rb
sportdb-1.6.10 lib/sportdb/models/person.rb
sportdb-1.6.9 lib/sportdb/models/person.rb