Merge pull request #91 from gnattery/friendly-plantings

Friendly plantings
This commit is contained in:
pozorvlak
2013-01-18 13:06:09 -08:00
8 changed files with 29 additions and 7 deletions

2
.gitignore vendored
View File

@@ -3,4 +3,4 @@
/tmp/*
.pt
.*.sw*
*~

View File

@@ -8,8 +8,7 @@ class Garden < ActiveRecord::Base
has_many :crops, :through => :plantings
def garden_slug
formatted_name = name.downcase.gsub(' ', '-')
"#{owner.login_name}-#{formatted_name}"
"#{owner.login_name}-#{name}".downcase.gsub(' ', '-')
end
# featured plantings returns the most recent 4 plantings for a garden,

View File

@@ -1,9 +1,16 @@
class Planting < ActiveRecord::Base
extend FriendlyId
friendly_id :planting_slug, use: :slugged
attr_accessible :crop_id, :description, :garden_id, :planted_at, :quantity
belongs_to :garden
belongs_to :crop
def planting_slug
"#{owner.login_name}-#{garden.name}-#{crop.system_name}".downcase.gsub(' ', '-')
end
def location
return "#{garden.owner.login_name}'s #{garden.name}"
end

View File

@@ -0,0 +1,6 @@
class AddSlugToPlantings < ActiveRecord::Migration
def change
add_column :plantings, :slug, :string
add_index :plantings, :slug, unique: true
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130118031942) do
ActiveRecord::Schema.define(:version => 20130118043431) do
create_table "crops", :force => true do |t|
t.string "system_name", :null => false
@@ -75,8 +75,11 @@ ActiveRecord::Schema.define(:version => 20130118031942) do
t.text "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "slug"
end
add_index "plantings", ["slug"], :name => "index_plantings_on_slug", :unique => true
create_table "posts", :force => true do |t|
t.integer "author_id", :null => false
t.string "subject", :null => false

View File

@@ -2,8 +2,11 @@ require 'spec_helper'
describe PlantingsController do
def valid_attributes
{ :garden_id => 1, :crop_id => 1 }
def valid_attributes
{
:garden_id => FactoryGirl.create(:garden).id,
:crop_id => FactoryGirl.create(:crop).id
}
end
def valid_session

View File

@@ -7,7 +7,7 @@ describe Garden do
end
it "should have a slug" do
@garden.garden_slug.should == "member1-springfield-community-garden"
@garden.slug.should == "member1-springfield-community-garden"
end
it "should have a description" do

View File

@@ -19,4 +19,8 @@ describe Planting do
@planting.location.should match /^member1's Springfield Community Garden$/
end
it "should have a slug" do
@planting.slug.should == "member1-springfield-community-garden-tomato"
end
end