From 803f8244c9aa9b747ea8403ea71a73c85bd655ba Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 25 Oct 2013 21:54:13 +1100 Subject: [PATCH] Added various fields to garden - active (default: true) - location, latitude and longitude (because when you move house, you don't take your garden with you) - area and area units (square feet or metres) --- app/models/garden.rb | 10 +++++++-- app/models/member.rb | 18 ++------------- config/initializers/geocoder.rb | 2 ++ .../20131025104228_add_fields_to_gardens.rb | 10 +++++++++ db/schema.rb | 16 +++++++++----- lib/geocodable.rb | 22 +++++++++++++++++++ 6 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 db/migrate/20131025104228_add_fields_to_gardens.rb create mode 100644 lib/geocodable.rb diff --git a/app/models/garden.rb b/app/models/garden.rb index 13d8927c6..ec9146c7b 100644 --- a/app/models/garden.rb +++ b/app/models/garden.rb @@ -1,13 +1,19 @@ class Garden < ActiveRecord::Base + include Geocodable extend FriendlyId friendly_id :garden_slug, use: :slugged - attr_accessible :name, :slug, :owner_id, :description + attr_accessible :name, :slug, :owner_id, :description, :active, + :location, :latitude, :longitude, :area, :area_unit + belongs_to :owner, :class_name => 'Member', :foreign_key => 'owner_id' has_many :plantings, :order => 'created_at DESC', :dependent => :destroy has_many :crops, :through => :plantings - # before_create :replace_blank_name + # set up geocoding + geocoded_by :location + after_validation :geocode + after_validation :empty_unwanted_geocodes default_scope order("lower(name) asc") diff --git a/app/models/member.rb b/app/models/member.rb index b39d01ea0..10c293c99 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -1,5 +1,7 @@ class Member < ActiveRecord::Base + include Geocodable extend FriendlyId + friendly_id :login_name, use: :slugged has_many :posts, :foreign_key => 'author_id' @@ -216,22 +218,6 @@ class Member < ActiveRecord::Base return nearby_members end - private - - def geocode - unless self.location.blank? - self.latitude, self.longitude = - Geocoder.coordinates(location, params: {limit: 1}) - end - end - - def empty_unwanted_geocodes - if self.location.blank? - self.latitude = nil - self.longitude = nil - end - end - def update_newsletter_subscription if confirmed_at_changed? and newsletter # just signed up newsletter_subscribe diff --git a/config/initializers/geocoder.rb b/config/initializers/geocoder.rb index 2f99c783c..16de69ec0 100644 --- a/config/initializers/geocoder.rb +++ b/config/initializers/geocoder.rb @@ -1,3 +1,5 @@ +require 'geocodable' + Geocoder.configure( :units => :km, :timeout => 10, diff --git a/db/migrate/20131025104228_add_fields_to_gardens.rb b/db/migrate/20131025104228_add_fields_to_gardens.rb new file mode 100644 index 000000000..24b73c966 --- /dev/null +++ b/db/migrate/20131025104228_add_fields_to_gardens.rb @@ -0,0 +1,10 @@ +class AddFieldsToGardens < ActiveRecord::Migration + def change + add_column :gardens, :active, :boolean, :default => true + add_column :gardens, :location, :string + add_column :gardens, :latitude, :float + add_column :gardens, :longitude, :float + add_column :gardens, :area, :decimal + add_column :gardens, :area_unit, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index e5cccba8d..3a85e3b24 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20131018101204) do +ActiveRecord::Schema.define(:version => 20131025104228) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -76,12 +76,18 @@ ActiveRecord::Schema.define(:version => 20131018101204) do add_index "forums", ["slug"], :name => "index_forums_on_slug", :unique => true create_table "gardens", :force => true do |t| - t.string "name", :null => false + t.string "name", :null => false t.integer "owner_id" - t.string "slug", :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.string "slug", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.text "description" + t.boolean "active", :default => true + t.string "location" + t.float "latitude" + t.float "longitude" + t.decimal "area" + t.string "area_unit" end add_index "gardens", ["owner_id"], :name => "index_gardens_on_user_id" diff --git a/lib/geocodable.rb b/lib/geocodable.rb new file mode 100644 index 000000000..2b914b535 --- /dev/null +++ b/lib/geocodable.rb @@ -0,0 +1,22 @@ +module Geocodable + def self.included(base) + base.extend(self) + end + + private + + def geocode + unless self.location.blank? + self.latitude, self.longitude = + Geocoder.coordinates(location, params: {limit: 1}) + end + end + + def empty_unwanted_geocodes + if self.location.blank? + self.latitude = nil + self.longitude = nil + end + end + +end