diff --git a/Gemfile b/Gemfile index b34df05..9fbc973 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ gem 'mysql2' #gem 'haml-rails' gem 'haml' gem 'simple_form' +gem 'friendly_id' gem 'bcrypt-ruby', :require => 'bcrypt' gem 'paperclip', :git => "git://github.com/thoughtbot/paperclip.git" diff --git a/Gemfile.lock b/Gemfile.lock index b8175ce..96a2f89 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -84,6 +84,7 @@ GEM factory_girl (~> 4.2.0) railties (>= 3.0.0) ffi (1.0.11) + friendly_id (4.0.8) haml (3.1.4) highline (1.6.11) hike (1.2.1) @@ -192,6 +193,7 @@ DEPENDENCIES capybara-webkit coffee-rails (~> 3.2.1) factory_girl_rails + friendly_id haml jquery-rails mysql2 diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb new file mode 100644 index 0000000..6572d9d --- /dev/null +++ b/app/controllers/categories_controller.rb @@ -0,0 +1,15 @@ +class CategoriesController < ApplicationController + before_filter :find_category, :only => [:show] + def index + + end + + def show + + end + + private + def find_category + @category = Category.find params[:id] + end +end diff --git a/app/models/category.rb b/app/models/category.rb index 14eb65b..b0e0cfa 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,7 +1,10 @@ class Category < ActiveRecord::Base + extend FriendlyId attr_accessible :name, :exclusive has_many :subcategories has_many :products validates :name, :presence => true + + friendly_id :name, :use => :slugged end diff --git a/app/models/subcategory.rb b/app/models/subcategory.rb index 7182404..c967858 100644 --- a/app/models/subcategory.rb +++ b/app/models/subcategory.rb @@ -1,8 +1,11 @@ class Subcategory < ActiveRecord::Base + extend FriendlyId belongs_to :category has_many :products attr_accessible :name, :category_id, :order validates :name, :presence => true validates_presence_of :category + + friendly_id :name, :use => :slugged end diff --git a/app/views/categories/show.html.haml b/app/views/categories/show.html.haml new file mode 100644 index 0000000..100031a --- /dev/null +++ b/app/views/categories/show.html.haml @@ -0,0 +1 @@ +%h1 Category!! diff --git a/config/routes.rb b/config/routes.rb index 09cac59..3d30250 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,9 @@ end end + match ':category_name' => 'category#show', as: :category_show, via: :get + match ':category_name/:subcategory_name' => 'subcategories#show', as: :category_subcategory, via: :get + # See how all your routes lay out with "rake routes" # This is a legacy wild controller route that's not recommended for RESTful applications. diff --git a/db/migrate/20130605055014_add_slugsto_category_subcategory.rb b/db/migrate/20130605055014_add_slugsto_category_subcategory.rb new file mode 100644 index 0000000..b15efa0 --- /dev/null +++ b/db/migrate/20130605055014_add_slugsto_category_subcategory.rb @@ -0,0 +1,11 @@ +class AddSlugstoCategorySubcategory < ActiveRecord::Migration + def change + # Change to Category table + add_column :categories, :slug, :string + add_index :categories, :slug, unique: true + + #Change to Subcategory table + add_column :subcategories, :slug, :string + add_index :subcategories, :slug, unique: true + end +end diff --git a/db/migrate/20130605061330_create_friendly_id_slugs.rb b/db/migrate/20130605061330_create_friendly_id_slugs.rb new file mode 100644 index 0000000..bb80e48 --- /dev/null +++ b/db/migrate/20130605061330_create_friendly_id_slugs.rb @@ -0,0 +1,18 @@ +class CreateFriendlyIdSlugs < ActiveRecord::Migration + + def self.up + create_table :friendly_id_slugs do |t| + t.string :slug, :null => false + t.integer :sluggable_id, :null => false + t.string :sluggable_type, :limit => 40 + t.datetime :created_at + end + add_index :friendly_id_slugs, :sluggable_id + add_index :friendly_id_slugs, [:slug, :sluggable_type], :unique => true + add_index :friendly_id_slugs, :sluggable_type + end + + def self.down + drop_table :friendly_id_slugs + end +end diff --git a/db/schema.rb b/db/schema.rb index 0427586..b4b799f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,13 +11,27 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130603015906) do +ActiveRecord::Schema.define(:version => 20130605061330) do create_table "categories", :force => true do |t| t.string "name", :null => false t.boolean "exclusive", :default => false, :null => false + t.string "slug" end + add_index "categories", ["slug"], :name => "index_categories_on_slug", :unique => true + + create_table "friendly_id_slugs", :force => true do |t| + t.string "slug", :null => false + t.integer "sluggable_id", :null => false + t.string "sluggable_type", :limit => 40 + t.datetime "created_at" + end + + add_index "friendly_id_slugs", ["slug", "sluggable_type"], :name => "index_friendly_id_slugs_on_slug_and_sluggable_type", :unique => true + add_index "friendly_id_slugs", ["sluggable_id"], :name => "index_friendly_id_slugs_on_sluggable_id" + add_index "friendly_id_slugs", ["sluggable_type"], :name => "index_friendly_id_slugs_on_sluggable_type" + create_table "products", :force => true do |t| t.integer "code" t.string "name" @@ -37,8 +51,11 @@ t.string "name", :null => false t.integer "category_id", :null => false t.integer "order", :default => 1 + t.string "slug" end + add_index "subcategories", ["slug"], :name => "index_subcategories_on_slug", :unique => true + create_table "users", :force => true do |t| t.string "first_name", :null => false t.string "last_name", :null => false