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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/winedb/models/winery.rb', line 39
def self.create_or_update_from_attribs( new_attributes, values )
logger = LogKernel::Logger.root
value_tag_keys = []
new_attributes[ :grade ] ||= 4
value_tag_keys += find_tags_in_attribs!( new_attributes )
values.each_with_index do |value,index|
if match_country(value) do |country|
new_attributes[ :country_id ] = country.id
end
elsif match_region_for_country(value,new_attributes[:country_id]) do |region|
new_attributes[ :region_id ] = region.id
end
elsif match_city(value) do |city|
if city.present?
new_attributes[ :city_id ] = city.id
else
logger.warn "city with key #{value[5..-1]} missing - for winery #{new_attributes[:key]}"
end
if city.present? && city.region.present?
new_attributes[ :region_id ] = city.region.id
end
end
elsif match_year( value ) do |num| new_attributes[ :since ] = num
end
elsif match_website( value ) do |website| new_attributes[ :web ] = website
end
elsif is_address?( value ) new_attributes[ :address ] = normalize_addr( value )
elsif value =~ /^(\?+|[0-9_]+)\s*ha$/ logger.debug " skipping area (in ha) for now >#{value}<"
elsif value =~ /^(\?+|[0-9_]+)\s*hl$/ logger.debug " skipping prod (in hl) for now >#{value}<"
elsif (values.size==(index+1)) && is_taglist?( value ) logger.debug " found tags: >>#{value}<<"
value_tag_keys += find_tags( value )
else
logger.warn "unknown type for value >#{value}< - key #{new_attributes[:key]}"
end
end
rec = Winery.find_by_key( new_attributes[ :key ] )
if rec.present?
logger.debug "update Winery #{rec.id}-#{rec.key}:"
else
logger.debug "create Winery:"
rec = Winery.new
end
logger.debug new_attributes.to_json
rec.update_attributes!( new_attributes )
if new_attributes[:city_id].blank? &&
new_attributes[:country_id].present? &&
new_attributes[:region_id].present?
country_key = rec.country.key
if country_key == 'at' || country_key == 'de'
city_title = find_city_in_addr( new_attributes[:address], country_key )
if city_title.present?
city_values = [city_title]
city_attributes = {
country_id: rec.country_id,
region_id: rec.region_id
}
city = City.create_or_update_from_values( city_values, city_attributes )
rec.city_id = city.id
rec.save!
else
logger.warn "auto-add city for #{new_attributes[:key]} (#{country_key}) >>#{new_attributes[:address]}<< failed; no city title found"
end
end
end
if value_tag_keys.size > 0
value_tag_keys.uniq! logger.debug " adding #{value_tag_keys.size} taggings: >>#{value_tag_keys.join('|')}<<..."
value_tag_keys.each do |key|
tag = Tag.find_by_key( key )
if tag.nil? logger.debug " creating tag >#{key}<"
tag = Tag.create!( key: key )
end
rec.tags << tag
end
end
rec
end
|