Skip to content

Commit

Permalink
Extract module. [#82]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Aug 2, 2013
1 parent 8342acc commit 6d3ac25
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 37 deletions.
15 changes: 2 additions & 13 deletions app/models/event.rb
@@ -1,12 +1,8 @@
# coding: UTF-8
require 'acts/geocoded'

class Event < ActiveRecord::Base
acts_as_addressed
geocoded_by :address_for_geocoding do |event, results|
if result = results.first
event.coords = Point.from_x_y result.longitude, result.latitude
end
end
acts_as_geocoded

belongs_to :created_by, :class_name => "User"
belongs_to :calendar
Expand All @@ -18,7 +14,6 @@ class Event < ActiveRecord::Base
validates_presence_of :state_id

before_create :set_created_by_id
after_validation :geocode

default_scope :conditions => 'deleted is distinct from true'

Expand Down Expand Up @@ -106,10 +101,4 @@ def set_created_by_id
self.created_by = User.current_user
end
end

private

def address_for_geocoding
address.to_s :geo
end
end
37 changes: 13 additions & 24 deletions app/models/user.rb
@@ -1,18 +1,13 @@
# coding: UTF-8

require 'acts/geocoded'
require 'digest/sha1'

class User < ActiveRecord::Base
acts_as_authentic do |c|
c.transition_from_restful_authentication = true
end

acts_as_addressed
geocoded_by :address_for_geocoding do |user, results|
if result = results.first
user.coords = Point.from_x_y result.longitude, result.latitude
end
end
acts_as_geocoded

cattr_accessor :current_user

Expand All @@ -31,7 +26,6 @@ class User < ActiveRecord::Base
validates_uniqueness_of :email, :case_sensitive => false
before_save :make_single_access_token
after_create :set_calendar
after_validation :geocode

# prevents a user from submitting a crafted form that bypasses activation
# anything else you want your user to change should be added here.
Expand Down Expand Up @@ -77,25 +71,20 @@ def to_s(format = :first_last)
end

protected
def password_required?
crypted_password.blank? || !password.blank? || !password_confirmation.blank?
end

def make_single_access_token
if single_access_token.blank?
reset_single_access_token!
end
end
def password_required?
crypted_password.blank? || !password.blank? || !password_confirmation.blank?
end

def set_calendar
if Calendar.count == 1
permissions.create(:user => self, :calendar => Calendar.find(:first), :role => Role.find_or_create_by_name('user'))
end
def make_single_access_token
if single_access_token.blank?
reset_single_access_token!
end
end

private

def address_for_geocoding
address.to_s :geo
def set_calendar
if Calendar.count == 1
permissions.create(:user => self, :calendar => Calendar.find(:first), :role => Role.find_or_create_by_name('user'))
end
end
end
29 changes: 29 additions & 0 deletions lib/acts/geocoded.rb
@@ -0,0 +1,29 @@
module Acts
module Geocoded
extend ActiveSupport::Concern

included do
acts_as_addressed
geocoded_by :address_for_geocoding do |model, results|
if result = results.first
model.coords = Point.from_x_y result.longitude, result.latitude
end
end
after_validation :geocode
end

module InstanceMethods
private

def address_for_geocoding
address.to_s :geo
end
end
end
end

ActiveRecord::Base.class_eval do
def self.acts_as_geocoded
include Acts::Geocoded
end
end
46 changes: 46 additions & 0 deletions spec/lib/acts/geocoded_spec.rb
@@ -0,0 +1,46 @@
require 'spec_helper'
require 'acts/geocoded'

describe Acts::Geocoded do
describe '.acts_as_geocoded' do
let(:klass) { Class.new ActiveRecord::Base }

it 'should include the module' do
klass.acts_as_geocoded
klass.should include Acts::Geocoded
end

it 'should imply acts_as_addressed' do
klass.should_receive :acts_as_addressed
klass.acts_as_geocoded
end

context 'setting coords' do
let(:address) { Faker::Lorem.sentence }
let(:model) do
klass.stub connection: nil, columns: [], transaction: true
klass.acts_as_geocoded
klass.new
end

before(:each) { model.stub address_for_geocoding: address }

it "should geocode based on the host's address" do
coords = {'latitude' => 1.0, 'longitude' => 2.0}
Geocoder::Lookup::Test.add_stub address, [coords]
point = double 'Point'
Point.should_receive(:from_x_y).with(2.0, 1.0).and_return point
model.should_receive(:coords=).with(point).and_return true
model.geocode
Geocoder::Lookup::Test.stubs.delete address
end

it "should not set coords if the geocoder doesn't return anything" do
Geocoder::Lookup::Test.add_stub address, []
model.should_not_receive :coords=
model.geocode
Geocoder::Lookup::Test.stubs.delete address
end
end
end
end

0 comments on commit 6d3ac25

Please sign in to comment.