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
58
59
|
# File 'lib/generators/scaffold_plus/geocoder/geocoder_generator.rb', line 23
def update_model
geocoded = " geocoded_by :#{options.address}"
geocoded << ", latitude: :#{options.latitude}"
geocoded << ", longitude: :#{options.longitude}"
if options.country?
geocoded << " do |obj,results|"
country = [
" if geo = results.first",
" # puts JSON.pretty_generate(geo.as_json)",
" if geo.country_code.upcase == 'DE'",
" obj.country = 'DE'",
" obj.address = geo.address.gsub(/, Deutschland/, '')",
" if obj.#{options.latitude}.blank? and obj.#{options.longitude}.blank?",
" obj.#{options.latitude} = geo.latitude",
" obj.#{options.longitude} = geo.longitude",
" end",
" end",
" end",
" end"
]
else
country = []
end
lines = options.before? ? [ "" ] : []
lines << [
geocoded,
country,
" after_validation :geocode" +
", if: ->(obj){ obj.#{options.address}.present?" +
" and obj.#{options.address}_changed? }",
""
]
lines << "" if options.after?
inject_into_class "app/models/#{name}.rb", class_name do
lines.flatten.join("\n")
end
end
|