Class: CitiesTasks

Inherits:
Object
  • Object
show all
Defined in:
lib/cities_tasks.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attach_reports_to_citiesObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cities_tasks.rb', line 22

def self.attach_reports_to_cities
  old_cities = SqlCity.find :all
  old_cities.each do |old_city|
    old_city.reports.each do |old_report|
      new_report = Report.where( :name_seo => old_report.name_seo ).first
      if new_report.blank?
        unless Rails.env.test?
          puts "this name_seo does not exist:"
          puts old_report.name_seo
        end
      else
        new_city = City.where( :cityname => old_report.city.name_seo ).first
        new_report.city = new_city
        new_report.save
      end
    end
  end
end

.to_mongoidObject

include Rails.application.routes.url_helpers



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cities_tasks.rb', line 6

def self.to_mongoid
  old_cities = SqlCity.find :all
  old_cities.each do |old|
    new = City.new
    new.name = old.name
    new.cityname = old.name_seo
    # new.descr = old.descr
    new.x = old.x
    new.y = old.y
    new.is_feature = old.is_feature
    new.created_at = old.created_at
    new.updated_at = old.updated_at
    new.save
  end
end

Instance Method Details

#refresh_features(city_name) ⇒ Object



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
# File 'lib/cities_tasks.rb', line 90

def refresh_features city_name
  do_refresh = Proc.new do |city|
    puts! "City #{city.name} has #{city.features.length} features."

    city.features.each do |f|
      if !f.report.blank?
        f.name = f.report.name
        f.subhead = f.report.subhead
        f.link_path = report_path( f.report, f.report.city )
        f.save
      elsif !f.gallery.blank?
        f.name = f.gallery.name
        f.link_path = gallery_path( f.gallery )
        f.save
      else
        puts! "A feature is neither a report nor a gallery:"
        puts f.inspect
      end
    end
    if city.save
      puts! "City #{city.name} features refreshed."
    else
      puts! "There was an error:"
      puts city.errors
    end
  end
  if city_name.blank?
    City.all.each do |city|
      do_refresh.call city
    end
  else
    if City.where( :cityname => city_name ).length > 1
      puts! 'Warning: more than one city with this cityname'
    end
    do_refresh.call City.where( :cityname => city_name ).first
  end
end

#refresh_newsitems(cityname) ⇒ Object



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
# File 'lib/cities_tasks.rb', line 41

def refresh_newsitems cityname
  do_refresh = Proc.new do |city|
    # reports
    city.reports.each do |r|
      existing_newsitem = city.newsitems.where( :name => r.name ).first
      if existing_newsitem.blank?
        n = Newsitem.new({ :name => r.name, :link_path => report_path( r, city ), :descr => r.subhead,
                           :username => (r.user || User.new).username })
        city.newsitems << n
      end
    end

    # galleries
    city.galleries.each do |g|
      begin
        image_path = g.photos[0].photo.url(:thumb)
      rescue
        image_path = nil
      end
      newsitem = city.newsitems.where( :name => g.name ).first
      if newsitem.blank?
        city.newsitems << Newsitem.new({ :name => g.name, :link_path => "http://piousbox.com/en/galleries/show/#{g.galleryname}/0", :descr => g.descr,
                                         :username => (g.user || User.new ).username, :image_path => image_path })
      else
        newsitem[:link_path] = "http://piousbox.com/en/galleries/show/#{g.galleryname}/0"
      end
    end
    
    # features
    # I omit that b/c there aren't that many, and new content will overwhelm whatever is there right now.
    #

    if city.save
      puts! "City #{city.name} saved."
    else
      puts! "There were errors with #{city.name}:"
      puts city.errors.inspect
    end
  end

  if cityname.blank?
    City.all.each do |city|
      do_refresh.call city
    end
  else
    do_refresh.call City.where( :cityname => cityname ).first
  end
end