mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-18 13:38:24 -04:00
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)
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require 'geocodable'
|
||||
|
||||
Geocoder.configure(
|
||||
:units => :km,
|
||||
:timeout => 10,
|
||||
|
||||
10
db/migrate/20131025104228_add_fields_to_gardens.rb
Normal file
10
db/migrate/20131025104228_add_fields_to_gardens.rb
Normal file
@@ -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
|
||||
16
db/schema.rb
16
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"
|
||||
|
||||
22
lib/geocodable.rb
Normal file
22
lib/geocodable.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user