diff --git a/app/models/category.rb b/app/models/category.rb index c944000..0973e0b 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,6 +1,7 @@ class Category < ActiveRecord::Base has_and_belongs_to_many :photos validates_presence_of :name, :slug + validates_uniqueness_of :name, :slug has_slug :slug, from: :name end diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb new file mode 100644 index 0000000..47c01a1 --- /dev/null +++ b/spec/factories/categories.rb @@ -0,0 +1,7 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :category do + name "Test Category" + end +end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb new file mode 100644 index 0000000..781f025 --- /dev/null +++ b/spec/models/category_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe Category do + it { should have_and_belong_to_many(:photos) } + it { should validate_presence_of(:name) } + it { should validate_presence_of(:slug) } + it { should validate_uniqueness_of(:name) } + it { should validate_uniqueness_of(:slug) } + + let(:category) { create(:category, name: "A Test Category") } + + it "should have a slug generated from name" do + expect(category.slug).to eql("a-test-category") + end +end