diff --git a/.gitignore b/.gitignore index 1f4495ec1..980c2b664 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ /tmp/* .pt .*.sw* - +*~ diff --git a/app/models/garden.rb b/app/models/garden.rb index 1caf223be..8e17873ce 100644 --- a/app/models/garden.rb +++ b/app/models/garden.rb @@ -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, diff --git a/app/models/planting.rb b/app/models/planting.rb index b09399095..9971e19d1 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -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 diff --git a/db/migrate/20130118043431_add_slug_to_plantings.rb b/db/migrate/20130118043431_add_slug_to_plantings.rb new file mode 100644 index 000000000..e3e9fa46d --- /dev/null +++ b/db/migrate/20130118043431_add_slug_to_plantings.rb @@ -0,0 +1,6 @@ +class AddSlugToPlantings < ActiveRecord::Migration + def change + add_column :plantings, :slug, :string + add_index :plantings, :slug, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 36ef483fb..9ee8655cc 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 => 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 diff --git a/spec/controllers/plantings_controller_spec.rb b/spec/controllers/plantings_controller_spec.rb index e750cc289..4b778a9ca 100644 --- a/spec/controllers/plantings_controller_spec.rb +++ b/spec/controllers/plantings_controller_spec.rb @@ -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 diff --git a/spec/models/garden_spec.rb b/spec/models/garden_spec.rb index e93e6f656..40b17cc3a 100644 --- a/spec/models/garden_spec.rb +++ b/spec/models/garden_spec.rb @@ -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 diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index a874d1c5c..60b22d3df 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -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