From 02ab844347a149f20fb75f56139054683d88bf3f Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Thu, 30 May 2013 08:09:31 +0100 Subject: [PATCH] Add slug to categories. --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app/models/category.rb | 5 ++++- app/views/admin/categories/_category.html.haml | 1 + app/views/admin/categories/_form.html.haml | 2 +- app/views/admin/categories/index.html.haml | 3 ++- db/migrate/20130522071132_add_slug_to_categories.rb | 5 +++++ db/schema.rb | 3 ++- 8 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20130522071132_add_slug_to_categories.rb diff --git a/Gemfile b/Gemfile index 1f0c917..ac1f681 100644 --- a/Gemfile +++ b/Gemfile @@ -70,3 +70,5 @@ gem 'rdiscount' gem 'paperclip' gem 'acts_as_markup' gem 'haml' + +gem 'slugtastic' diff --git a/Gemfile.lock b/Gemfile.lock index 43199c8..a4a816c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -190,6 +190,7 @@ GEM shoulda-matchers (2.1.0) activesupport (>= 3.0.0) slop (3.4.3) + slugtastic (1.2.1) sprockets (2.2.2) hike (~> 1.2) multi_json (~> 1.0) @@ -253,6 +254,7 @@ DEPENDENCIES ruby_parser sass-rails (~> 3.2.0) shoulda-matchers + slugtastic sqlite3-ruby squeel uglifier diff --git a/app/models/category.rb b/app/models/category.rb index 12ce4f1..c944000 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,3 +1,6 @@ class Category < ActiveRecord::Base - has_and_belongs_to_many :photos + has_and_belongs_to_many :photos + validates_presence_of :name, :slug + + has_slug :slug, from: :name end diff --git a/app/views/admin/categories/_category.html.haml b/app/views/admin/categories/_category.html.haml index 8809248..ec7ec69 100644 --- a/app/views/admin/categories/_category.html.haml +++ b/app/views/admin/categories/_category.html.haml @@ -1,5 +1,6 @@ %tr %td= category.name + %td= category.slug %td= category.base_colour %td{ :class => :actions } = link_to 'Show', [:admin, category] diff --git a/app/views/admin/categories/_form.html.haml b/app/views/admin/categories/_form.html.haml index 7961630..7338e98 100644 --- a/app/views/admin/categories/_form.html.haml +++ b/app/views/admin/categories/_form.html.haml @@ -1,3 +1,3 @@ = semantic_form_for [:admin, category] do |f| - = f.inputs :name, :description, :base_colour, :sort + = f.inputs :name, :slug, :description, :base_colour, :sort = f.actions diff --git a/app/views/admin/categories/index.html.haml b/app/views/admin/categories/index.html.haml index 423bf90..d3cfeb7 100644 --- a/app/views/admin/categories/index.html.haml +++ b/app/views/admin/categories/index.html.haml @@ -4,12 +4,13 @@ %thead %tr %th Name + %th Slug %th Base Colour %th %tbody = render :partial => "category", :collection => @categories %tfoot %tr - %td{:colspan => 4} + %td{:colspan => 5} %br/ = link_to 'New Category', new_admin_category_path, :class => [:button, :new] diff --git a/db/migrate/20130522071132_add_slug_to_categories.rb b/db/migrate/20130522071132_add_slug_to_categories.rb new file mode 100644 index 0000000..3615e1b --- /dev/null +++ b/db/migrate/20130522071132_add_slug_to_categories.rb @@ -0,0 +1,5 @@ +class AddSlugToCategories < ActiveRecord::Migration + def change + add_column :categories, :slug, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 5ee3352..01a5354 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 => 20111030164012) do +ActiveRecord::Schema.define(:version => 20130522071132) do create_table "admin_users", :force => true do |t| t.string "email", :default => "", :null => false @@ -47,6 +47,7 @@ ActiveRecord::Schema.define(:version => 20111030164012) do t.integer "photo_id" t.string "base_colour" t.integer "sort" + t.string "slug" end create_table "categories_photos", :id => false, :force => true do |t|