Class: Area

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/area.rb

Constant Summary collapse

SEND_MODES =

Constants ============================================================

%w{immediate batched}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_selectObject



134
135
136
# File 'app/models/area.rb', line 134

def self.for_select
  Area.order('state, city, name').collect{|a| [[a.name, a.city, a.state].join(', '), a.id]}
end

.newposts_countObject

Return a hash with the form => posts.count



63
64
65
66
67
68
69
70
# File 'app/models/area.rb', line 63

def self.newposts_count
  connection.select_rows("
    SELECT area_id, COUNT(posts.id) 
    FROM posts LEFT JOIN areas ON (posts.area_id = areas.id)
    WHERE issue_id IS NULL
    GROUP BY area_id
  ").inject({}){|h, count| h[count[0].to_i] = count[1].to_i; h}
end

Instance Method Details

#border_coordinatesObject



107
108
109
# File 'app/models/area.rb', line 107

def border_coordinates
  @border_coordinates ||= border.rings.first.points.collect{|point| "new google.maps.LatLng(#{point.x}, #{point.y})"}.join(',')
end

#boundsObject



99
100
101
102
103
104
105
# File 'app/models/area.rb', line 99

def bounds
  return [] unless self.border.present?
  [
    [self.border.envelope.lower_corner.x, self.border.envelope.lower_corner.y],
    [self.border.envelope.upper_corner.x, self.border.envelope.upper_corner.y]
  ]
end

#centerObject

Returns a Point object



93
94
95
96
97
# File 'app/models/area.rb', line 93

def center
  if self.border.present?
    border.envelope.center
  end
end

#coordinates=(points) ⇒ Object

Instance Methods =====================================================



74
75
76
77
# File 'app/models/area.rb', line 74

def coordinates=(points)
  coords = points.collect{|k, v| [v[0].to_f, v[1].to_f]}
  self.border = Polygon.from_coordinates([coords + [coords.first]])
end

#current_issueObject



115
116
117
# File 'app/models/area.rb', line 115

def current_issue
  @current_issue ||= self.issues.where(:sent_at => nil).first
end

#emailObject



138
139
140
141
142
# File 'app/models/area.rb', line 138

def email
  if defined?(OPEN_PORCH_POP3)
    "#{self.slug}@#{OPEN_PORCH_POP3['mailto']}"
  end
end

#locationObject



111
112
113
# File 'app/models/area.rb', line 111

def location
  [self.city, self.state].compact.join(', ')
end

#openx_ad(zone_name) ⇒ Object

afr.php returns HTML code



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/area.rb', line 145

def openx_ad(zone_name)
  return unless defined?(OPEN_PORCH_ZONES)
  time = Time.now.to_i
  openx_url = "http://d1.openx.org"

  case zone_name.to_sym
  when :newsletter_text
    url = URI.parse(openx_url)
    res = Net::HTTP.start(url.host, url.port) {|http|
      http.get("/afr.php?zoneid=#{OPEN_PORCH_ZONES[zone_name.to_s]}&region=#{self.slug}&cb=#{time}")
    }
    res.body.gsub("\n", '').gsub(/.*<body>(.*)<div id='beacon.*/, '\1')
  else
    %{
      <a href="#{openx_url}/ck.php?cb=#{time}&n=a8417ca6", class="ad #{zone_name}" target="_blank">
        <img src="http://d1.openx.org/avw.php?zoneid=#{OPEN_PORCH_ZONES[zone_name.to_s]}&region=#{self.slug}&cb=#{time}&n=a8417ca6" />
      </a>
    }.html_safe
  end
end

#record_activity_for!(field) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'app/models/area.rb', line 123

def record_activity_for!(field)
  field = field.to_s
  if AreaActivity::TRACKABLE.include?(field)
    activity = self.activities.find_or_create_by_day(Time.now.utc.to_date)
    activity.increment!([field, 'count'].join('_'), 1)
    activity.reload
  else
    raise "Cannot find field in the list of trackable fields. Currently tracking: #{AreaActivity::TRACKABLE.join(', ')}"
  end
end

#send_mode?(mode) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/area.rb', line 119

def send_mode?(mode)
  self.send_mode == mode.to_s
end

#to_aObject

Returns an array with coordinates removing the last (repeated) point



82
83
84
85
86
87
88
89
90
# File 'app/models/area.rb', line 82

def to_a
  if self.border.present?
    {
      :id => self.id,
      :name => self.name,
      :points => self.border.first.points[0..-2].collect{|p| [p.x, p.y]}
    }
  end
end